summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string
diff options
context:
space:
mode:
authorBenoit Daloze <[email protected]>2021-10-05 19:41:44 +0200
committerBenoit Daloze <[email protected]>2021-10-05 19:41:44 +0200
commitb9f34062f00d1d2548ca9b6af61a6447c2d0f8e3 (patch)
tree37c7600088a5e080b2f35794b0923395daf036d0 /spec/ruby/core/string
parentafcbb501ac17ba2ad5370ada5fd26e8dda9a5aaa (diff)
Update to ruby/spec@ccf0d85
Diffstat (limited to 'spec/ruby/core/string')
-rw-r--r--spec/ruby/core/string/lstrip_spec.rb3
-rw-r--r--spec/ruby/core/string/partition_spec.rb3
-rw-r--r--spec/ruby/core/string/reverse_spec.rb17
-rw-r--r--spec/ruby/core/string/rpartition_spec.rb3
-rw-r--r--spec/ruby/core/string/rstrip_spec.rb3
-rw-r--r--spec/ruby/core/string/scrub_spec.rb32
-rw-r--r--spec/ruby/core/string/shared/partition.rb36
-rw-r--r--spec/ruby/core/string/shared/strip.rb20
-rw-r--r--spec/ruby/core/string/strip_spec.rb3
9 files changed, 118 insertions, 2 deletions
diff --git a/spec/ruby/core/string/lstrip_spec.rb b/spec/ruby/core/string/lstrip_spec.rb
index 6b40189500..20e4cdeabd 100644
--- a/spec/ruby/core/string/lstrip_spec.rb
+++ b/spec/ruby/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/spec/ruby/core/string/partition_spec.rb b/spec/ruby/core/string/partition_spec.rb
index 996368d6a4..98311f2be4 100644
--- a/spec/ruby/core/string/partition_spec.rb
+++ b/spec/ruby/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/spec/ruby/core/string/reverse_spec.rb b/spec/ruby/core/string/reverse_spec.rb
index cf4956a528..b45ff2cf6f 100644
--- a/spec/ruby/core/string/reverse_spec.rb
+++ b/spec/ruby/core/string/reverse_spec.rb
@@ -10,6 +10,22 @@ describe "String#reverse" do
"".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' 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 @@ describe "String#reverse" do
it "reverses a string with multi byte characters" do
"微軟正黑體".reverse.should == "體黑正軟微"
end
-
end
describe "String#reverse!" do
diff --git a/spec/ruby/core/string/rpartition_spec.rb b/spec/ruby/core/string/rpartition_spec.rb
index fc37f8b427..c8f9afaee9 100644
--- a/spec/ruby/core/string/rpartition_spec.rb
+++ b/spec/ruby/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
diff --git a/spec/ruby/core/string/rstrip_spec.rb b/spec/ruby/core/string/rstrip_spec.rb
index 57e1867956..a1453f91fe 100644
--- a/spec/ruby/core/string/rstrip_spec.rb
+++ b/spec/ruby/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"
diff --git a/spec/ruby/core/string/scrub_spec.rb b/spec/ruby/core/string/scrub_spec.rb
index 5c67ad01bc..4da44a7992 100644
--- a/spec/ruby/core/string/scrub_spec.rb
+++ b/spec/ruby/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 @@ describe "String#scrub with a default replacement" do
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' 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 @@ describe "String#scrub with a custom replacement" do
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 @@ describe "String#scrub with a block" do
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
diff --git a/spec/ruby/core/string/shared/partition.rb b/spec/ruby/core/string/shared/partition.rb
new file mode 100644
index 0000000000..7dc3d9cc0b
--- /dev/null
+++ b/spec/ruby/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' 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
diff --git a/spec/ruby/core/string/shared/strip.rb b/spec/ruby/core/string/shared/strip.rb
new file mode 100644
index 0000000000..9c232b4694
--- /dev/null
+++ b/spec/ruby/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' 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
diff --git a/spec/ruby/core/string/strip_spec.rb b/spec/ruby/core/string/strip_spec.rb
index 463a9fedf3..8517bf2d2a 100644
--- a/spec/ruby/core/string/strip_spec.rb
+++ b/spec/ruby/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"