summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/test/string.rb
diff options
context:
space:
mode:
authorHiroshi Mimaki <[email protected]>2019-10-18 14:46:03 +0900
committerHiroshi Mimaki <[email protected]>2019-10-18 14:46:03 +0900
commitb6546835457d1935a9c77965686b2a1256874d96 (patch)
tree724cfd71a7c956b0648e8c58f3717d797fff5f29 /mrbgems/mruby-string-ext/test/string.rb
parent8ee516436b8d174a50764939bee23a442aa00b3f (diff)
parent20d01f118ddb7e7f2f36926a7a3db35573611857 (diff)
downloadmruby-b6546835457d1935a9c77965686b2a1256874d96.tar.gz
mruby-b6546835457d1935a9c77965686b2a1256874d96.zip
Merge master.
Diffstat (limited to 'mrbgems/mruby-string-ext/test/string.rb')
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb82
1 files changed, 32 insertions, 50 deletions
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 44ca1fde2..3f11c00a0 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -2,39 +2,18 @@
##
# String(Ext) Test
-UTF8STRING = ("\343\201\202".size == 1)
+UTF8STRING = __ENCODING__ == "UTF-8"
-assert('String#getbyte') do
- str1 = "hello"
- bytes1 = [104, 101, 108, 108, 111]
- assert_equal bytes1[0], str1.getbyte(0)
- assert_equal bytes1[-1], str1.getbyte(-1)
- assert_equal bytes1[6], str1.getbyte(6)
-
- str2 = "\xFF"
- bytes2 = [0xFF]
- assert_equal bytes2[0], str2.getbyte(0)
-end
-
-assert('String#setbyte') do
- str1 = "hello"
- h = "H".getbyte(0)
- str1.setbyte(0, h)
- assert_equal(h, str1.getbyte(0))
- assert_equal("Hello", str1)
-end
-
-assert('String#byteslice') do
- str1 = "hello"
- assert_equal("e", str1.byteslice(1))
- assert_equal("o", str1.byteslice(-1))
- assert_equal("ell", str1.byteslice(1..3))
- assert_equal("el", str1.byteslice(1...3))
+def assert_upto(exp, receiver, *args)
+ act = []
+ receiver.upto(*args) { |v| act << v }
+ assert_equal exp, act
end
assert('String#dump') do
assert_equal("\"\\x00\"", "\0".dump)
assert_equal("\"foo\"", "foo".dump)
+ assert_equal('"\xe3\x82\x8b"', "る".dump)
assert_nothing_raised { ("\1" * 100).dump } # regress #1210
end
@@ -116,8 +95,15 @@ end
assert('String#concat') do
assert_equal "Hello World!", "Hello " << "World" << 33
assert_equal "Hello World!", "Hello ".concat("World").concat(33)
-
assert_raise(TypeError) { "".concat(Object.new) }
+
+ if UTF8STRING
+ assert_equal "H«", "H" << 0xab
+ assert_equal "Hは", "H" << 12399
+ else
+ assert_equal "H\xab", "H" << 0xab
+ assert_raise(RangeError) { "H" << 12399 }
+ end
end
assert('String#casecmp') do
@@ -247,12 +233,6 @@ assert('String#oct') do
assert_equal (-8), "-10".oct
end
-assert('String#chr') do
- assert_equal "a", "abcde".chr
- # test Fixnum#chr as well
- assert_equal "a", 97.chr
-end
-
assert('String#lines') do
assert_equal ["Hel\n", "lo\n", "World!"], "Hel\nlo\nWorld!".lines
assert_equal ["Hel\n", "lo\n", "World!\n"], "Hel\nlo\nWorld!\n".lines
@@ -539,16 +519,15 @@ assert('String#rjust should raise on zero width padding') do
end
assert('String#upto') do
- assert_equal %w(a8 a9 b0 b1 b2 b3 b4 b5 b6), "a8".upto("b6").to_a
- assert_equal ["9", "10", "11"], "9".upto("11").to_a
- assert_equal [], "25".upto("5").to_a
- assert_equal ["07", "08", "09", "10", "11"], "07".upto("11").to_a
-
-if UTF8STRING
- assert_equal ["あ", "ぃ", "い", "ぅ", "う", "ぇ", "え", "ぉ", "お"], "あ".upto("お").to_a
-end
-
- assert_equal ["9", ":", ";", "<", "=", ">", "?", "@", "A"], "9".upto("A").to_a
+ assert_upto %w(a8 a9 b0 b1 b2 b3 b4 b5 b6), "a8", "b6"
+ assert_upto ["9", "10", "11"], "9", "11"
+ assert_upto [], "25", "5"
+ assert_upto ["07", "08", "09", "10", "11"], "07", "11"
+ assert_upto ["9", ":", ";", "<", "=", ">", "?", "@", "A"], "9", "A"
+
+ if UTF8STRING
+ assert_upto %w(あ ぃ い ぅ う ぇ え ぉ お), "あ", "お"
+ end
a = "aa"
start = "aa"
@@ -630,8 +609,11 @@ assert('String#ord(UTF-8)') do
end if UTF8STRING
assert('String#chr') do
+ assert_equal "a", "abcde".chr
assert_equal "h", "hello!".chr
+ assert_equal "", "".chr
end
+
assert('String#chr(UTF-8)') do
assert_equal "こ", "こんにちは世界!".chr
end if UTF8STRING
@@ -657,19 +639,19 @@ assert('String#chars(UTF-8)') do
end if UTF8STRING
assert('String#each_char') do
- s = ""
+ chars = []
"hello!".each_char do |x|
- s += x
+ chars << x
end
- assert_equal "hello!", s
+ assert_equal ["h", "e", "l", "l", "o", "!"], chars
end
assert('String#each_char(UTF-8)') do
- s = ""
+ chars = []
"こんにちは世界!".each_char do |x|
- s += x
+ chars << x
end
- assert_equal "こんにちは世界!", s
+ assert_equal ["こ", "ん", "に", "ち", "は", "世", "界", "!"], chars
end if UTF8STRING
assert('String#codepoints') do