From 65c2351739386644b88558344e8086dbc118b5c9 Mon Sep 17 00:00:00 2001 From: sonots Date: Wed, 21 Jun 2017 07:43:27 +0000 Subject: test/ruby/test_string.rb: add tests to chomp substr * test/ruby/test_string.rb (TestString#test_chomp): add tests to chomp substr * test/ruby/test_string.rb (TestString#test_chomp!): ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_string.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index a12af416ab..c3fd2b7d94 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -383,6 +383,42 @@ CODE $/ = save assert_equal(S("a").hash, S("a\u0101").chomp(S("\u0101")).hash, '[ruby-core:22414]') + + s = S("hello") + assert_equal("hel", s.chomp('lo')) + assert_equal("hello", s) + + s = S("hello") + assert_equal("hello", s.chomp('he')) + assert_equal("hello", s) + + s = S("\u{3053 3093 306b 3061 306f}") + assert_equal("\u{3053 3093 306b}", s.chomp("\u{3061 306f}")) + assert_equal("\u{3053 3093 306b 3061 306f}", s) + + s = S("\u{3053 3093 306b 3061 306f}") + assert_equal("\u{3053 3093 306b 3061 306f}", s.chomp('lo')) + assert_equal("\u{3053 3093 306b 3061 306f}", s) + + s = S("hello") + assert_equal("hello", s.chomp("\u{3061 306f}")) + assert_equal("hello", s) + + # skip if argument is a broken string + s = S("\xe3\x81\x82") + assert_equal("\xe3\x81\x82", s.chomp("\x82")) + assert_equal("\xe3\x81\x82", s) + + # clear coderange + s = S("hello\u{3053 3093}") + assert_not_predicate(s, :ascii_only?) + assert_predicate(s.chomp("\u{3053 3093}"), :ascii_only?) + + # argument should be converted to String + klass = Class.new {|klass| def to_str; 'a'; end } + s = S("abba") + assert_equal("abb", s.chomp(klass.new)) + assert_equal("abba", s) ensure $/ = save end @@ -452,6 +488,42 @@ CODE "x" end assert_raise_with_message(RuntimeError, /frozen/) {s.chomp!(o)} + + s = S("hello") + assert_equal("hel", s.chomp!('lo')) + assert_equal("hel", s) + + s = S("hello") + assert_equal(nil, s.chomp!('he')) + assert_equal("hello", s) + + s = S("\u{3053 3093 306b 3061 306f}") + assert_equal("\u{3053 3093 306b}", s.chomp!("\u{3061 306f}")) + assert_equal("\u{3053 3093 306b}", s) + + s = S("\u{3053 3093 306b 3061 306f}") + assert_equal(nil, s.chomp!('lo')) + assert_equal("\u{3053 3093 306b 3061 306f}", s) + + s = S("hello") + assert_equal(nil, s.chomp!("\u{3061 306f}")) + assert_equal("hello", s) + + # skip if argument is a broken string + s = S("\xe3\x81\x82") + assert_equal(nil, s.chomp!("\x82")) + assert_equal("\xe3\x81\x82", s) + + # clear coderange + s = S("hello\u{3053 3093}") + assert_not_predicate(s, :ascii_only?) + assert_predicate(s.chomp!("\u{3053 3093}"), :ascii_only?) + + # argument should be converted to String + klass = Class.new {|klass| def to_str; 'a'; end } + s = S("abba") + assert_equal("abb", s.chomp!(klass.new)) + assert_equal("abb", s) ensure $/ = save end -- cgit v1.2.3