diff options
Diffstat (limited to 'test/ruby/test_string.rb')
| -rw-r--r-- | test/ruby/test_string.rb | 1070 |
1 files changed, 340 insertions, 730 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 22bec09855..6c00aa15f9 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -83,7 +83,7 @@ class TestString < Test::Unit::TestCase end def test_initialize_shared - S(str = "mystring" * 10).__send__(:initialize, capacity: str.bytesize) + String.new(str = "mystring" * 10).__send__(:initialize, capacity: str.bytesize) assert_equal("mystring", str[0, 8]) end @@ -97,10 +97,8 @@ class TestString < Test::Unit::TestCase end def test_initialize_memory_leak - return unless @cls == String - assert_no_memory_leak([], <<-PREP, <<-CODE, rss: true) -code = proc {('x'*100_000).__send__(:initialize, '')} +code = proc {('x'*100000).__send__(:initialize, '')} 1_000.times(&code) PREP 100_000.times(&code) @@ -109,10 +107,8 @@ CODE # Bug #18154 def test_initialize_nofree_memory_leak - return unless @cls == String - assert_no_memory_leak([], <<-PREP, <<-CODE, rss: true) -code = proc {0.to_s.__send__(:initialize, capacity: 100_000)} +code = proc {0.to_s.__send__(:initialize, capacity: 10000)} 1_000.times(&code) PREP 100_000.times(&code) @@ -244,23 +240,23 @@ CODE assert_equal(-1, S("ABCDEF") <=> S("abcdef")) - assert_nil(S("foo") <=> Object.new) + assert_nil("foo" <=> Object.new) o = Object.new def o.to_str; "bar"; end - assert_equal(1, S("foo") <=> o) + assert_equal(1, "foo" <=> o) class << o;remove_method :to_str;end def o.<=>(x); nil; end - assert_nil(S("foo") <=> o) + assert_nil("foo" <=> o) class << o;remove_method :<=>;end def o.<=>(x); 1; end - assert_equal(-1, S("foo") <=> o) + assert_equal(-1, "foo" <=> o) class << o;remove_method :<=>;end def o.<=>(x); 2**100; end - assert_equal(-1, S("foo") <=> o) + assert_equal(-1, "foo" <=> o) end def test_EQUAL # '==' @@ -274,10 +270,10 @@ CODE o = Object.new def o.to_str; end def o.==(x); false; end - assert_equal(false, S("foo") == o) + assert_equal(false, "foo" == o) class << o;remove_method :==;end def o.==(x); true; end - assert_equal(true, S("foo") == o) + assert_equal(true, "foo" == o) end def test_LSHIFT # '<<' @@ -652,36 +648,15 @@ CODE result << 0x0300 expected = S("\u0300".encode(Encoding::UTF_16LE)) assert_equal(expected, result, bug7090) - assert_raise(TypeError) { S('foo') << :foo } - assert_raise(FrozenError) { S('foo').freeze.concat('bar') } + assert_raise(TypeError) { 'foo' << :foo } + assert_raise(FrozenError) { 'foo'.freeze.concat('bar') } end def test_concat_literals - s=S("." * 50) + s="." * 50 assert_equal(Encoding::UTF_8, "#{s}x".encoding) end - def test_string_interpolations_across_size_pools_get_embedded - omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1 - - require 'objspace' - base_slot_size = GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] - small_obj_size = (base_slot_size / 2) - large_obj_size = base_slot_size * 2 - - a = "a" * small_obj_size - b = "a" * large_obj_size - - res = "#{a}, #{b}" - dump_res = ObjectSpace.dump(res) - dump_orig = ObjectSpace.dump(a) - new_slot_size = Integer(dump_res.match(/"slot_size":(\d+)/)[1]) - orig_slot_size = Integer(dump_orig.match(/"slot_size":(\d+)/)[1]) - - assert_match(/"embedded":true/, dump_res) - assert_operator(new_slot_size, :>, orig_slot_size) - end - def test_count a = S("hello world") assert_equal(5, a.count(S("lo"))) @@ -689,18 +664,18 @@ CODE assert_equal(4, a.count(S("hello"), S("^l"))) assert_equal(4, a.count(S("ej-m"))) assert_equal(0, S("y").count(S("a\\-z"))) - assert_equal(5, S("abc\u{3042 3044 3046}").count("^a")) - assert_equal(1, S("abc\u{3042 3044 3046}").count("\u3042")) - assert_equal(5, S("abc\u{3042 3044 3046}").count("^\u3042")) - assert_equal(2, S("abc\u{3042 3044 3046}").count("a-z", "^a")) - assert_equal(0, S("abc\u{3042 3044 3046}").count("a", "\u3042")) - assert_equal(0, S("abc\u{3042 3044 3046}").count("\u3042", "a")) - assert_equal(0, S("abc\u{3042 3044 3046}").count("\u3042", "\u3044")) - assert_equal(4, S("abc\u{3042 3044 3046}").count("^a", "^\u3044")) - assert_equal(4, S("abc\u{3042 3044 3046}").count("^\u3044", "^a")) - assert_equal(4, S("abc\u{3042 3044 3046}").count("^\u3042", "^\u3044")) + assert_equal(5, "abc\u{3042 3044 3046}".count("^a")) + assert_equal(1, "abc\u{3042 3044 3046}".count("\u3042")) + assert_equal(5, "abc\u{3042 3044 3046}".count("^\u3042")) + assert_equal(2, "abc\u{3042 3044 3046}".count("a-z", "^a")) + assert_equal(0, "abc\u{3042 3044 3046}".count("a", "\u3042")) + assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "a")) + assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "\u3044")) + assert_equal(4, "abc\u{3042 3044 3046}".count("^a", "^\u3044")) + assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3044", "^a")) + assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3042", "^\u3044")) - assert_raise(ArgumentError) { S("foo").count } + assert_raise(ArgumentError) { "foo".count } end def crypt_supports_des_crypt? @@ -742,17 +717,17 @@ CODE assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e"))) assert_equal(S("ho"), S("hello").delete(S("ej-m"))) - assert_equal(S("a").hash, S("a\u0101").delete("\u0101").hash, '[ruby-talk:329267]') - assert_equal(true, S("a\u0101").delete("\u0101").ascii_only?) - assert_equal(true, S("a\u3041").delete("\u3041").ascii_only?) - assert_equal(false, S("a\u3041\u3042").delete("\u3041").ascii_only?) + assert_equal("a".hash, "a\u0101".delete("\u0101").hash, '[ruby-talk:329267]') + assert_equal(true, "a\u0101".delete("\u0101").ascii_only?) + assert_equal(true, "a\u3041".delete("\u3041").ascii_only?) + assert_equal(false, "a\u3041\u3042".delete("\u3041").ascii_only?) - assert_equal("a", S("abc\u{3042 3044 3046}").delete("^a")) - assert_equal("bc\u{3042 3044 3046}", S("abc\u{3042 3044 3046}").delete("a")) - assert_equal("\u3042", S("abc\u{3042 3044 3046}").delete("^\u3042")) + assert_equal("a", "abc\u{3042 3044 3046}".delete("^a")) + assert_equal("bc\u{3042 3044 3046}", "abc\u{3042 3044 3046}".delete("a")) + assert_equal("\u3042", "abc\u{3042 3044 3046}".delete("^\u3042")) bug6160 = '[ruby-dev:45374]' - assert_equal("", S('\\').delete('\\'), bug6160) + assert_equal("", '\\'.delete('\\'), bug6160) end def test_delete! @@ -857,10 +832,10 @@ CODE assert_equal(Encoding::UTF_8, S('"\\u3042"').encode(Encoding::EUC_JP).undump.encoding) assert_equal("abc".encode(Encoding::UTF_16LE), - S('"a\x00b\x00c\x00".force_encoding("UTF-16LE")').undump) + '"a\x00b\x00c\x00".force_encoding("UTF-16LE")'.undump) - assert_equal('\#', S('"\\\\#"').undump) - assert_equal('\#{', S('"\\\\\#{"').undump) + assert_equal('\#', '"\\\\#"'.undump) + assert_equal('\#{', '"\\\\\#{"'.undump) assert_raise(RuntimeError) { S('\u3042').undump } assert_raise(RuntimeError) { S('"\x82\xA0\u3042"'.force_encoding("SJIS")).undump } @@ -892,7 +867,7 @@ CODE assert_raise(RuntimeError) { S('"\\"').undump } assert_raise(RuntimeError) { S(%("\0")).undump } assert_raise_with_message(RuntimeError, /invalid/) { - S('"\\u{007F}".xxxxxx').undump + '"\\u{007F}".xxxxxx'.undump } end @@ -908,18 +883,6 @@ CODE end end - class StringWithIVSet < String - def set_iv - @foo = 1 - end - end - - def test_ivar_set_after_frozen_dup - str = StringWithIVSet.new.freeze - str.dup.set_iv - assert_raise(FrozenError) { str.set_iv } - end - def test_each verbose, $VERBOSE = $VERBOSE, nil @@ -1084,9 +1047,9 @@ CODE g = g.encode(enc) assert_equal g.chars, g.grapheme_clusters end - assert_equal ["a", "b", "c"], S("abc").b.grapheme_clusters + assert_equal ["a", "b", "c"], "abc".b.grapheme_clusters - s = S("ABC").b + s = "ABC".b res = [] assert_same s, s.grapheme_clusters {|x| res << x } assert_equal(3, res.size) @@ -1132,7 +1095,7 @@ CODE $/ = save s = nil - S("foo\nbar").each_line(nil) {|s2| s = s2 } + "foo\nbar".each_line(nil) {|s2| s = s2 } assert_equal("foo\nbar", s) assert_equal "hello\n", S("hello\nworld").each_line.next @@ -1140,7 +1103,7 @@ CODE bug7646 = "[ruby-dev:46827]" assert_nothing_raised(bug7646) do - S("\n\u0100").each_line("\n") {} + "\n\u0100".each_line("\n") {} end ensure $/ = save @@ -1154,19 +1117,14 @@ CODE assert_equal(S("world"), res[1]) res = [] - S("hello\n\n\nworld\n").each_line(S(''), chomp: true) {|x| res << x} - assert_equal(S("hello"), res[0]) - assert_equal(S("world\n"), res[1]) - - res = [] - S("hello\r\n\r\nworld\r\n").each_line(S(''), chomp: true) {|x| res << x} - assert_equal(S("hello"), res[0]) - assert_equal(S("world\r\n"), res[1]) + S("hello\n\n\nworld").each_line(S(''), chomp: true) {|x| res << x} + assert_equal(S("hello\n"), res[0]) + assert_equal(S("world"), res[1]) res = [] - S("hello\r\n\n\nworld").each_line(S(''), chomp: true) {|x| res << x} - assert_equal(S("hello"), res[0]) - assert_equal(S("world"), res[1]) + S("hello\r\n\r\nworld").each_line(S(''), chomp: true) {|x| res << x} + assert_equal(S("hello\r\n"), res[0]) + assert_equal(S("world"), res[1]) res = [] S("hello!world").each_line(S('!'), chomp: true) {|x| res << x} @@ -1179,7 +1137,7 @@ CODE assert_equal(S("a"), res[0]) s = nil - S("foo\nbar").each_line(nil, chomp: true) {|s2| s = s2 } + "foo\nbar".each_line(nil, chomp: true) {|s2| s = s2 } assert_equal("foo\nbar", s) assert_equal "hello", S("hello\nworld").each_line(chomp: true).next @@ -1244,9 +1202,9 @@ CODE S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }) assert_equal(S("<>h<>e<>l<>l<>o<>"), S("hello").gsub(S(''), S('<\0>'))) - assert_equal("z", S("abc").gsub(/./, "a" => "z"), "moved from btest/knownbug") + assert_equal("z", "abc".gsub(/./, "a" => "z"), "moved from btest/knownbug") - assert_raise(ArgumentError) { S("foo").gsub } + assert_raise(ArgumentError) { "foo".gsub } end def test_gsub_encoding @@ -1293,23 +1251,23 @@ CODE end def test_sub_hash - assert_equal('azc', S('abc').sub(/b/, "b" => "z")) - assert_equal('ac', S('abc').sub(/b/, {})) - assert_equal('a1c', S('abc').sub(/b/, "b" => 1)) - assert_equal('aBc', S('abc').sub(/b/, Hash.new {|h, k| k.upcase })) - assert_equal('a[\&]c', S('abc').sub(/b/, "b" => '[\&]')) - assert_equal('aBcabc', S('abcabc').sub(/b/, Hash.new {|h, k| h[k] = k.upcase })) - assert_equal('aBcdef', S('abcdef').sub(/de|b/, "b" => "B", "de" => "DE")) + assert_equal('azc', 'abc'.sub(/b/, "b" => "z")) + assert_equal('ac', 'abc'.sub(/b/, {})) + assert_equal('a1c', 'abc'.sub(/b/, "b" => 1)) + assert_equal('aBc', 'abc'.sub(/b/, Hash.new {|h, k| k.upcase })) + assert_equal('a[\&]c', 'abc'.sub(/b/, "b" => '[\&]')) + assert_equal('aBcabc', 'abcabc'.sub(/b/, Hash.new {|h, k| h[k] = k.upcase })) + assert_equal('aBcdef', 'abcdef'.sub(/de|b/, "b" => "B", "de" => "DE")) end def test_gsub_hash - assert_equal('azc', S('abc').gsub(/b/, "b" => "z")) - assert_equal('ac', S('abc').gsub(/b/, {})) - assert_equal('a1c', S('abc').gsub(/b/, "b" => 1)) - assert_equal('aBc', S('abc').gsub(/b/, Hash.new {|h, k| k.upcase })) - assert_equal('a[\&]c', S('abc').gsub(/b/, "b" => '[\&]')) - assert_equal('aBcaBc', S('abcabc').gsub(/b/, Hash.new {|h, k| h[k] = k.upcase })) - assert_equal('aBcDEf', S('abcdef').gsub(/de|b/, "b" => "B", "de" => "DE")) + assert_equal('azc', 'abc'.gsub(/b/, "b" => "z")) + assert_equal('ac', 'abc'.gsub(/b/, {})) + assert_equal('a1c', 'abc'.gsub(/b/, "b" => 1)) + assert_equal('aBc', 'abc'.gsub(/b/, Hash.new {|h, k| k.upcase })) + assert_equal('a[\&]c', 'abc'.gsub(/b/, "b" => '[\&]')) + assert_equal('aBcaBc', 'abcabc'.gsub(/b/, Hash.new {|h, k| h[k] = k.upcase })) + assert_equal('aBcDEf', 'abcdef'.gsub(/de|b/, "b" => "B", "de" => "DE")) end def test_hash @@ -1339,54 +1297,45 @@ CODE end def test_index - assert_index(0, S("hello"), ?h) - assert_index(1, S("hello"), S("ell")) - assert_index(2, S("hello"), /ll./) + assert_equal(0, S("hello").index(?h)) + assert_equal(1, S("hello").index(S("ell"))) + assert_equal(2, S("hello").index(/ll./)) - assert_index(3, S("hello"), ?l, 3) - assert_index(3, S("hello"), S("l"), 3) - assert_index(3, S("hello"), /l./, 3) + assert_equal(3, S("hello").index(?l, 3)) + assert_equal(3, S("hello").index(S("l"), 3)) + assert_equal(3, S("hello").index(/l./, 3)) - assert_index(nil, S("hello"), ?z, 3) - assert_index(nil, S("hello"), S("z"), 3) - assert_index(nil, S("hello"), /z./, 3) + assert_nil(S("hello").index(?z, 3)) + assert_nil(S("hello").index(S("z"), 3)) + assert_nil(S("hello").index(/z./, 3)) - assert_index(nil, S("hello"), ?z) - assert_index(nil, S("hello"), S("z")) - assert_index(nil, S("hello"), /z./) + assert_nil(S("hello").index(?z)) + assert_nil(S("hello").index(S("z"))) + assert_nil(S("hello").index(/z./)) - assert_index(0, S(""), S("")) - assert_index(0, S(""), //) - assert_index(nil, S(""), S("hello")) - assert_index(nil, S(""), /hello/) - assert_index(0, S("hello"), S("")) - assert_index(0, S("hello"), //) + assert_equal(0, S("").index(S(""))) + assert_equal(0, S("").index(//)) + assert_nil(S("").index(S("hello"))) + assert_nil(S("").index(/hello/)) + assert_equal(0, S("hello").index(S(""))) + assert_equal(0, S("hello").index(//)) s = S("long") * 1000 << "x" - assert_index(nil, s, S("y")) - assert_index(4 * 1000, s, S("x")) + assert_nil(s.index(S("y"))) + assert_equal(4 * 1000, s.index(S("x"))) s << "yx" - assert_index(4 * 1000, s, S("x")) - assert_index(4 * 1000, s, S("xyx")) + assert_equal(4 * 1000, s.index(S("x"))) + assert_equal(4 * 1000, s.index(S("xyx"))) o = Object.new def o.to_str; "bar"; end - assert_index(3, S("foobarbarbaz"), o) - assert_raise(TypeError) { S("foo").index(Object.new) } - - assert_index(nil, S("foo"), //, -100) - assert_index(nil, S("foo"), //, 4) - - assert_index(2, S("abcdbce"), /b\Kc/) + assert_equal(3, "foobarbarbaz".index(o)) + assert_raise(TypeError) { "foo".index(Object.new) } - assert_index(0, S("こんにちは"), ?こ) - assert_index(1, S("こんにちは"), S("んにち")) - assert_index(2, S("こんにちは"), /にち./) + assert_nil("foo".index(//, -100)) + assert_nil($~) - assert_index(0, S("にんにちは"), ?に, 0) - assert_index(2, S("にんにちは"), ?に, 1) - assert_index(2, S("にんにちは"), ?に, 2) - assert_index(nil, S("にんにちは"), ?に, 3) + assert_equal(2, S("abcdbce").index(/b\Kc/)) end def test_insert @@ -1493,16 +1442,16 @@ CODE b = a.replace(S("xyz")) assert_equal(S("xyz"), b) - s = S("foo") * 100 + s = "foo" * 100 s2 = ("bar" * 100).dup s.replace(s2) assert_equal(s2, s) - s2 = [S("foo")].pack("p") + s2 = ["foo"].pack("p") s.replace(s2) assert_equal(s2, s) - fs = S("").freeze + fs = "".freeze assert_raise(FrozenError) { fs.replace("a") } assert_raise(FrozenError) { fs.replace(fs) } assert_raise(ArgumentError) { fs.replace() } @@ -1533,57 +1482,34 @@ CODE end def test_rindex - assert_rindex(3, S("hello"), ?l) - assert_rindex(6, S("ell, hello"), S("ell")) - assert_rindex(7, S("ell, hello"), /ll./) + assert_equal(3, S("hello").rindex(?l)) + assert_equal(6, S("ell, hello").rindex(S("ell"))) + assert_equal(7, S("ell, hello").rindex(/ll./)) - assert_rindex(3, S("hello,lo"), ?l, 3) - assert_rindex(3, S("hello,lo"), S("l"), 3) - assert_rindex(3, S("hello,lo"), /l./, 3) + assert_equal(3, S("hello,lo").rindex(?l, 3)) + assert_equal(3, S("hello,lo").rindex(S("l"), 3)) + assert_equal(3, S("hello,lo").rindex(/l./, 3)) - assert_rindex(nil, S("hello"), ?z, 3) - assert_rindex(nil, S("hello"), S("z"), 3) - assert_rindex(nil, S("hello"), /z./, 3) + assert_nil(S("hello").rindex(?z, 3)) + assert_nil(S("hello").rindex(S("z"), 3)) + assert_nil(S("hello").rindex(/z./, 3)) - assert_rindex(nil, S("hello"), ?z) - assert_rindex(nil, S("hello"), S("z")) - assert_rindex(nil, S("hello"), /z./) - - assert_rindex(5, S("hello"), S("")) - assert_rindex(5, S("hello"), S(""), 5) - assert_rindex(4, S("hello"), S(""), 4) - assert_rindex(0, S("hello"), S(""), 0) + assert_nil(S("hello").rindex(?z)) + assert_nil(S("hello").rindex(S("z"))) + assert_nil(S("hello").rindex(/z./)) o = Object.new def o.to_str; "bar"; end - assert_rindex(6, S("foobarbarbaz"), o) - assert_raise(TypeError) { S("foo").rindex(Object.new) } - - assert_rindex(nil, S("foo"), //, -100) + assert_equal(6, "foobarbarbaz".rindex(o)) + assert_raise(TypeError) { "foo".rindex(Object.new) } - m = assert_rindex(3, S("foo"), //) - assert_equal([3, 3], m.offset(0)) - assert_rindex(3, S("foo"), //, 4) - - assert_rindex(5, S("abcdbce"), /b\Kc/) - - assert_rindex(2, S("こんにちは"), ?に) - assert_rindex(6, S("にちは、こんにちは"), S("にちは")) - assert_rindex(6, S("にちは、こんにちは"), /にち./) + assert_nil("foo".rindex(//, -100)) + assert_nil($~) - assert_rindex(6, S("にちは、こんにちは"), S("にちは"), 7) - assert_rindex(6, S("にちは、こんにちは"), S("にちは"), -2) - assert_rindex(6, S("にちは、こんにちは"), S("にちは"), 6) - assert_rindex(6, S("にちは、こんにちは"), S("にちは"), -3) - assert_rindex(0, S("にちは、こんにちは"), S("にちは"), 5) - assert_rindex(0, S("にちは、こんにちは"), S("にちは"), -4) - assert_rindex(0, S("にちは、こんにちは"), S("にちは"), 1) - assert_rindex(0, S("にちは、こんにちは"), S("にちは"), 0) + assert_equal(3, "foo".rindex(//)) + assert_equal([3, 3], $~.offset(0)) - assert_rindex(0, S("こんにちは"), S("こんにちは")) - assert_rindex(nil, S("こんにち"), S("こんにちは")) - assert_rindex(nil, S("こ"), S("こんにちは")) - assert_rindex(nil, S(""), S("こんにちは")) + assert_equal(5, S("abcdbce").rindex(/b\Kc/)) end def test_rjust @@ -1622,15 +1548,6 @@ CODE assert_equal(%w[1 2 3], S("a1 a2 a3").scan(/a\K./)) end - def test_scan_segv - bug19159 = '[Bug #19159]' - assert_nothing_raised(Exception, bug19159) do - ObjectSpace.each_object(MatchData).to_a - "".scan(//) - ObjectSpace.each_object(MatchData).to_a.inspect - end - end - def test_size assert_equal(0, S("").size) assert_equal(4, S("1234").size) @@ -1772,8 +1689,7 @@ CODE assert_equal(S("Bar"), a.slice!(S("Bar"))) assert_equal(S("Foo"), a) - a = S("foo") - assert_raise(ArgumentError) { a.slice! } + assert_raise(ArgumentError) { "foo".slice! } end def test_split @@ -1798,7 +1714,7 @@ CODE assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|'))) assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1)) - assert_equal([], S("").split(//, 1)) + assert_equal([], "".split(//, 1)) ensure EnvUtil.suppress_warning {$; = fs} end @@ -1837,18 +1753,16 @@ CODE result = []; S("a||b|c|").split(S('|'), -1) {|s| result << s} assert_equal([S("a"), S(""), S("b"), S("c"), S("")], result) - result = []; S("").split(//, 1) {|s| result << s} + result = []; "".split(//, 1) {|s| result << s} assert_equal([], result) - result = []; S("aaa,bbb,ccc,ddd").split(/,/) {|s| result << s.gsub(/./, "A")} + result = []; "aaa,bbb,ccc,ddd".split(/,/) {|s| result << s.gsub(/./, "A")} assert_equal(["AAA"]*4, result) ensure EnvUtil.suppress_warning {$; = fs} end def test_fs - return unless @cls == String - assert_raise_with_message(TypeError, /\$;/) { $; = [] } @@ -1938,18 +1852,13 @@ CODE assert_send([S("hello"), :start_with?, S("hel")]) assert_not_send([S("hello"), :start_with?, S("el")]) assert_send([S("hello"), :start_with?, S("el"), S("he")]) - assert_send([S("\xFF\xFE"), :start_with?, S("\xFF")]) - assert_send([S("hello\xBE"), :start_with?, S("hello")]) - assert_not_send([S("\u{c4}"), :start_with?, S("\xC3")]) bug5536 = '[ruby-core:40623]' assert_raise(TypeError, bug5536) {S("str").start_with? :not_convertible_to_string} - end - def test_start_with_regexp - assert_equal(true, S("hello").start_with?(/hel/)) + assert_equal(true, "hello".start_with?(/hel/)) assert_equal("hel", $&) - assert_equal(false, S("hello").start_with?(/el/)) + assert_equal(false, "hello".start_with?(/el/)) assert_nil($&) end @@ -1959,9 +1868,9 @@ CODE assert_equal(S("x"), S("\x00x\x00").strip) assert_equal("0b0 ".force_encoding("UTF-16BE"), - S("\x00 0b0 ").force_encoding("UTF-16BE").strip) + "\x00 0b0 ".force_encoding("UTF-16BE").strip) assert_equal("0\x000b0 ".force_encoding("UTF-16BE"), - S("0\x000b0 ").force_encoding("UTF-16BE").strip) + "0\x000b0 ".force_encoding("UTF-16BE").strip) end def test_strip! @@ -2026,17 +1935,17 @@ CODE o = Object.new def o.to_str; "bar"; end - assert_equal("fooBARbaz", S("foobarbaz").sub(o, "BAR")) + assert_equal("fooBARbaz", "foobarbaz".sub(o, "BAR")) - assert_raise(TypeError) { S("foo").sub(Object.new, "") } + assert_raise(TypeError) { "foo".sub(Object.new, "") } - assert_raise(ArgumentError) { S("foo").sub } + assert_raise(ArgumentError) { "foo".sub } assert_raise(IndexError) { "foo"[/(?:(o$)|(x))/, 2] = 'bar' } o = Object.new def o.to_s; self; end - assert_match(/^foo#<Object:0x.*>baz$/, S("foobarbaz").sub("bar") { o }) + assert_match(/^foo#<Object:0x.*>baz$/, "foobarbaz".sub("bar") { o }) assert_equal(S("Abc"), S("abc").sub("a", "A")) m = nil @@ -2045,7 +1954,7 @@ CODE assert_equal(/a/, m.regexp) bug = '[ruby-core:78686] [Bug #13042] other than regexp has no name references' assert_raise_with_message(IndexError, /oops/, bug) { - S('hello').gsub('hello', '\k<oops>') + 'hello'.gsub('hello', '\k<oops>') } end @@ -2092,18 +2001,18 @@ CODE assert_equal(S("AAAAA000"), S("ZZZZ999").succ) assert_equal(S("*+"), S("**").succ) - assert_equal("abce", S("abcd").succ) - assert_equal("THX1139", S("THX1138").succ) - assert_equal("<\<koalb>>", S("<\<koala>>").succ) - assert_equal("2000aaa", S("1999zzz").succ) - assert_equal("AAAA0000", S("ZZZ9999").succ) - assert_equal("**+", S("***").succ) + assert_equal("abce", "abcd".succ) + assert_equal("THX1139", "THX1138".succ) + assert_equal("<\<koalb>>", "<\<koala>>".succ) + assert_equal("2000aaa", "1999zzz".succ) + assert_equal("AAAA0000", "ZZZ9999".succ) + assert_equal("**+", "***".succ) - assert_equal("!", S(" ").succ) - assert_equal("", S("").succ) + assert_equal("!", " ".succ) + assert_equal("", "".succ) bug = '[ruby-core:83062] [Bug #13952]' - s = S("\xff").b + s = "\xff".b assert_not_predicate(s, :ascii_only?) assert_predicate(s.succ, :ascii_only?, bug) end @@ -2155,8 +2064,8 @@ CODE assert_equal(S(""), a.succ!) assert_equal(S(""), a) - assert_equal("aaaaaaaaaaaa", S("zzzzzzzzzzz").succ!) - assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", S("zzzzzzzzzzzzzzzzzzzzzzz").succ!) + assert_equal("aaaaaaaaaaaa", "zzzzzzzzzzz".succ!) + assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzz".succ!) end def test_sum @@ -2178,8 +2087,8 @@ CODE end def test_sum_2 - assert_equal(0, S("").sum) - assert_equal(294, S("abc").sum) + assert_equal(0, "".sum) + assert_equal(294, "abc".sum) check_sum("abc") check_sum("\x80") -3.upto(70) {|bits| @@ -2226,39 +2135,39 @@ CODE def test_to_i assert_equal(1480, S("1480ft/sec").to_i) assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i) - assert_equal(0, S(" 0").to_i) - assert_equal(0, S("+0").to_i) - assert_equal(0, S("-0").to_i) - assert_equal(0, S("--0").to_i) - assert_equal(16, S("0x10").to_i(0)) - assert_equal(16, S("0X10").to_i(0)) - assert_equal(2, S("0b10").to_i(0)) - assert_equal(2, S("0B10").to_i(0)) - assert_equal(8, S("0o10").to_i(0)) - assert_equal(8, S("0O10").to_i(0)) - assert_equal(10, S("0d10").to_i(0)) - assert_equal(10, S("0D10").to_i(0)) - assert_equal(8, S("010").to_i(0)) - assert_raise(ArgumentError) { S("010").to_i(-10) } + assert_equal(0, " 0".to_i) + assert_equal(0, "+0".to_i) + assert_equal(0, "-0".to_i) + assert_equal(0, "--0".to_i) + assert_equal(16, "0x10".to_i(0)) + assert_equal(16, "0X10".to_i(0)) + assert_equal(2, "0b10".to_i(0)) + assert_equal(2, "0B10".to_i(0)) + assert_equal(8, "0o10".to_i(0)) + assert_equal(8, "0O10".to_i(0)) + assert_equal(10, "0d10".to_i(0)) + assert_equal(10, "0D10".to_i(0)) + assert_equal(8, "010".to_i(0)) + assert_raise(ArgumentError) { "010".to_i(-10) } 2.upto(36) {|radix| - assert_equal(radix, S("10").to_i(radix)) - assert_equal(radix**2, S("100").to_i(radix)) + assert_equal(radix, "10".to_i(radix)) + assert_equal(radix**2, "100".to_i(radix)) } - assert_raise(ArgumentError) { S("0").to_i(1) } - assert_raise(ArgumentError) { S("0").to_i(37) } - assert_equal(0, S("z").to_i(10)) - assert_equal(12, S("1_2").to_i(10)) - assert_equal(0x40000000, S("1073741824").to_i(10)) - assert_equal(0x4000000000000000, S("4611686018427387904").to_i(10)) - assert_equal(1, S("1__2").to_i(10)) - assert_equal(1, S("1_z").to_i(10)) + assert_raise(ArgumentError) { "0".to_i(1) } + assert_raise(ArgumentError) { "0".to_i(37) } + assert_equal(0, "z".to_i(10)) + assert_equal(12, "1_2".to_i(10)) + assert_equal(0x40000000, "1073741824".to_i(10)) + assert_equal(0x4000000000000000, "4611686018427387904".to_i(10)) + assert_equal(1, "1__2".to_i(10)) + assert_equal(1, "1_z".to_i(10)) bug6192 = '[ruby-core:43566]' - assert_raise(Encoding::CompatibilityError, bug6192) {S("0".encode("utf-16be")).to_i} - assert_raise(Encoding::CompatibilityError, bug6192) {S("0".encode("utf-16le")).to_i} - assert_raise(Encoding::CompatibilityError, bug6192) {S("0".encode("utf-32be")).to_i} - assert_raise(Encoding::CompatibilityError, bug6192) {S("0".encode("utf-32le")).to_i} - assert_raise(Encoding::CompatibilityError, bug6192) {S("0".encode("iso-2022-jp")).to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16be").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16le").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32be").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32le").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("iso-2022-jp").to_i} end def test_to_s @@ -2290,13 +2199,13 @@ CODE assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*"))) assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z"))) - a = S("abc".force_encoding(Encoding::US_ASCII)) + a = "abc".force_encoding(Encoding::US_ASCII) assert_equal(Encoding::US_ASCII, a.tr(S("z"), S("\u0101")).encoding, '[ruby-core:22326]') - assert_equal("a".hash, S("a").tr("a", "\u0101").tr("\u0101", "a").hash, '[ruby-core:22328]') - assert_equal(true, S("\u0101").tr("\u0101", "a").ascii_only?) - assert_equal(true, S("\u3041").tr("\u3041", "a").ascii_only?) - assert_equal(false, S("\u3041\u3042").tr("\u3041", "a").ascii_only?) + assert_equal("a".hash, "a".tr("a", "\u0101").tr("\u0101", "a").hash, '[ruby-core:22328]') + assert_equal(true, "\u0101".tr("\u0101", "a").ascii_only?) + assert_equal(true, "\u3041".tr("\u3041", "a").ascii_only?) + assert_equal(false, "\u3041\u3042".tr("\u3041", "a").ascii_only?) bug6156 = '[ruby-core:43335]' bug13950 = '[ruby-core:83056] [Bug #13950]' @@ -2306,8 +2215,6 @@ CODE assert_not_predicate(str, :ascii_only?) assert_not_predicate(star, :ascii_only?) assert_not_predicate(result, :ascii_only?, bug13950) - - assert_equal(S("XYC"), S("ABC").tr("A-AB", "XY")) end def test_tr! @@ -2329,20 +2236,16 @@ CODE assert_nil(a.tr!(S("B-Z"), S("A-Z"))) assert_equal(S("ibm"), a) - a = S("abc".force_encoding(Encoding::US_ASCII)) + a = "abc".force_encoding(Encoding::US_ASCII) assert_nil(a.tr!(S("z"), S("\u0101")), '[ruby-core:22326]') assert_equal(Encoding::US_ASCII, a.encoding, '[ruby-core:22326]') - - assert_equal(S("XYC"), S("ABC").tr!("A-AB", "XY")) end def test_tr_s assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp"))) assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*"))) - assert_equal("a".hash, S("\u0101\u0101").tr_s("\u0101", "a").hash) - assert_equal(true, S("\u3041\u3041").tr("\u3041", "a").ascii_only?) - - assert_equal(S("XYC"), S("ABC").tr_s("A-AB", "XY")) + assert_equal("a".hash, "\u0101\u0101".tr_s("\u0101", "a").hash) + assert_equal(true, "\u3041\u3041".tr("\u3041", "a").ascii_only?) end def test_tr_s! @@ -2355,8 +2258,6 @@ CODE a = S("hello") assert_equal(S("h*o"), a.tr_s!(S("el"), S("*"))) assert_equal(S("h*o"), a) - - assert_equal(S("XYC"), S("ABC").tr_s!("A-AB", "XY")) end def test_unpack @@ -2444,7 +2345,6 @@ CODE assert_equal(S("HELLO"), S("HELLO").upcase) assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase) assert_equal(S("H\0""ELLO"), S("H\0""ello").upcase) - assert_equal(S("\u{10574}"), S("\u{1059B}").upcase) end def test_upcase! @@ -2518,8 +2418,6 @@ CODE class S2 < String end def test_str_new4 - return unless @cls == String - s = (0..54).to_a.join # length = 100 s2 = S2.new(s[10,90]) s3 = s2[10,80] @@ -2528,7 +2426,7 @@ CODE end def test_rb_str_new4 - s = S("a" * 100) + s = "a" * 100 s2 = s[10,90] assert_equal("a" * 90, s2) s3 = s2[10,80] @@ -2546,11 +2444,11 @@ CODE end def test_rb_str_to_str - assert_equal("ab", S("a") + StringLike.new("b")) + assert_equal("ab", "a" + StringLike.new("b")) end def test_rb_str_shared_replace - s = S("a" * 100) + s = "a" * 100 s.succ! assert_equal("a" * 99 + "b", s) s = "" @@ -2574,12 +2472,12 @@ CODE def test_times2 s1 = '' 100.times {|n| - s2 = S("a") * n + s2 = "a" * n assert_equal(s1, s2) s1 << 'a' } - assert_raise(ArgumentError) { S("foo") * (-1) } + assert_raise(ArgumentError) { "foo" * (-1) } end def test_respond_to @@ -2587,41 +2485,41 @@ CODE def o.respond_to?(arg) [:to_str].include?(arg) ? nil : super end def o.to_str() "" end def o.==(other) "" == other end - assert_equal(false, S("") == o) + assert_equal(false, "" == o) end def test_match_method - assert_equal("bar", S("foobarbaz").match(/bar/).to_s) + assert_equal("bar", "foobarbaz".match(/bar/).to_s) o = Regexp.new('foo') def o.match(x, y, z); x + y + z; end - assert_equal("foobarbaz", S("foo").match(o, "bar", "baz")) + assert_equal("foobarbaz", "foo".match(o, "bar", "baz")) x = nil - S("foo").match(o, "bar", "baz") {|y| x = y } + "foo".match(o, "bar", "baz") {|y| x = y } assert_equal("foobarbaz", x) - assert_raise(ArgumentError) { S("foo").match } + assert_raise(ArgumentError) { "foo".match } end def test_match_p_regexp /backref/ =~ 'backref' # must match here, but not in a separate method, e.g., assert_send, # to check if $~ is affected or not. - assert_equal(true, S("").match?(//)) + assert_equal(true, "".match?(//)) assert_equal(true, :abc.match?(/.../)) - assert_equal(true, S('abc').match?(/b/)) - assert_equal(true, S('abc').match?(/b/, 1)) - assert_equal(true, S('abc').match?(/../, 1)) - assert_equal(true, S('abc').match?(/../, -2)) - assert_equal(false, S('abc').match?(/../, -4)) - assert_equal(false, S('abc').match?(/../, 4)) - assert_equal(true, S("\u3042xx").match?(/../, 1)) - assert_equal(false, S("\u3042x").match?(/../, 1)) - assert_equal(true, S('').match?(/\z/)) - assert_equal(true, S('abc').match?(/\z/)) - assert_equal(true, S('Ruby').match?(/R.../)) - assert_equal(false, S('Ruby').match?(/R.../, 1)) - assert_equal(false, S('Ruby').match?(/P.../)) + assert_equal(true, 'abc'.match?(/b/)) + assert_equal(true, 'abc'.match?(/b/, 1)) + assert_equal(true, 'abc'.match?(/../, 1)) + assert_equal(true, 'abc'.match?(/../, -2)) + assert_equal(false, 'abc'.match?(/../, -4)) + assert_equal(false, 'abc'.match?(/../, 4)) + assert_equal(true, "\u3042xx".match?(/../, 1)) + assert_equal(false, "\u3042x".match?(/../, 1)) + assert_equal(true, ''.match?(/\z/)) + assert_equal(true, 'abc'.match?(/\z/)) + assert_equal(true, 'Ruby'.match?(/R.../)) + assert_equal(false, 'Ruby'.match?(/R.../, 1)) + assert_equal(false, 'Ruby'.match?(/P.../)) assert_equal('backref', $&) end @@ -2629,21 +2527,21 @@ CODE /backref/ =~ 'backref' # must match here, but not in a separate method, e.g., assert_send, # to check if $~ is affected or not. - assert_equal(true, S("").match?('')) + assert_equal(true, "".match?('')) assert_equal(true, :abc.match?('...')) - assert_equal(true, S('abc').match?('b')) - assert_equal(true, S('abc').match?('b', 1)) - assert_equal(true, S('abc').match?('..', 1)) - assert_equal(true, S('abc').match?('..', -2)) - assert_equal(false, S('abc').match?('..', -4)) - assert_equal(false, S('abc').match?('..', 4)) - assert_equal(true, S("\u3042xx").match?('..', 1)) - assert_equal(false, S("\u3042x").match?('..', 1)) - assert_equal(true, S('').match?('\z')) - assert_equal(true, S('abc').match?('\z')) - assert_equal(true, S('Ruby').match?('R...')) - assert_equal(false, S('Ruby').match?('R...', 1)) - assert_equal(false, S('Ruby').match?('P...')) + assert_equal(true, 'abc'.match?('b')) + assert_equal(true, 'abc'.match?('b', 1)) + assert_equal(true, 'abc'.match?('..', 1)) + assert_equal(true, 'abc'.match?('..', -2)) + assert_equal(false, 'abc'.match?('..', -4)) + assert_equal(false, 'abc'.match?('..', 4)) + assert_equal(true, "\u3042xx".match?('..', 1)) + assert_equal(false, "\u3042x".match?('..', 1)) + assert_equal(true, ''.match?('\z')) + assert_equal(true, 'abc'.match?('\z')) + assert_equal(true, 'Ruby'.match?('R...')) + assert_equal(false, 'Ruby'.match?('R...', 1)) + assert_equal(false, 'Ruby'.match?('P...')) assert_equal('backref', $&) end @@ -2663,23 +2561,18 @@ CODE def test_inspect_nul bug8290 = '[ruby-core:54458]' - s = S("\0") + "12" + s = "\0" + "12" assert_equal '"\u000012"', s.inspect, bug8290 - s = S("\0".b) + "12" + s = "\0".b + "12" assert_equal '"\x0012"', s.inspect, bug8290 end - def test_inspect_next_line - bug16842 = '[ruby-core:98231]' - assert_equal '"\\u0085"', 0x85.chr(Encoding::UTF_8).inspect, bug16842 - end - def test_partition - assert_equal(%w(he l lo), S("hello").partition(/l/)) - assert_equal(%w(he l lo), S("hello").partition("l")) - assert_raise(TypeError) { S("hello").partition(1) } + assert_equal(%w(he l lo), "hello".partition(/l/)) + assert_equal(%w(he l lo), "hello".partition("l")) + assert_raise(TypeError) { "hello".partition(1) } def (hyphen = Object.new).to_str; "-"; end - assert_equal(%w(foo - bar), S("foo-bar").partition(hyphen), '[ruby-core:23540]') + assert_equal(%w(foo - bar), "foo-bar".partition(hyphen), '[ruby-core:23540]') bug6206 = '[ruby-dev:45441]' Encoding.list.each do |enc| @@ -2689,24 +2582,24 @@ CODE end assert_equal(["\u30E6\u30FC\u30B6", "@", "\u30C9\u30E1.\u30A4\u30F3"], - S("\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3").partition(/[@.]/)) + "\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3".partition(/[@.]/)) bug = '[ruby-core:82911]' - hello = S("hello") + hello = "hello" hello.partition("hi").map(&:upcase!) assert_equal("hello", hello, bug) - assert_equal(["", "", "foo"], S("foo").partition(/^=*/)) + assert_equal(["", "", "foo"], "foo".partition(/^=*/)) assert_equal([S("ab"), S("c"), S("dbce")], S("abcdbce").partition(/b\Kc/)) end def test_rpartition - assert_equal(%w(hel l o), S("hello").rpartition(/l/)) - assert_equal(%w(hel l o), S("hello").rpartition("l")) - assert_raise(TypeError) { S("hello").rpartition(1) } + assert_equal(%w(hel l o), "hello".rpartition(/l/)) + assert_equal(%w(hel l o), "hello".rpartition("l")) + assert_raise(TypeError) { "hello".rpartition(1) } def (hyphen = Object.new).to_str; "-"; end - assert_equal(%w(foo - bar), S("foo-bar").rpartition(hyphen), '[ruby-core:23540]') + assert_equal(%w(foo - bar), "foo-bar".rpartition(hyphen), '[ruby-core:23540]') bug6206 = '[ruby-dev:45441]' Encoding.list.each do |enc| @@ -2717,7 +2610,7 @@ CODE bug8138 = '[ruby-dev:47183]' assert_equal(["\u30E6\u30FC\u30B6@\u30C9\u30E1", ".", "\u30A4\u30F3"], - S("\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3").rpartition(/[@.]/), bug8138) + "\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3".rpartition(/[@.]/), bug8138) bug = '[ruby-core:82911]' hello = "hello" @@ -2727,13 +2620,10 @@ CODE assert_equal([S("abcdb"), S("c"), S("e")], S("abcdbce").rpartition(/b\Kc/)) end - def test_fs_setter - return unless @cls == String - + def test_setter assert_raise(TypeError) { $/ = 1 } name = "\u{5206 884c}" - assert_separately([], "#{<<~"do;"}\n#{<<~"end;"}") - do; + assert_separately([], <<-"end;") # do alias $#{name} $/ assert_raise_with_message(TypeError, /\\$#{name}/) { $#{name} = 1 } end; @@ -2766,19 +2656,16 @@ CODE end def test_gsub_enumerator - e = S("abc").gsub(/./) - assert_equal("a", e.next, "[ruby-dev:34828]") - assert_equal("b", e.next) - assert_equal("c", e.next) + assert_normal_exit %q{"abc".gsub(/./).next}, "[ruby-dev:34828]" end def test_clear_nonasciicompat - assert_equal("", S("\u3042".encode("ISO-2022-JP")).clear) + assert_equal("", "\u3042".encode("ISO-2022-JP").clear) end def test_try_convert - assert_equal(nil, @cls.try_convert(1)) - assert_equal("foo", @cls.try_convert("foo")) + assert_equal(nil, String.try_convert(1)) + assert_equal("foo", String.try_convert("foo")) end def test_substr_negative_begin @@ -2787,55 +2674,55 @@ CODE =begin def test_compare_different_encoding_string - s1 = S("\xff".force_encoding("UTF-8")) - s2 = S("\xff".force_encoding("ISO-2022-JP")) + s1 = "\xff".force_encoding("UTF-8") + s2 = "\xff".force_encoding("ISO-2022-JP") assert_equal([-1, 1], [s1 <=> s2, s2 <=> s1].sort) end =end def test_casecmp - assert_equal(0, S("FoO").casecmp("fOO")) - assert_equal(1, S("FoO").casecmp("BaR")) - assert_equal(-1, S("baR").casecmp("FoO")) - assert_equal(1, S("\u3042B").casecmp("\u3042a")) - assert_equal(-1, S("foo").casecmp("foo\0")) + assert_equal(0, "FoO".casecmp("fOO")) + assert_equal(1, "FoO".casecmp("BaR")) + assert_equal(-1, "baR".casecmp("FoO")) + assert_equal(1, "\u3042B".casecmp("\u3042a")) + assert_equal(-1, "foo".casecmp("foo\0")) - assert_nil(S("foo").casecmp(:foo)) - assert_nil(S("foo").casecmp(Object.new)) + assert_nil("foo".casecmp(:foo)) + assert_nil("foo".casecmp(Object.new)) o = Object.new def o.to_str; "fOO"; end - assert_equal(0, S("FoO").casecmp(o)) + assert_equal(0, "FoO".casecmp(o)) end def test_casecmp? - assert_equal(true, S('FoO').casecmp?('fOO')) - assert_equal(false, S('FoO').casecmp?('BaR')) - assert_equal(false, S('baR').casecmp?('FoO')) - assert_equal(true, S('äöü').casecmp?('ÄÖÜ')) - assert_equal(false, S("foo").casecmp?("foo\0")) + assert_equal(true, 'FoO'.casecmp?('fOO')) + assert_equal(false, 'FoO'.casecmp?('BaR')) + assert_equal(false, 'baR'.casecmp?('FoO')) + assert_equal(true, 'äöü'.casecmp?('ÄÖÜ')) + assert_equal(false, "foo".casecmp?("foo\0")) - assert_nil(S("foo").casecmp?(:foo)) - assert_nil(S("foo").casecmp?(Object.new)) + assert_nil("foo".casecmp?(:foo)) + assert_nil("foo".casecmp?(Object.new)) o = Object.new def o.to_str; "fOO"; end - assert_equal(true, S("FoO").casecmp?(o)) + assert_equal(true, "FoO".casecmp?(o)) end def test_upcase2 - assert_equal("\u3042AB", S("\u3042aB").upcase) + assert_equal("\u3042AB", "\u3042aB".upcase) end def test_downcase2 - assert_equal("\u3042ab", S("\u3042aB").downcase) + assert_equal("\u3042ab", "\u3042aB".downcase) end def test_rstrip - assert_equal(" hello", S(" hello ").rstrip) - assert_equal("\u3042", S("\u3042 ").rstrip) - assert_equal("\u3042", S("\u3042\u0000").rstrip) - assert_raise(Encoding::CompatibilityError) { S("\u3042".encode("ISO-2022-JP")).rstrip } + assert_equal(" hello", " hello ".rstrip) + assert_equal("\u3042", "\u3042 ".rstrip) + assert_equal("\u3042", "\u3042\u0000".rstrip) + assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip } end def test_rstrip_bang @@ -2859,18 +2746,13 @@ CODE assert_equal("\u3042", s5.rstrip!) assert_equal("\u3042", s5) - assert_raise(Encoding::CompatibilityError) { S("\u3042".encode("ISO-2022-JP")).rstrip! } - assert_raise(Encoding::CompatibilityError) { S("abc \x80 ".force_encoding('UTF-8')).rstrip! } - assert_raise(Encoding::CompatibilityError) { S("abc\x80 ".force_encoding('UTF-8')).rstrip! } - assert_raise(Encoding::CompatibilityError) { S("abc \x80".force_encoding('UTF-8')).rstrip! } - assert_raise(Encoding::CompatibilityError) { S("\x80".force_encoding('UTF-8')).rstrip! } - assert_raise(Encoding::CompatibilityError) { S(" \x80 ".force_encoding('UTF-8')).rstrip! } + assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip! } end def test_lstrip - assert_equal("hello ", S(" hello ").lstrip) - assert_equal("\u3042", S(" \u3042").lstrip) - assert_equal("hello ", S("\x00hello ").lstrip) + assert_equal("hello ", " hello ".lstrip) + assert_equal("\u3042", " \u3042".lstrip) + assert_equal("hello ", "\x00hello ".lstrip) end def test_lstrip_bang @@ -2896,13 +2778,11 @@ CODE end - def test_delete_prefix_type_error - assert_raise(TypeError) { S('hello').delete_prefix(nil) } - assert_raise(TypeError) { S('hello').delete_prefix(1) } - assert_raise(TypeError) { S('hello').delete_prefix(/hel/) } - end - def test_delete_prefix + assert_raise(TypeError) { 'hello'.delete_prefix(nil) } + assert_raise(TypeError) { 'hello'.delete_prefix(1) } + assert_raise(TypeError) { 'hello'.delete_prefix(/hel/) } + s = S("hello") assert_equal("lo", s.delete_prefix('hel')) assert_equal("hello", s) @@ -2922,9 +2802,8 @@ CODE s = S("hello") assert_equal("hello", s.delete_prefix("\u{3053 3093}")) assert_equal("hello", s) - end - def test_delete_prefix_broken_encoding + # skip if argument is a broken string s = S("\xe3\x81\x82") assert_equal("\xe3\x81\x82", s.delete_prefix("\xe3")) assert_equal("\xe3\x81\x82", s) @@ -2933,31 +2812,23 @@ CODE assert_equal("\x95\x5c".force_encoding("Shift_JIS"), s.delete_prefix("\x95")) assert_equal("\x95\x5c".force_encoding("Shift_JIS"), s) - assert_equal("\xFE", S("\xFF\xFE").delete_prefix("\xFF")) - assert_equal("\xBE", S("hello\xBE").delete_prefix("hello")) - assert_equal("\xBE", S("\xFFhello\xBE").delete_prefix("\xFFhello")) - end - - def test_delete_prefix_clear_coderange + # clear coderange s = S("\u{3053 3093}hello") assert_not_predicate(s, :ascii_only?) assert_predicate(s.delete_prefix("\u{3053 3093}"), :ascii_only?) - end - def test_delete_prefix_argument_conversion + # argument should be converted to String klass = Class.new { def to_str; 'a'; end } s = S("abba") assert_equal("bba", s.delete_prefix(klass.new)) assert_equal("abba", s) end - def test_delete_prefix_bang_type_error - assert_raise(TypeError) { S('hello').delete_prefix!(nil) } - assert_raise(TypeError) { S('hello').delete_prefix!(1) } - assert_raise(TypeError) { S('hello').delete_prefix!(/hel/) } - end - def test_delete_prefix_bang + assert_raise(TypeError) { 'hello'.delete_prefix!(nil) } + assert_raise(TypeError) { 'hello'.delete_prefix!(1) } + assert_raise(TypeError) { 'hello'.delete_prefix!(/hel/) } + s = S("hello") assert_equal("lo", s.delete_prefix!('hel')) assert_equal("lo", s) @@ -2977,32 +2848,23 @@ CODE s = S("hello") assert_equal(nil, s.delete_prefix!("\u{3053 3093}")) assert_equal("hello", s) - end - def test_delete_prefix_bang_broken_encoding + # skip if argument is a broken string s = S("\xe3\x81\x82") assert_equal(nil, s.delete_prefix!("\xe3")) assert_equal("\xe3\x81\x82", s) - s = S("\xFF\xFE") - assert_equal("\xFE", s.delete_prefix!("\xFF")) - assert_equal("\xFE", s) - end - - def test_delete_prefix_bang_clear_coderange + # clear coderange s = S("\u{3053 3093}hello") assert_not_predicate(s, :ascii_only?) assert_predicate(s.delete_prefix!("\u{3053 3093}"), :ascii_only?) - end - def test_delete_prefix_bang_argument_conversion + # argument should be converted to String klass = Class.new { def to_str; 'a'; end } s = S("abba") assert_equal("bba", s.delete_prefix!(klass.new)) assert_equal("bba", s) - end - def test_delete_prefix_bang_frozen_error s = S("ax").freeze assert_raise_with_message(FrozenError, /frozen/) {s.delete_prefix!("a")} @@ -3015,13 +2877,11 @@ CODE assert_raise_with_message(FrozenError, /frozen/) {s.delete_prefix!(o)} end - def test_delete_suffix_type_error - assert_raise(TypeError) { S('hello').delete_suffix(nil) } - assert_raise(TypeError) { S('hello').delete_suffix(1) } - assert_raise(TypeError) { S('hello').delete_suffix(/hel/) } - end - def test_delete_suffix + assert_raise(TypeError) { 'hello'.delete_suffix(nil) } + assert_raise(TypeError) { 'hello'.delete_suffix(1) } + assert_raise(TypeError) { 'hello'.delete_suffix(/hel/) } + s = S("hello") assert_equal("hel", s.delete_suffix('lo')) assert_equal("hello", s) @@ -3041,28 +2901,23 @@ CODE s = S("hello") assert_equal("hello", s.delete_suffix("\u{3061 306f}")) assert_equal("hello", s) - end - def test_delete_suffix_broken_encoding + # skip if argument is a broken string s = S("\xe3\x81\x82") assert_equal("\xe3\x81\x82", s.delete_suffix("\x82")) assert_equal("\xe3\x81\x82", s) - end - def test_delete_suffix_clear_coderange + # clear coderange s = S("hello\u{3053 3093}") assert_not_predicate(s, :ascii_only?) assert_predicate(s.delete_suffix("\u{3053 3093}"), :ascii_only?) - end - def test_delete_suffix_argument_conversion + # argument should be converted to String klass = Class.new { def to_str; 'a'; end } s = S("abba") assert_equal("abb", s.delete_suffix(klass.new)) assert_equal("abba", s) - end - def test_delete_suffix_newline # chomp removes any of "\n", "\r\n", "\r" when "\n" is specified, # but delete_suffix does not s = "foo\n" @@ -3073,13 +2928,11 @@ CODE assert_equal("foo\r", s.delete_suffix("\n")) end - def test_delete_suffix_bang_type_error - assert_raise(TypeError) { S('hello').delete_suffix!(nil) } - assert_raise(TypeError) { S('hello').delete_suffix!(1) } - assert_raise(TypeError) { S('hello').delete_suffix!(/hel/) } - end + def test_delete_suffix_bang + assert_raise(TypeError) { 'hello'.delete_suffix!(nil) } + assert_raise(TypeError) { 'hello'.delete_suffix!(1) } + assert_raise(TypeError) { 'hello'.delete_suffix!(/hel/) } - def test_delete_suffix_bang_frozen_error s = S("hello").freeze assert_raise_with_message(FrozenError, /frozen/) {s.delete_suffix!('lo')} @@ -3090,9 +2943,7 @@ CODE "x" end assert_raise_with_message(FrozenError, /frozen/) {s.delete_suffix!(o)} - end - def test_delete_suffix_bang s = S("hello") assert_equal("hel", s.delete_suffix!('lo')) assert_equal("hel", s) @@ -3112,9 +2963,8 @@ CODE s = S("hello") assert_equal(nil, s.delete_suffix!("\u{3061 306f}")) assert_equal("hello", s) - end - def test_delete_suffix_bang_broken_encoding + # skip if argument is a broken string s = S("\xe3\x81\x82") assert_equal(nil, s.delete_suffix!("\x82")) assert_equal("\xe3\x81\x82", s) @@ -3122,22 +2972,18 @@ CODE s = S("\x95\x5c").force_encoding("Shift_JIS") assert_equal(nil, s.delete_suffix!("\x5c")) assert_equal("\x95\x5c".force_encoding("Shift_JIS"), s) - end - def test_delete_suffix_bang_clear_coderange + # clear coderange s = S("hello\u{3053 3093}") assert_not_predicate(s, :ascii_only?) assert_predicate(s.delete_suffix!("\u{3053 3093}"), :ascii_only?) - end - def test_delete_suffix_bang_argument_conversion + # argument should be converted to String klass = Class.new { def to_str; 'a'; end } s = S("abba") assert_equal("abb", s.delete_suffix!(klass.new)) assert_equal("abb", s) - end - def test_delete_suffix_bang_newline # chomp removes any of "\n", "\r\n", "\r" when "\n" is specified, # but delete_suffix does not s = "foo\n" @@ -3174,7 +3020,7 @@ CODE end def test_shared_force_encoding - s = S("\u{3066}\u{3059}\u{3068}").gsub(//, '') + s = "\u{3066}\u{3059}\u{3068}".gsub(//, '') h = {} h[s] = nil k = h.keys[0] @@ -3189,16 +3035,16 @@ CODE def test_ascii_incomat_inspect bug4081 = '[ruby-core:33283]' WIDE_ENCODINGS.each do |e| - assert_equal('"abc"', S("abc".encode(e)).inspect) - assert_equal('"\\u3042\\u3044\\u3046"', S("\u3042\u3044\u3046".encode(e)).inspect) - assert_equal('"ab\\"c"', S("ab\"c".encode(e)).inspect, bug4081) + assert_equal('"abc"', "abc".encode(e).inspect) + assert_equal('"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).inspect) + assert_equal('"ab\\"c"', "ab\"c".encode(e).inspect, bug4081) end begin verbose, $VERBOSE = $VERBOSE, nil ext = Encoding.default_external Encoding.default_external = "us-ascii" $VERBOSE = verbose - i = S("abc\"\\".force_encoding("utf-8")).inspect + i = "abc\"\\".force_encoding("utf-8").inspect ensure $VERBOSE = nil Encoding.default_external = ext @@ -3209,11 +3055,11 @@ CODE def test_dummy_inspect assert_equal('"\e\x24\x42\x22\x4C\x22\x68\e\x28\x42"', - S("\u{ffe2}\u{2235}".encode("cp50220")).inspect) + "\u{ffe2}\u{2235}".encode("cp50220").inspect) end def test_prepend - assert_equal(S("hello world!"), S("!").prepend("hello ", "world")) + assert_equal(S("hello world!"), "!".prepend("hello ", "world")) b = S("ue") assert_equal(S("ueueue"), b.prepend(b, b)) @@ -3221,7 +3067,7 @@ CODE def foo.to_str "b" end - assert_equal(S("ba"), S("a").prepend(foo)) + assert_equal(S("ba"), "a".prepend(foo)) a = S("world") b = S("hello ") @@ -3235,34 +3081,34 @@ CODE end def test_byteslice - assert_equal("h", S("hello").byteslice(0)) - assert_equal(nil, S("hello").byteslice(5)) - assert_equal("o", S("hello").byteslice(-1)) - assert_equal(nil, S("hello").byteslice(-6)) - - assert_equal("", S("hello").byteslice(0, 0)) - assert_equal("hello", S("hello").byteslice(0, 6)) - assert_equal("hello", S("hello").byteslice(0, 6)) - assert_equal("", S("hello").byteslice(5, 1)) - assert_equal("o", S("hello").byteslice(-1, 6)) - assert_equal(nil, S("hello").byteslice(-6, 1)) - assert_equal(nil, S("hello").byteslice(0, -1)) - - assert_equal("h", S("hello").byteslice(0..0)) - assert_equal("", S("hello").byteslice(5..0)) - assert_equal("o", S("hello").byteslice(4..5)) - assert_equal(nil, S("hello").byteslice(6..0)) - assert_equal("", S("hello").byteslice(-1..0)) - assert_equal("llo", S("hello").byteslice(-3..5)) - - assert_equal(u("\x81"), S("\u3042").byteslice(1)) - assert_equal(u("\x81\x82"), S("\u3042").byteslice(1, 2)) - assert_equal(u("\x81\x82"), S("\u3042").byteslice(1..2)) - - assert_equal(u("\x82")+("\u3042"*9), S("\u3042"*10).byteslice(2, 28)) + assert_equal("h", "hello".byteslice(0)) + assert_equal(nil, "hello".byteslice(5)) + assert_equal("o", "hello".byteslice(-1)) + assert_equal(nil, "hello".byteslice(-6)) + + assert_equal("", "hello".byteslice(0, 0)) + assert_equal("hello", "hello".byteslice(0, 6)) + assert_equal("hello", "hello".byteslice(0, 6)) + assert_equal("", "hello".byteslice(5, 1)) + assert_equal("o", "hello".byteslice(-1, 6)) + assert_equal(nil, "hello".byteslice(-6, 1)) + assert_equal(nil, "hello".byteslice(0, -1)) + + assert_equal("h", "hello".byteslice(0..0)) + assert_equal("", "hello".byteslice(5..0)) + assert_equal("o", "hello".byteslice(4..5)) + assert_equal(nil, "hello".byteslice(6..0)) + assert_equal("", "hello".byteslice(-1..0)) + assert_equal("llo", "hello".byteslice(-3..5)) + + assert_equal(u("\x81"), "\u3042".byteslice(1)) + assert_equal(u("\x81\x82"), "\u3042".byteslice(1, 2)) + assert_equal(u("\x81\x82"), "\u3042".byteslice(1..2)) + + assert_equal(u("\x82")+("\u3042"*9), ("\u3042"*10).byteslice(2, 28)) bug7954 = '[ruby-dev:47108]' - assert_equal(false, S("\u3042").byteslice(0, 2).valid_encoding?, bug7954) + assert_equal(false, "\u3042".byteslice(0, 2).valid_encoding?, bug7954) assert_equal(false, ("\u3042"*10).byteslice(0, 20).valid_encoding?, bug7954) end @@ -3277,8 +3123,6 @@ CODE end def test_eq_tilde_can_be_overridden - return unless @cls == String - assert_separately([], <<-RUBY) class String undef =~ @@ -3296,7 +3140,7 @@ CODE end def test_regexp_match_subclass - s = Bug9581.new(S("abc")) + s = Bug9581.new("abc") r = /abc/ assert_equal(:foo, s =~ r) assert_equal(:foo, s.send(:=~, r)) @@ -3306,7 +3150,6 @@ CODE def test_LSHIFT_neary_long_max return unless @cls == String - assert_ruby_status([], <<-'end;', '[ruby-core:61886] [Bug #9709]', timeout: 20) begin a = "a" * 0x4000_0000 @@ -3318,8 +3161,6 @@ CODE # enable only when string size range is smaller than memory space def test_uplus_minus - return unless @cls == String - str = "foo" assert_not_predicate(str, :frozen?) assert_not_predicate(+str, :frozen?) @@ -3341,8 +3182,6 @@ CODE end def test_uminus_frozen - return unless @cls == String - # embedded str1 = ("foobar" * 3).freeze str2 = ("foobar" * 3).freeze @@ -3359,274 +3198,45 @@ CODE end def test_uminus_no_freeze_not_bare - str = S("foo") + str = @cls.new("foo") assert_instance_of(@cls, -str) assert_equal(false, str.frozen?) - str = S("foo") + str = @cls.new("foo") str.instance_variable_set(:@iv, 1) assert_instance_of(@cls, -str) assert_equal(false, str.frozen?) assert_equal(1, str.instance_variable_get(:@iv)) - str = S("foo") + str = @cls.new("foo") assert_instance_of(@cls, -str) assert_equal(false, str.frozen?) end def test_ord - assert_equal(97, S("a").ord) - assert_equal(97, S("abc").ord) - assert_equal(0x3042, S("\u3042\u3043").ord) - assert_raise(ArgumentError) { S("").ord } + assert_equal(97, "a".ord) + assert_equal(97, "abc".ord) + assert_equal(0x3042, "\u3042\u3043".ord) + assert_raise(ArgumentError) { "".ord } end def test_chr - assert_equal("a", S("abcde").chr) - assert_equal("a", S("a").chr) - assert_equal("\u3042", S("\u3042\u3043").chr) - assert_equal('', S('').chr) + assert_equal("a", "abcde".chr) + assert_equal("a", "a".chr) + assert_equal("\u3042", "\u3042\u3043".chr) + assert_equal('', ''.chr) end def test_substr_code_range - data = S("\xff" + "a"*200) + data = "\xff" + "a"*200 assert_not_predicate(data, :valid_encoding?) assert_predicate(data[100..-1], :valid_encoding?) end - def test_byteindex - assert_byteindex(0, S("hello"), ?h) - assert_byteindex(1, S("hello"), S("ell")) - assert_byteindex(2, S("hello"), /ll./) - - assert_byteindex(3, S("hello"), ?l, 3) - assert_byteindex(3, S("hello"), S("l"), 3) - assert_byteindex(3, S("hello"), /l./, 3) - - assert_byteindex(nil, S("hello"), ?z, 3) - assert_byteindex(nil, S("hello"), S("z"), 3) - assert_byteindex(nil, S("hello"), /z./, 3) - - assert_byteindex(nil, S("hello"), ?z) - assert_byteindex(nil, S("hello"), S("z")) - assert_byteindex(nil, S("hello"), /z./) - - assert_byteindex(0, S(""), S("")) - assert_byteindex(0, S(""), //) - assert_byteindex(nil, S(""), S("hello")) - assert_byteindex(nil, S(""), /hello/) - assert_byteindex(0, S("hello"), S("")) - assert_byteindex(0, S("hello"), //) - - s = S("long") * 1000 << "x" - assert_byteindex(nil, s, S("y")) - assert_byteindex(4 * 1000, s, S("x")) - s << "yx" - assert_byteindex(4 * 1000, s, S("x")) - assert_byteindex(4 * 1000, s, S("xyx")) - - o = Object.new - def o.to_str; "bar"; end - assert_byteindex(3, S("foobarbarbaz"), o) - assert_raise(TypeError) { S("foo").byteindex(Object.new) } - - assert_byteindex(nil, S("foo"), //, -100) - assert_byteindex(nil, S("foo"), //, -4) - - assert_byteindex(2, S("abcdbce"), /b\Kc/) - - assert_byteindex(0, S("こんにちは"), ?こ) - assert_byteindex(3, S("こんにちは"), S("んにち")) - assert_byteindex(6, S("こんにちは"), /にち./) - - assert_byteindex(0, S("にんにちは"), ?に, 0) - assert_raise(IndexError) { S("にんにちは").byteindex(?に, 1) } - assert_raise(IndexError) { S("にんにちは").byteindex(?に, 5) } - assert_byteindex(6, S("にんにちは"), ?に, 6) - assert_byteindex(6, S("にんにちは"), S("に"), 6) - assert_byteindex(6, S("にんにちは"), /に./, 6) - assert_raise(IndexError) { S("にんにちは").byteindex(?に, 7) } - - s = S("foobarbarbaz") - assert !1000.times.any? {s.byteindex("", 100_000_000)} - end - - def test_byterindex - assert_byterindex(3, S("hello"), ?l) - assert_byterindex(6, S("ell, hello"), S("ell")) - assert_byterindex(7, S("ell, hello"), /ll./) - - assert_byterindex(3, S("hello,lo"), ?l, 3) - assert_byterindex(3, S("hello,lo"), S("l"), 3) - assert_byterindex(3, S("hello,lo"), /l./, 3) - - assert_byterindex(nil, S("hello"), ?z, 3) - assert_byterindex(nil, S("hello"), S("z"), 3) - assert_byterindex(nil, S("hello"), /z./, 3) - - assert_byterindex(nil, S("hello"), ?z) - assert_byterindex(nil, S("hello"), S("z")) - assert_byterindex(nil, S("hello"), /z./) - - assert_byterindex(5, S("hello"), S("")) - assert_byterindex(5, S("hello"), S(""), 5) - assert_byterindex(4, S("hello"), S(""), 4) - assert_byterindex(0, S("hello"), S(""), 0) - - o = Object.new - def o.to_str; "bar"; end - assert_byterindex(6, S("foobarbarbaz"), o) - assert_raise(TypeError) { S("foo").byterindex(Object.new) } - - assert_byterindex(nil, S("foo"), //, -100) - - m = assert_byterindex(3, S("foo"), //) - assert_equal([3, 3], m.offset(0)) - assert_byterindex(3, S("foo"), //, 4) - - assert_byterindex(5, S("abcdbce"), /b\Kc/) - - assert_byterindex(6, S("こんにちは"), ?に) - assert_byterindex(18, S("にちは、こんにちは"), S("にちは")) - assert_byterindex(18, S("にちは、こんにちは"), /にち./) - - assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 19) } - assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), -2) } - assert_byterindex(18, S("にちは、こんにちは"), S("にちは"), 18) - assert_byterindex(18, S("にちは、こんにちは"), S("にちは"), -3) - assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 17) } - assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), -4) } - assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 1) } - assert_byterindex(0, S("にちは、こんにちは"), S("にちは"), 0) - - assert_byterindex(0, S("こんにちは"), S("こんにちは")) - assert_byterindex(nil, S("こんにち"), S("こんにちは")) - assert_byterindex(nil, S("こ"), S("こんにちは")) - assert_byterindex(nil, S(""), S("こんにちは")) - end - - def test_bytesplice - assert_bytesplice_raise(IndexError, S("hello"), -6, 0, "bye") - assert_bytesplice_result("byehello", S("hello"), -5, 0, "bye") - assert_bytesplice_result("byehello", S("hello"), 0, 0, "bye") - assert_bytesplice_result("byeello", S("hello"), 0, 1, "bye") - assert_bytesplice_result("bye", S("hello"), 0, 5, "bye") - assert_bytesplice_result("bye", S("hello"), 0, 6, "bye") - - assert_bytesplice_raise(IndexError, S("hello"), -5, 0, "bye", -4, 0) - assert_bytesplice_result("byehello", S("hello"), 0, 0, "bye", 0, 3) - assert_bytesplice_result("yehello", S("hello"), 0, 0, "bye", 1, 3) - assert_bytesplice_result("yehello", S("hello"), 0, 0, "bye", 1, 2) - assert_bytesplice_result("ehello", S("hello"), 0, 0, "bye", 2, 1) - assert_bytesplice_result("hello", S("hello"), 0, 0, "bye", 3, 0) - assert_bytesplice_result("hello", s = S("hello"), 0, 5, s, 0, 5) - assert_bytesplice_result("elloo", s = S("hello"), 0, 4, s, 1, 4) - assert_bytesplice_result("llolo", s = S("hello"), 0, 3, s, 2, 3) - assert_bytesplice_result("lollo", s = S("hello"), 0, 2, s, 3, 2) - assert_bytesplice_result("oello", s = S("hello"), 0, 1, s, 4, 1) - assert_bytesplice_result("hhell", s = S("hello"), 1, 4, s, 0, 4) - assert_bytesplice_result("hehel", s = S("hello"), 2, 3, s, 0, 3) - assert_bytesplice_result("helhe", s = S("hello"), 3, 2, s, 0, 2) - assert_bytesplice_result("hellh", s = S("hello"), 4, 1, s, 0, 1) - - assert_bytesplice_raise(RangeError, S("hello"), -6...-6, "bye") - assert_bytesplice_result("byehello", S("hello"), -5...-5, "bye") - assert_bytesplice_result("byehello", S("hello"), 0...0, "bye") - assert_bytesplice_result("byeello", S("hello"), 0..0, "bye") - assert_bytesplice_result("byeello", S("hello"), 0...1, "bye") - assert_bytesplice_result("byello", S("hello"), 0..1, "bye") - assert_bytesplice_result("bye", S("hello"), 0..-1, "bye") - assert_bytesplice_result("bye", S("hello"), 0...5, "bye") - assert_bytesplice_result("bye", S("hello"), 0...6, "bye") - assert_bytesplice_result("llolo", s = S("hello"), 0..2, s, 2..4) - - assert_bytesplice_raise(RangeError, S("hello"), -5...-5, "bye", -6...-6) - assert_bytesplice_result("byehello", S("hello"), -5...-5, "bye", 0..-1) - assert_bytesplice_result("byehello", S("hello"), 0...0, "bye", 0..-1) - assert_bytesplice_result("bhello", S("hello"), 0...0, "bye", 0..0) - assert_bytesplice_result("byhello", S("hello"), 0...0, "bye", 0..1) - assert_bytesplice_result("byehello", S("hello"), 0...0, "bye", 0..2) - assert_bytesplice_result("yehello", S("hello"), 0...0, "bye", 1..2) - - assert_bytesplice_raise(TypeError, S("hello"), 0, "bye") - - assert_bytesplice_raise(IndexError, S("こんにちは"), -16, 0, "bye") - assert_bytesplice_result("byeこんにちは", S("こんにちは"), -15, 0, "bye") - assert_bytesplice_result("byeこんにちは", S("こんにちは"), 0, 0, "bye") - assert_bytesplice_raise(IndexError, S("こんにちは"), 1, 0, "bye") - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 1, "bye") - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 2, "bye") - assert_bytesplice_result("byeんにちは", S("こんにちは"), 0, 3, "bye") - assert_bytesplice_result("こんにちはbye", S("こんにちは"), 15, 0, "bye") - - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 0, "さようなら", -16, 0) - assert_bytesplice_result("こんにちはさようなら", S("こんにちは"), 15, 0, "さようなら", 0, 15) - assert_bytesplice_result("さようなら", S("こんにちは"), 0, 15, "さようなら", 0, 15) - assert_bytesplice_result("さんにちは", S("こんにちは"), 0, 3, "さようなら", 0, 3) - assert_bytesplice_result("さようちは", S("こんにちは"), 0, 9, "さようなら", 0, 9) - assert_bytesplice_result("ようなちは", S("こんにちは"), 0, 9, "さようなら", 3, 9) - assert_bytesplice_result("ようちは", S("こんにちは"), 0, 9, "さようなら", 3, 6) - assert_bytesplice_result("ようならちは", S("こんにちは"), 0, 9, "さようなら", 3, 12) - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", -16, 0) - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 1, 0) - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 2, 0) - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 0, 1) - assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 0, 2) - assert_bytesplice_result("にちはちは", s = S("こんにちは"), 0, 9, s, 6, 9) - - assert_bytesplice_result("", S(""), 0, 0, "") - assert_bytesplice_result("xxx", S(""), 0, 0, "xxx") - - assert_bytesplice_raise(ArgumentError, S("hello"), 0, 5, "bye", 0) - assert_bytesplice_raise(ArgumentError, S("hello"), 0, 5, "bye", 0..-1) - assert_bytesplice_raise(ArgumentError, S("hello"), 0..-1, "bye", 0, 3) - end - - private - - def assert_bytesplice_result(expected, s, *args) - assert_equal(expected, s.send(:bytesplice, *args)) - assert_equal(expected, s) - end - - def assert_bytesplice_raise(e, s, *args) - assert_raise(e) { s.send(:bytesplice, *args) } - end - - def assert_index_like(method, expected, string, match, *rest) - message = "#{method} with string does not affect $~" - /.*/ =~ message - md_before = $~ - assert_equal(expected, string.__send__(method, match, *rest)) - md_after = $~ - case match - when Regexp - if expected - assert_not_nil(md_after) - assert_not_same(md_before, md_after) - else - assert_nil(md_after) - end - else - assert_same(md_before, md_after) - end - md_after - end - - def assert_index(expected, string, match, *rest) - assert_index_like(:index, expected, string, match, *rest) - end - - def assert_rindex(expected, string, match, *rest) - assert_index_like(:rindex, expected, string, match, *rest) - end - - def assert_byteindex(expected, string, match, *rest) - assert_index_like(:byteindex, expected, string, match, *rest) - end - - def assert_byterindex(expected, string, match, *rest) - assert_index_like(:byterindex, expected, string, match, *rest) + def test_slice_bang_code_range + str = "[Bug #19739] ABC OÜ" + str.slice!(/ oü$/i) + assert_predicate str, :ascii_only? end end |
