diff options
author | Benoit Daloze <eregontp@gmail.com> | 2022-06-26 14:50:14 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2022-06-26 14:50:14 +0200 |
commit | d3d5ef0cca160fca538c7f556c5a6e08df5847e6 (patch) | |
tree | 57358b4b9cdd6f429d0383005ac393cb74dd3bff /spec/ruby/core/string | |
parent | f616e816372d14e605879d2e43c7fbdda29ef837 (diff) |
Update to ruby/spec@ab32a1a
Diffstat (limited to 'spec/ruby/core/string')
-rw-r--r-- | spec/ruby/core/string/capitalize_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/string/dup_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/string/insert_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/string/lstrip_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/string/rstrip_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/string/scrub_spec.rb | 5 | ||||
-rw-r--r-- | spec/ruby/core/string/shared/slice.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/string/split_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/core/string/unpack/z_spec.rb | 5 |
9 files changed, 71 insertions, 6 deletions
diff --git a/spec/ruby/core/string/capitalize_spec.rb b/spec/ruby/core/string/capitalize_spec.rb index ca8df88fec..8afaefc021 100644 --- a/spec/ruby/core/string/capitalize_spec.rb +++ b/spec/ruby/core/string/capitalize_spec.rb @@ -35,6 +35,10 @@ describe "String#capitalize" do it "does not capitalize non-ASCII characters" do "ßet".capitalize(:ascii).should == "ßet" end + + it "handles non-ASCII substrings properly" do + "garçon"[1..-1].capitalize(:ascii).should == "Arçon" + end end describe "full Unicode case mapping adapted for Turkic languages" do diff --git a/spec/ruby/core/string/dup_spec.rb b/spec/ruby/core/string/dup_spec.rb index d650788210..eec3cf0a70 100644 --- a/spec/ruby/core/string/dup_spec.rb +++ b/spec/ruby/core/string/dup_spec.rb @@ -49,4 +49,13 @@ describe "String#dup" do orig.should == "xtring" dup.should == "string" end + + it "does not modify the original setbyte-mutated string when changing dupped string" do + orig = "a" + orig.setbyte 0, "b".ord + copy = orig.dup + orig.setbyte 0, "c".ord + orig.should == "c" + copy.should == "b" + end end diff --git a/spec/ruby/core/string/insert_spec.rb b/spec/ruby/core/string/insert_spec.rb index db42a37941..0c87df3a95 100644 --- a/spec/ruby/core/string/insert_spec.rb +++ b/spec/ruby/core/string/insert_spec.rb @@ -69,4 +69,13 @@ describe "String#insert with index, other" do "あれ".insert 0, pat end.should raise_error(Encoding::CompatibilityError) end + + it "should not call subclassed string methods" do + cls = Class.new(String) do + def replace(arg) + raise "should not call replace" + end + end + cls.new("abcd").insert(0, 'X').should == "Xabcd" + end end diff --git a/spec/ruby/core/string/lstrip_spec.rb b/spec/ruby/core/string/lstrip_spec.rb index 6e868eac4f..02bc6b4322 100644 --- a/spec/ruby/core/string/lstrip_spec.rb +++ b/spec/ruby/core/string/lstrip_spec.rb @@ -50,4 +50,10 @@ describe "String#lstrip!" do -> { "hello".freeze.lstrip! }.should raise_error(FrozenError) -> { "".freeze.lstrip! }.should raise_error(FrozenError) end + + it "raises an ArgumentError if the first codepoint is invalid" do + s = "\xDFabc".force_encoding(Encoding::UTF_8) + s.valid_encoding?.should be_false + -> { s.lstrip! }.should raise_error(ArgumentError) + end end diff --git a/spec/ruby/core/string/rstrip_spec.rb b/spec/ruby/core/string/rstrip_spec.rb index 2dc55182ae..dc34b12719 100644 --- a/spec/ruby/core/string/rstrip_spec.rb +++ b/spec/ruby/core/string/rstrip_spec.rb @@ -46,4 +46,10 @@ describe "String#rstrip!" do -> { "hello".freeze.rstrip! }.should raise_error(FrozenError) -> { "".freeze.rstrip! }.should raise_error(FrozenError) end + + it "raises an ArgumentError if the last codepoint is invalid" do + s = "abc\xDF".force_encoding(Encoding::UTF_8) + s.valid_encoding?.should be_false + -> { s.rstrip! }.should raise_error(ArgumentError) + end end diff --git a/spec/ruby/core/string/scrub_spec.rb b/spec/ruby/core/string/scrub_spec.rb index 3137399291..66755bcc7b 100644 --- a/spec/ruby/core/string/scrub_spec.rb +++ b/spec/ruby/core/string/scrub_spec.rb @@ -14,6 +14,11 @@ describe "String#scrub with a default replacement" do "abc\u3042#{x81}".scrub.should == "abc\u3042\uFFFD" end + it "replaces invalid byte sequences in lazy substrings" do + x81 = [0x81].pack('C').force_encoding('utf-8') + "abc\u3042#{x81}def"[1...-1].scrub.should == "bc\u3042\uFFFDde" + end + it "returns a copy of self when the input encoding is BINARY" do input = "foo".encode('BINARY') diff --git a/spec/ruby/core/string/shared/slice.rb b/spec/ruby/core/string/shared/slice.rb index 228af5f824..713234fffd 100644 --- a/spec/ruby/core/string/shared/slice.rb +++ b/spec/ruby/core/string/shared/slice.rb @@ -375,10 +375,20 @@ describe :string_slice_regexp_index, shared: true do "hello there".send(@method, /(what?)/, 1).should == nil end + it "returns nil if the index is larger than the number of captures" do + "hello there".send(@method, /hello (.)/, 2).should == nil + # You can't refer to 0 using negative indices + "hello there".send(@method, /hello (.)/, -2).should == nil + end + it "returns nil if there is no capture for the given index" do "hello there".send(@method, /[aeiou](.)\1/, 2).should == nil - # You can't refer to 0 using negative indices - "hello there".send(@method, /[aeiou](.)\1/, -2).should == nil + end + + it "returns nil if the given capture group was not matched but still sets $~" do + "test".send(@method, /te(z)?/, 1).should == nil + $~[0].should == "te" + $~[1].should == nil end it "calls to_int on the given index" do diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index 82911ef50b..7ef34c65da 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -3,12 +3,17 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "String#split with String" do + it "throws an ArgumentError if the string is not a valid" do + s = "\xDF".force_encoding(Encoding::UTF_8) + + -> { s.split }.should raise_error(ArgumentError) + -> { s.split(':') }.should raise_error(ArgumentError) + end + it "throws an ArgumentError if the pattern is not a valid string" do str = 'проверка' - broken_str = 'проверка' - broken_str.force_encoding('binary') - broken_str.chop! - broken_str.force_encoding('utf-8') + broken_str = "\xDF".force_encoding(Encoding::UTF_8) + -> { str.split(broken_str) }.should raise_error(ArgumentError) end @@ -218,6 +223,12 @@ describe "String#split with String" do end describe "String#split with Regexp" do + it "throws an ArgumentError if the string is not a valid" do + s = "\xDF".force_encoding(Encoding::UTF_8) + + -> { s.split(/./) }.should raise_error(ArgumentError) + end + it "divides self on regexp matches" do " now's the time".split(/ /).should == ["", "now's", "", "the", "time"] " x\ny ".split(/ /).should == ["", "x\ny"] diff --git a/spec/ruby/core/string/unpack/z_spec.rb b/spec/ruby/core/string/unpack/z_spec.rb index 552851ce04..ce8da4b29e 100644 --- a/spec/ruby/core/string/unpack/z_spec.rb +++ b/spec/ruby/core/string/unpack/z_spec.rb @@ -20,4 +20,9 @@ describe "String#unpack with format 'Z'" do ["\x00a\x00 bc \x00", ["", "c"]] ].should be_computed_by(:unpack, "Z5Z") end + + it "does not advance past the null byte when given a 'Z' format specifier" do + "a\x00\x0f".unpack('Zxc').should == ['a', 15] + "a\x00\x0f".unpack('Zcc').should == ['a', 0, 15] + end end |