From 67a1e2258974df4b597d019739595c18fbb9a7c1 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 29 Nov 2021 15:50:28 +0100 Subject: Update to ruby/spec@7f22a0b --- spec/ruby/core/string/each_grapheme_cluster_spec.rb | 8 ++++++++ spec/ruby/core/string/force_encoding_spec.rb | 2 +- spec/ruby/core/string/scan_spec.rb | 8 ++++++++ spec/ruby/core/string/scrub_spec.rb | 4 +++- spec/ruby/core/string/shared/eql.rb | 2 +- spec/ruby/core/string/shared/length.rb | 16 ++++++++++++++++ spec/ruby/core/string/valid_encoding_spec.rb | 8 ++++---- 7 files changed, 41 insertions(+), 7 deletions(-) (limited to 'spec/ruby/core/string') diff --git a/spec/ruby/core/string/each_grapheme_cluster_spec.rb b/spec/ruby/core/string/each_grapheme_cluster_spec.rb index 632f2337f1..b45d89ecb0 100644 --- a/spec/ruby/core/string/each_grapheme_cluster_spec.rb +++ b/spec/ruby/core/string/each_grapheme_cluster_spec.rb @@ -6,4 +6,12 @@ describe "String#each_grapheme_cluster" do it_behaves_like :string_chars, :each_grapheme_cluster it_behaves_like :string_grapheme_clusters, :each_grapheme_cluster it_behaves_like :string_each_char_without_block, :each_grapheme_cluster + + ruby_version_is '3.0' do + it "yields String instances for subclasses" do + a = [] + StringSpecs::MyString.new("abc").each_grapheme_cluster { |s| a << s.class } + a.should == [String, String, String] + end + end end diff --git a/spec/ruby/core/string/force_encoding_spec.rb b/spec/ruby/core/string/force_encoding_spec.rb index 2fe5f79c0f..f37aaf9eb4 100644 --- a/spec/ruby/core/string/force_encoding_spec.rb +++ b/spec/ruby/core/string/force_encoding_spec.rb @@ -60,7 +60,7 @@ describe "String#force_encoding" do end it "does not transcode self" do - str = "\u{8612}" + str = "é" str.dup.force_encoding('utf-16le').should_not == str.encode('utf-16le') end diff --git a/spec/ruby/core/string/scan_spec.rb b/spec/ruby/core/string/scan_spec.rb index 024e97022a..5f86dbbecc 100644 --- a/spec/ruby/core/string/scan_spec.rb +++ b/spec/ruby/core/string/scan_spec.rb @@ -198,4 +198,12 @@ describe "String#scan with pattern and block" do third.should == 'c'; end end + + ruby_version_is '3.0' do + it "yields String instances for subclasses" do + a = [] + StringSpecs::MyString.new("abc").scan(/./) { |s| a << s.class } + a.should == [String, String, String] + end + end end diff --git a/spec/ruby/core/string/scrub_spec.rb b/spec/ruby/core/string/scrub_spec.rb index 4da44a7992..3137399291 100644 --- a/spec/ruby/core/string/scrub_spec.rb +++ b/spec/ruby/core/string/scrub_spec.rb @@ -56,7 +56,9 @@ describe "String#scrub with a custom replacement" do it "replaces invalid byte sequences in frozen strings" do x81 = [0x81].pack('C').force_encoding('utf-8') (-"abc\u3042#{x81}").scrub("*").should == "abc\u3042*" - utf16_str = ("abc".encode('UTF-16LE').bytes + [0x81]).pack('c*').force_encoding('UTF-16LE') + + leading_surrogate = [0x00, 0xD8] + utf16_str = ("abc".encode('UTF-16LE').bytes + leading_surrogate).pack('c*').force_encoding('UTF-16LE') (-(utf16_str)).scrub("*".encode('UTF-16LE')).should == "abc*".encode('UTF-16LE') end diff --git a/spec/ruby/core/string/shared/eql.rb b/spec/ruby/core/string/shared/eql.rb index 85b861f4f1..b57d6895ff 100644 --- a/spec/ruby/core/string/shared/eql.rb +++ b/spec/ruby/core/string/shared/eql.rb @@ -21,7 +21,7 @@ describe :string_eql_value, shared: true do end it "considers encoding compatibility" do - "hello".force_encoding("utf-8").send(@method, "hello".force_encoding("utf-32le")).should be_false + "abcd".force_encoding("utf-8").send(@method, "abcd".force_encoding("utf-32le")).should be_false end it "ignores subclass differences" do diff --git a/spec/ruby/core/string/shared/length.rb b/spec/ruby/core/string/shared/length.rb index b9eae5170f..e931961455 100644 --- a/spec/ruby/core/string/shared/length.rb +++ b/spec/ruby/core/string/shared/length.rb @@ -36,4 +36,20 @@ describe :string_length, shared: true do concat.force_encoding(Encoding::ASCII_8BIT) concat.size.should == 4 end + + it "adds 1 for every invalid byte in UTF-8" do + "\xF4\x90\x80\x80".size.should == 4 + "a\xF4\x90\x80\x80b".size.should == 6 + "é\xF4\x90\x80\x80è".size.should == 6 + end + + it "adds 1 (and not 2) for a incomplete surrogate in UTF-16" do + "\x00\xd8".force_encoding("UTF-16LE").size.should == 1 + "\xd8\x00".force_encoding("UTF-16BE").size.should == 1 + end + + it "adds 1 for a broken sequence in UTF-32" do + "\x04\x03\x02\x01".force_encoding("UTF-32LE").size.should == 1 + "\x01\x02\x03\x04".force_encoding("UTF-32BE").size.should == 1 + end end diff --git a/spec/ruby/core/string/valid_encoding_spec.rb b/spec/ruby/core/string/valid_encoding_spec.rb index d5bf31710f..be7cef7a8e 100644 --- a/spec/ruby/core/string/valid_encoding_spec.rb +++ b/spec/ruby/core/string/valid_encoding_spec.rb @@ -43,10 +43,10 @@ describe "String#valid_encoding?" do str.force_encoding('KOI8-R').valid_encoding?.should be_true str.force_encoding('KOI8-U').valid_encoding?.should be_true str.force_encoding('Shift_JIS').valid_encoding?.should be_false - str.force_encoding('UTF-16BE').valid_encoding?.should be_false - str.force_encoding('UTF-16LE').valid_encoding?.should be_false - str.force_encoding('UTF-32BE').valid_encoding?.should be_false - str.force_encoding('UTF-32LE').valid_encoding?.should be_false + "\xD8\x00".force_encoding('UTF-16BE').valid_encoding?.should be_false + "\x00\xD8".force_encoding('UTF-16LE').valid_encoding?.should be_false + "\x04\x03\x02\x01".force_encoding('UTF-32BE').valid_encoding?.should be_false + "\x01\x02\x03\x04".force_encoding('UTF-32LE').valid_encoding?.should be_false str.force_encoding('Windows-1251').valid_encoding?.should be_true str.force_encoding('IBM437').valid_encoding?.should be_true str.force_encoding('IBM737').valid_encoding?.should be_true -- cgit v1.2.3