Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returns String instances when called on a subclass in Ruby 3.0.0 #851

Merged
3 changes: 3 additions & 0 deletions core/string/lstrip_spec.rb
Original file line number Diff line number Diff line change
@@ -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 "
Expand Down
3 changes: 3 additions & 0 deletions core/string/partition_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 16 additions & 1 deletion core/string/reverse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
eregon marked this conversation as resolved.
Show resolved Hide resolved
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?
Expand All @@ -20,7 +36,6 @@
it "reverses a string with multi byte characters" do
"微軟正黑體".reverse.should == "體黑正軟微"
end

end

describe "String#reverse!" do
Expand Down
3 changes: 3 additions & 0 deletions core/string/rpartition_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions core/string/rstrip_spec.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
32 changes: 31 additions & 1 deletion core/string/scrub_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
eregon marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions core/string/shared/partition.rb
Original file line number Diff line number Diff line change
@@ -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
eregon marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 20 additions & 0 deletions core/string/shared/strip.rb
Original file line number Diff line number Diff line change
@@ -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
eregon marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 3 additions & 0 deletions core/string/strip_spec.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down