diff options
Diffstat (limited to 'spec/ruby/core/string/gsub_spec.rb')
-rw-r--r-- | spec/ruby/core/string/gsub_spec.rb | 195 |
1 files changed, 39 insertions, 156 deletions
diff --git a/spec/ruby/core/string/gsub_spec.rb b/spec/ruby/core/string/gsub_spec.rb index ad41b7e0a2..0d9f32eca2 100644 --- a/spec/ruby/core/string/gsub_spec.rb +++ b/spec/ruby/core/string/gsub_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -160,56 +161,12 @@ describe "String#gsub with pattern and replacement" do it_behaves_like :string_gsub_named_capture, :gsub - ruby_version_is ''...'2.7' do - it "taints the result if the original string or replacement is tainted" do - hello = "hello" - hello_t = "hello" - a = "a" - a_t = "a" - empty = "" - empty_t = "" - - hello_t.taint; a_t.taint; empty_t.taint - - hello_t.gsub(/./, a).should.tainted? - hello_t.gsub(/./, empty).should.tainted? - - hello.gsub(/./, a_t).should.tainted? - hello.gsub(/./, empty_t).should.tainted? - hello.gsub(//, empty_t).should.tainted? - - hello.gsub(//.taint, "foo").should_not.tainted? - end - end - it "handles pattern collapse" do str = "こにちわ" reg = %r!! str.gsub(reg, ".").should == ".こ.に.ち.わ." end - ruby_version_is ''...'2.7' do - it "untrusts the result if the original string or replacement is untrusted" do - hello = "hello" - hello_t = "hello" - a = "a" - a_t = "a" - empty = "" - empty_t = "" - - hello_t.untrust; a_t.untrust; empty_t.untrust - - hello_t.gsub(/./, a).should.untrusted? - hello_t.gsub(/./, empty).should.untrusted? - - hello.gsub(/./, a_t).should.untrusted? - hello.gsub(/./, empty_t).should.untrusted? - hello.gsub(//, empty_t).should.untrusted? - - hello.gsub(//.untrust, "foo").should_not.untrusted? - end - end - it "tries to convert pattern to a string using to_str" do pattern = mock('.') def pattern.to_str() "." end @@ -236,26 +193,13 @@ describe "String#gsub with pattern and replacement" do -> { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError) end - ruby_version_is ''...'3.0' do - it "returns subclass instances when called on a subclass" do - StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(StringSpecs::MyString) - end + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(String) + StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(String) + StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(String) + StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(String) end - ruby_version_is '3.0' do - it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(String) - StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(String) - end - end - - # Note: $~ cannot be tested because mspec messes with it - it "sets $~ to MatchData of last match and nil when there's none" do 'hello.'.gsub('hello', 'x') $~[0].should == 'hello' @@ -269,6 +213,18 @@ describe "String#gsub with pattern and replacement" do 'hello.'.gsub(/not/, 'x') $~.should == nil end + + it "handles a pattern in a superset encoding" do + result = 'abc'.force_encoding(Encoding::US_ASCII).gsub('é', 'è') + result.should == 'abc' + result.encoding.should == Encoding::US_ASCII + end + + it "handles a pattern in a subset encoding" do + result = 'été'.gsub('t'.force_encoding(Encoding::US_ASCII), 'u') + result.should == 'éué' + result.encoding.should == Encoding::UTF_8 + end end describe "String#gsub with pattern and Hash" do @@ -336,28 +292,6 @@ describe "String#gsub with pattern and Hash" do repl = '\& \0 \1 \` \\\' \+ \\\\ foo' "hello".gsub(/(.+)/, 'hello' => repl ).should == repl end - - ruby_version_is ''...'2.7' do - it "untrusts the result if the original string is untrusted" do - str = "Ghana".untrust - str.gsub(/[Aa]na/, 'ana' => '').untrusted?.should be_true - end - - it "untrusts the result if a hash value is untrusted" do - str = "Ghana" - str.gsub(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true - end - - it "taints the result if the original string is tainted" do - str = "Ghana".taint - str.gsub(/[Aa]na/, 'ana' => '').tainted?.should be_true - end - - it "taints the result if a hash value is tainted" do - str = "Ghana" - str.gsub(/a$/, 'a' => 'di'.taint).tainted?.should be_true - end - end end describe "String#gsub! with pattern and Hash" do @@ -426,28 +360,6 @@ describe "String#gsub! with pattern and Hash" do repl = '\& \0 \1 \` \\\' \+ \\\\ foo' "hello".gsub!(/(.+)/, 'hello' => repl ).should == repl end - - ruby_version_is ''...'2.7' do - it "keeps untrusted state" do - str = "Ghana".untrust - str.gsub!(/[Aa]na/, 'ana' => '').untrusted?.should be_true - end - - it "untrusts self if a hash value is untrusted" do - str = "Ghana" - str.gsub!(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true - end - - it "keeps tainted state" do - str = "Ghana".taint - str.gsub!(/[Aa]na/, 'ana' => '').tainted?.should be_true - end - - it "taints self if a hash value is tainted" do - str = "Ghana" - str.gsub!(/a$/, 'a' => 'di'.taint).tainted?.should be_true - end - end end describe "String#gsub with pattern and block" do @@ -526,28 +438,6 @@ describe "String#gsub with pattern and block" do "hello".gsub(/.+/) { obj }.should == "ok" end - ruby_version_is ''...'2.7' do - it "untrusts the result if the original string or replacement is untrusted" do - hello = "hello" - hello_t = "hello" - a = "a" - a_t = "a" - empty = "" - empty_t = "" - - hello_t.untrust; a_t.untrust; empty_t.untrust - - hello_t.gsub(/./) { a }.should.untrusted? - hello_t.gsub(/./) { empty }.should.untrusted? - - hello.gsub(/./) { a_t }.should.untrusted? - hello.gsub(/./) { empty_t }.should.untrusted? - hello.gsub(//) { empty_t }.should.untrusted? - - hello.gsub(//.untrust) { "foo" }.should_not.untrusted? - end - end - it "uses the compatible encoding if they are compatible" do s = "hello" s2 = "#{195.chr}#{192.chr}#{195.chr}" @@ -615,20 +505,6 @@ describe "String#gsub! with pattern and replacement" do a.should == "*¿** **é*?*" end - ruby_version_is ''...'2.7' do - it "taints self if replacement is tainted" do - a = "hello" - a.gsub!(/./.taint, "foo").should_not.tainted? - a.gsub!(/./, "foo".taint).should.tainted? - end - - it "untrusts self if replacement is untrusted" do - a = "hello" - a.gsub!(/./.untrust, "foo").should_not.untrusted? - a.gsub!(/./, "foo".untrust).should.untrusted? - end - end - it "returns nil if no modifications were made" do a = "hello" a.gsub!(/z/, '*').should == nil @@ -645,6 +521,27 @@ describe "String#gsub! with pattern and replacement" do -> { s.gsub!(/e/, "e") }.should raise_error(FrozenError) -> { s.gsub!(/[aeiou]/, '*') }.should raise_error(FrozenError) end + + it "handles a pattern in a superset encoding" do + string = 'abc'.force_encoding(Encoding::US_ASCII) + + result = string.gsub!('é', 'è') + + result.should == nil + string.should == 'abc' + string.encoding.should == Encoding::US_ASCII + end + + it "handles a pattern in a subset encoding" do + string = 'été' + pattern = 't'.force_encoding(Encoding::US_ASCII) + + result = string.gsub!(pattern, 'u') + + result.should == string + string.should == 'éué' + string.encoding.should == Encoding::UTF_8 + end end describe "String#gsub! with pattern and block" do @@ -654,20 +551,6 @@ describe "String#gsub! with pattern and block" do a.should == "h*ll*" end - ruby_version_is ''...'2.7' do - it "taints self if block's result is tainted" do - a = "hello" - a.gsub!(/./.taint) { "foo" }.should_not.tainted? - a.gsub!(/./) { "foo".taint }.should.tainted? - end - - it "untrusts self if block's result is untrusted" do - a = "hello" - a.gsub!(/./.untrust) { "foo" }.should_not.untrusted? - a.gsub!(/./) { "foo".untrust }.should.untrusted? - end - end - it "returns nil if no modifications were made" do a = "hello" a.gsub!(/z/) { '*' }.should == nil |