From d0a9cad7b4ab3332609a0572d3ad58dcd68fc84f Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 16:34:12 +0200 Subject: [PATCH 1/8] String#partition returns String instances when called on a subclass --- core/string/partition_spec.rb | 3 +++ core/string/shared/partition.rb | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 core/string/shared/partition.rb diff --git a/core/string/partition_spec.rb b/core/string/partition_spec.rb index 996368d6a4..98311f2be4 100644 --- a/core/string/partition_spec.rb +++ b/core/string/partition_spec.rb @@ -1,7 +1,10 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/partition' describe "String#partition with String" do + it_behaves_like :string_partition, :partition + it "returns an array of substrings based on splitting on the given string" do "hello world".partition("o").should == ["hell", "o", " world"] end diff --git a/core/string/shared/partition.rb b/core/string/shared/partition.rb new file mode 100644 index 0000000000..5785443d6f --- /dev/null +++ b/core/string/shared/partition.rb @@ -0,0 +1,36 @@ +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' + +describe :string_partition, shared: true do + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("hello").send(@method, "l").each do |item| + item.should be_an_instance_of(String) + end + + StringSpecs::MyString.new("hello").send(@method, "x").each do |item| + item.should be_an_instance_of(String) + end + + StringSpecs::MyString.new("hello").send(@method, /l./).each do |item| + item.should be_an_instance_of(String) + end + end + end + + ruby_version_is ''...'3.0.0' do + it "returns subclass instances when called on a subclass" do + StringSpecs::MyString.new("hello").send(@method, StringSpecs::MyString.new("l")).each do |item| + item.should be_an_instance_of(StringSpecs::MyString) + end + + StringSpecs::MyString.new("hello").send(@method, "x").each do |item| + item.should be_an_instance_of(StringSpecs::MyString) + end + + StringSpecs::MyString.new("hello").send(@method, /l./).each do |item| + item.should be_an_instance_of(StringSpecs::MyString) + end + end + end +end From 0c953f85144778c8f63450111a19d556c636863a Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 16:37:02 +0200 Subject: [PATCH 2/8] String#rpartition returns String instances when called on a subclass --- core/string/rpartition_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/string/rpartition_spec.rb b/core/string/rpartition_spec.rb index fc37f8b427..c8f9afaee9 100644 --- a/core/string/rpartition_spec.rb +++ b/core/string/rpartition_spec.rb @@ -1,7 +1,10 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/partition' describe "String#rpartition with String" do + it_behaves_like :string_partition, :rpartition + it "returns an array of substrings based on splitting on the given string" do "hello world".rpartition("o").should == ["hello w", "o", "rld"] end From e64dbb0f6499920407021b6cba4e7150db4aa438 Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 17:09:35 +0200 Subject: [PATCH 3/8] String#reverse returns String instances when called on a subclass --- core/string/reverse_spec.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/string/reverse_spec.rb b/core/string/reverse_spec.rb index cf4956a528..ac38d9bead 100644 --- a/core/string/reverse_spec.rb +++ b/core/string/reverse_spec.rb @@ -10,6 +10,22 @@ "".reverse.should == "" end + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("stressed").reverse.should be_an_instance_of(String) + StringSpecs::MyString.new("m").reverse.should be_an_instance_of(String) + StringSpecs::MyString.new("").reverse.should be_an_instance_of(String) + end + end + + ruby_version_is ''...'3.0.0' do + it "returns subclass instances when called on a subclass" do + StringSpecs::MyString.new("stressed").reverse.should be_an_instance_of(StringSpecs::MyString) + StringSpecs::MyString.new("m").reverse.should be_an_instance_of(StringSpecs::MyString) + StringSpecs::MyString.new("").reverse.should be_an_instance_of(StringSpecs::MyString) + end + end + ruby_version_is ''...'2.7' do it "taints the result if self is tainted" do "".taint.reverse.should.tainted? @@ -20,7 +36,6 @@ it "reverses a string with multi byte characters" do "微軟正黑體".reverse.should == "體黑正軟微" end - end describe "String#reverse!" do From e395f33d1323464de8cce9cfcdf9cbce367b68bf Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 17:20:41 +0200 Subject: [PATCH 4/8] String#lstrip returns String instances when called on a subclass --- core/string/lstrip_spec.rb | 3 +++ core/string/shared/strip.rb | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 core/string/shared/strip.rb diff --git a/core/string/lstrip_spec.rb b/core/string/lstrip_spec.rb index 6b40189500..20e4cdeabd 100644 --- a/core/string/lstrip_spec.rb +++ b/core/string/lstrip_spec.rb @@ -1,7 +1,10 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/strip' describe "String#lstrip" do + it_behaves_like :string_strip, :lstrip + it "returns a copy of self with leading whitespace removed" do " hello ".lstrip.should == "hello " " hello world ".lstrip.should == "hello world " diff --git a/core/string/shared/strip.rb b/core/string/shared/strip.rb new file mode 100644 index 0000000000..caf6275fae --- /dev/null +++ b/core/string/shared/strip.rb @@ -0,0 +1,20 @@ +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' + +describe :string_strip, shared: true do + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new(" hello ").send(@method).should be_an_instance_of(String) + StringSpecs::MyString.new(" ").send(@method).should be_an_instance_of(String) + StringSpecs::MyString.new("").send(@method).should be_an_instance_of(String) + end + end + + ruby_version_is ''...'3.0.0' do + it "returns subclass instances when called on a subclass" do + StringSpecs::MyString.new(" hello ").send(@method).should be_an_instance_of(StringSpecs::MyString) + StringSpecs::MyString.new(" ").send(@method).should be_an_instance_of(StringSpecs::MyString) + StringSpecs::MyString.new("").send(@method).should be_an_instance_of(StringSpecs::MyString) + end + end +end From 6a92dc9205a3bcf1a977fcd02fe2104ff068bd30 Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 17:25:07 +0200 Subject: [PATCH 5/8] String#rstrip returns String instances when called on a subclass --- core/string/rstrip_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/string/rstrip_spec.rb b/core/string/rstrip_spec.rb index 57e1867956..a1453f91fe 100644 --- a/core/string/rstrip_spec.rb +++ b/core/string/rstrip_spec.rb @@ -1,7 +1,10 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/strip' describe "String#rstrip" do + it_behaves_like :string_strip, :rstrip + it "returns a copy of self with trailing whitespace removed" do " hello ".rstrip.should == " hello" " hello world ".rstrip.should == " hello world" From e64781b09883620f94b8fa4f1ad1d92399857c85 Mon Sep 17 00:00:00 2001 From: mario Date: Sat, 2 Oct 2021 17:26:50 +0200 Subject: [PATCH 6/8] String#strip returns String instances when called on a subclass --- core/string/strip_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/string/strip_spec.rb b/core/string/strip_spec.rb index 463a9fedf3..8517bf2d2a 100644 --- a/core/string/strip_spec.rb +++ b/core/string/strip_spec.rb @@ -1,7 +1,10 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/strip' describe "String#strip" do + it_behaves_like :string_strip, :strip + it "returns a new string with leading and trailing whitespace removed" do " hello ".strip.should == "hello" " hello world ".strip.should == "hello world" From 9e6f708796c1832a101475da369078403d8f23d8 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 4 Oct 2021 09:06:39 +0200 Subject: [PATCH 7/8] String#scrub returns String instances when called on a subclass --- core/string/scrub_spec.rb | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/core/string/scrub_spec.rb b/core/string/scrub_spec.rb index 5c67ad01bc..df6d42e6e1 100644 --- a/core/string/scrub_spec.rb +++ b/core/string/scrub_spec.rb @@ -1,5 +1,6 @@ # -*- encoding: utf-8 -*- require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "String#scrub with a default replacement" do it "returns self for valid strings" do @@ -19,12 +20,25 @@ input.scrub.should == "foo" end - it "replaces invalid byte sequences when using ASCII as the input encoding" do xE3x80 = [0xE3, 0x80].pack('CC').force_encoding 'utf-8' input = "abc\u3042#{xE3x80}".force_encoding('ASCII') input.scrub.should == "abc?????" end + + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(String) + input = [0x81].pack('C').force_encoding('utf-8') + StringSpecs::MyString.new(input).scrub.should be_an_instance_of(String) + end + end + + ruby_version_is ''...'3.0.0' do + it "returns subclass instances when called on a subclass" do + StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(StringSpecs::MyString) + end + end end describe "String#scrub with a custom replacement" do @@ -65,6 +79,14 @@ block.should raise_error(TypeError) end + + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("foo").scrub("*").should be_an_instance_of(String) + input = [0x81].pack('C').force_encoding('utf-8') + StringSpecs::MyString.new(input).scrub("*").should be_an_instance_of(String) + end + end end describe "String#scrub with a block" do @@ -89,6 +111,14 @@ replaced.should == "€€" end + + ruby_version_is '3.0' do + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("foo").scrub { |b| "*" }.should be_an_instance_of(String) + input = [0x81].pack('C').force_encoding('utf-8') + StringSpecs::MyString.new(input).scrub { |b| "<#{b.unpack("H*")[0]}>" }.should be_an_instance_of(String) + end + end end describe "String#scrub!" do From 77c8eb94a2f17f2e57600936685275496792aae1 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 4 Oct 2021 15:18:31 +0200 Subject: [PATCH 8/8] Fix guards --- core/string/reverse_spec.rb | 2 +- core/string/scrub_spec.rb | 2 +- core/string/shared/partition.rb | 2 +- core/string/shared/strip.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/string/reverse_spec.rb b/core/string/reverse_spec.rb index ac38d9bead..b45ff2cf6f 100644 --- a/core/string/reverse_spec.rb +++ b/core/string/reverse_spec.rb @@ -18,7 +18,7 @@ end end - ruby_version_is ''...'3.0.0' do + ruby_version_is ''...'3.0' do it "returns subclass instances when called on a subclass" do StringSpecs::MyString.new("stressed").reverse.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("m").reverse.should be_an_instance_of(StringSpecs::MyString) diff --git a/core/string/scrub_spec.rb b/core/string/scrub_spec.rb index df6d42e6e1..4da44a7992 100644 --- a/core/string/scrub_spec.rb +++ b/core/string/scrub_spec.rb @@ -34,7 +34,7 @@ end end - ruby_version_is ''...'3.0.0' do + ruby_version_is ''...'3.0' do it "returns subclass instances when called on a subclass" do StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(StringSpecs::MyString) end diff --git a/core/string/shared/partition.rb b/core/string/shared/partition.rb index 5785443d6f..7dc3d9cc0b 100644 --- a/core/string/shared/partition.rb +++ b/core/string/shared/partition.rb @@ -18,7 +18,7 @@ end end - ruby_version_is ''...'3.0.0' do + ruby_version_is ''...'3.0' do it "returns subclass instances when called on a subclass" do StringSpecs::MyString.new("hello").send(@method, StringSpecs::MyString.new("l")).each do |item| item.should be_an_instance_of(StringSpecs::MyString) diff --git a/core/string/shared/strip.rb b/core/string/shared/strip.rb index caf6275fae..9c232b4694 100644 --- a/core/string/shared/strip.rb +++ b/core/string/shared/strip.rb @@ -10,7 +10,7 @@ end end - ruby_version_is ''...'3.0.0' do + ruby_version_is ''...'3.0' do it "returns subclass instances when called on a subclass" do StringSpecs::MyString.new(" hello ").send(@method).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new(" ").send(@method).should be_an_instance_of(StringSpecs::MyString)