diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-10-24 11:52:30 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-11-20 16:30:23 -0800 |
commit | 58325daae3beefda13ed100782cd19a89cc68771 (patch) | |
tree | fd1857904c1c86d9c7c21f6038535f3beeafc4bb /test | |
parent | 4f5d14eb8cb7fba1c583d516d597c5e28d7a5540 (diff) | |
download | ruby-58325daae3beefda13ed100782cd19a89cc68771.tar.gz |
Make String methods return String instances when called on a subclass instance
This modifies the following String methods to return String instances
instead of subclass instances:
* String#*
* String#capitalize
* String#center
* String#chomp
* String#chop
* String#delete
* String#delete_prefix
* String#delete_suffix
* String#downcase
* String#dump
* String#each/#each_line
* String#gsub
* String#ljust
* String#lstrip
* String#partition
* String#reverse
* String#rjust
* String#rpartition
* String#rstrip
* String#scrub
* String#slice!
* String#slice/#[]
* String#split
* String#squeeze
* String#strip
* String#sub
* String#succ/#next
* String#swapcase
* String#tr
* String#tr_s
* String#upcase
This also fixes a bug in String#swapcase where it would return the
receiver instead of a copy of the receiver if the receiver was the
empty string.
Some string methods were left to return subclass instances:
* String#+@
* String#-@
Both of these methods will return the receiver (subclass instance)
in some cases, so it is best to keep the returned class consistent.
Fixes [#10845]
Diffstat (limited to 'test')
-rw-r--r-- | test/-ext-/string/test_cstr.rb | 8 | ||||
-rw-r--r-- | test/-ext-/string/test_ellipsize.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 2 |
3 files changed, 7 insertions, 5 deletions
diff --git a/test/-ext-/string/test_cstr.rb b/test/-ext-/string/test_cstr.rb index 2083016db2..d909781700 100644 --- a/test/-ext-/string/test_cstr.rb +++ b/test/-ext-/string/test_cstr.rb @@ -13,13 +13,13 @@ class Test_StringCStr < Test::Unit::TestCase end def test_long - s = Bug::String.new("abcdef")*100000 + s = Bug::String.new(Bug::String.new("abcdef")*100000) s.cstr_unterm('x') assert_equal(0, s.cstr_term, Bug4319) end def test_shared - s = Bug::String.new("abcdef")*5 + s = Bug::String.new(Bug::String.new("abcdef")*5) s = s.unterminated_substring(0, 29) assert_equal(0, s.cstr_term, Bug4319) end @@ -28,7 +28,7 @@ class Test_StringCStr < Test::Unit::TestCase s0 = Bug::String.new("abcdefgh"*8) [4, 4*3-1, 8*3-1, 64].each do |n| - s = s0[0, n] + s = Bug::String.new(s0[0, n]) s.cstr_unterm('x') s.freeze assert_equal(0, s.cstr_term) @@ -67,7 +67,7 @@ class Test_StringCStr < Test::Unit::TestCase n = 100 len = str.size * n WCHARS.each do |enc| - s = Bug::String.new(str.encode(enc))*n + s = Bug::String.new(Bug::String.new(str.encode(enc))*n) s.cstr_unterm('x') assert_nothing_raised(ArgumentError, enc.name) {s.cstr_term} s.set_len(s.bytesize / 2) diff --git a/test/-ext-/string/test_ellipsize.rb b/test/-ext-/string/test_ellipsize.rb index d7947041d5..d340abd58a 100644 --- a/test/-ext-/string/test_ellipsize.rb +++ b/test/-ext-/string/test_ellipsize.rb @@ -10,7 +10,7 @@ class Test_StringEllipsize < Test::Unit::TestCase def assert_equal_with_class(expected, result, *rest) assert_equal(expected.encoding, result.encoding, *rest) assert_equal(expected, result, result.encoding.name) - assert_instance_of(Bug::String, result, *rest) + assert_instance_of(String, result, *rest) end def test_longer diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 810c700c8c..4814872789 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -2089,6 +2089,8 @@ CODE def test_swapcase assert_equal(S("hi&LOW"), S("HI&low").swapcase) + s = S("") + assert_not_same(s, s.swapcase) end def test_swapcase! |