summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/sub_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/sub_spec.rb')
-rw-r--r--spec/ruby/core/string/sub_spec.rb187
1 files changed, 64 insertions, 123 deletions
diff --git a/spec/ruby/core/string/sub_spec.rb b/spec/ruby/core/string/sub_spec.rb
index 901d1596c5..6ff28ec851 100644
--- a/spec/ruby/core/string/sub_spec.rb
+++ b/spec/ruby/core/string/sub_spec.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
@@ -137,26 +138,6 @@ describe "String#sub with pattern, replacement" do
"hello".sub(/./, 'hah\\').should == 'hah\\ello'
end
- 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.sub(/./, a).tainted?.should == true
- hello_t.sub(/./, empty).tainted?.should == true
-
- hello.sub(/./, a_t).tainted?.should == true
- hello.sub(/./, empty_t).tainted?.should == true
- hello.sub(//, empty_t).tainted?.should == true
-
- hello.sub(//.taint, "foo").tainted?.should == false
- end
-
it "tries to convert pattern to a string using to_str" do
pattern = mock('.')
pattern.should_receive(:to_str).and_return(".")
@@ -166,16 +147,16 @@ describe "String#sub with pattern, replacement" do
not_supported_on :opal do
it "raises a TypeError when pattern is a Symbol" do
- lambda { "hello".sub(:woot, "x") }.should raise_error(TypeError)
+ -> { "hello".sub(:woot, "x") }.should raise_error(TypeError)
end
end
it "raises a TypeError when pattern is an Array" do
- lambda { "hello".sub([], "x") }.should raise_error(TypeError)
+ -> { "hello".sub([], "x") }.should raise_error(TypeError)
end
it "raises a TypeError when pattern can't be converted to a string" do
- lambda { "hello".sub(Object.new, nil) }.should raise_error(TypeError)
+ -> { "hello".sub(Object.new, nil) }.should raise_error(TypeError)
end
it "tries to convert replacement to a string using to_str" do
@@ -186,15 +167,15 @@ describe "String#sub with pattern, replacement" do
end
it "raises a TypeError when replacement can't be converted to a string" do
- lambda { "hello".sub(/[aeiou]/, []) }.should raise_error(TypeError)
- lambda { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError)
+ -> { "hello".sub(/[aeiou]/, []) }.should raise_error(TypeError)
+ -> { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError)
end
- it "returns subclass instances when called on a subclass" do
- StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(StringSpecs::MyString)
- StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
- StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
- StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(StringSpecs::MyString)
+ it "returns String instances when called on a subclass" do
+ StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(String)
+ StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(String)
+ StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(String)
+ StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(String)
end
it "sets $~ to MatchData of match and nil when there's none" do
@@ -223,6 +204,17 @@ describe "String#sub with pattern, replacement" do
"ababa".sub(/(b)/, '\\\\\1').should == "a\\baba"
end
+ it "handles a pattern in a superset encoding" do
+ result = 'abc'.force_encoding(Encoding::US_ASCII).sub('é', 'è')
+ result.should == 'abc'
+ result.encoding.should == Encoding::US_ASCII
+ end
+
+ it "handles a pattern in a subset encoding" do
+ result = 'été'.sub('t'.force_encoding(Encoding::US_ASCII), 'u')
+ result.should == 'éué'
+ result.encoding.should == Encoding::UTF_8
+ end
end
describe "String#sub with pattern and block" do
@@ -240,10 +232,10 @@ describe "String#sub with pattern and block" do
offsets = []
str.sub(/([aeiou])/) do
- md = $~
- md.string.should == str
- offsets << md.offset(0)
- str
+ md = $~
+ md.string.should == str
+ offsets << md.offset(0)
+ str
end.should == "hhellollo"
offsets.should == [[1, 2]]
@@ -284,26 +276,6 @@ describe "String#sub with pattern and block" do
obj.should_receive(:to_s).and_return("ok")
"hello".sub(/.+/) { obj }.should == "ok"
end
-
- 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.sub(/./) { a }.tainted?.should == true
- hello_t.sub(/./) { empty }.tainted?.should == true
-
- hello.sub(/./) { a_t }.tainted?.should == true
- hello.sub(/./) { empty_t }.tainted?.should == true
- hello.sub(//) { empty_t }.tainted?.should == true
-
- hello.sub(//.taint) { "foo" }.tainted?.should == false
- end
end
describe "String#sub! with pattern, replacement" do
@@ -313,12 +285,6 @@ describe "String#sub! with pattern, replacement" do
a.should == "h*llo"
end
- it "taints self if replacement is tainted" do
- a = "hello"
- a.sub!(/./.taint, "foo").tainted?.should == false
- a.sub!(/./, "foo".taint).tainted?.should == true
- end
-
it "returns nil if no modifications were made" do
a = "hello"
a.sub!(/z/, '*').should == nil
@@ -326,13 +292,34 @@ describe "String#sub! with pattern, replacement" do
a.should == "hello"
end
- it "raises a #{frozen_error_class} when self is frozen" do
+ it "raises a FrozenError when self is frozen" do
s = "hello"
s.freeze
- lambda { s.sub!(/ROAR/, "x") }.should raise_error(frozen_error_class)
- lambda { s.sub!(/e/, "e") }.should raise_error(frozen_error_class)
- lambda { s.sub!(/[aeiou]/, '*') }.should raise_error(frozen_error_class)
+ -> { s.sub!(/ROAR/, "x") }.should raise_error(FrozenError)
+ -> { s.sub!(/e/, "e") }.should raise_error(FrozenError)
+ -> { s.sub!(/[aeiou]/, '*') }.should raise_error(FrozenError)
+ end
+
+ it "handles a pattern in a superset encoding" do
+ string = 'abc'.force_encoding(Encoding::US_ASCII)
+
+ result = string.sub!('é', 'è')
+
+ 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.sub!(pattern, 'u')
+
+ result.should == string
+ string.should == 'éué'
+ string.encoding.should == Encoding::UTF_8
end
end
@@ -352,21 +339,15 @@ describe "String#sub! with pattern and block" do
offsets = []
str.dup.sub!(/([aeiou])/) do
- md = $~
- md.string.should == str
- offsets << md.offset(0)
- str
+ md = $~
+ md.string.should == str
+ offsets << md.offset(0)
+ str
end.should == "hhellollo"
offsets.should == [[1, 2]]
end
- it "taints self if block's result is tainted" do
- a = "hello"
- a.sub!(/./.taint) { "foo" }.tainted?.should == false
- a.sub!(/./) { "foo".taint }.tainted?.should == true
- end
-
it "returns nil if no modifications were made" do
a = "hello"
a.sub!(/z/) { '*' }.should == nil
@@ -376,16 +357,16 @@ describe "String#sub! with pattern and block" do
it "raises a RuntimeError if the string is modified while substituting" do
str = "hello"
- lambda { str.sub!(//) { str << 'x' } }.should raise_error(RuntimeError)
+ -> { str.sub!(//) { str << 'x' } }.should raise_error(RuntimeError)
end
- it "raises a #{frozen_error_class} when self is frozen" do
+ it "raises a FrozenError when self is frozen" do
s = "hello"
s.freeze
- lambda { s.sub!(/ROAR/) { "x" } }.should raise_error(frozen_error_class)
- lambda { s.sub!(/e/) { "e" } }.should raise_error(frozen_error_class)
- lambda { s.sub!(/[aeiou]/) { '*' } }.should raise_error(frozen_error_class)
+ -> { s.sub!(/ROAR/) { "x" } }.should raise_error(FrozenError)
+ -> { s.sub!(/e/) { "e" } }.should raise_error(FrozenError)
+ -> { s.sub!(/[aeiou]/) { '*' } }.should raise_error(FrozenError)
end
end
@@ -428,7 +409,7 @@ describe "String#sub with pattern and Hash" do
it "uses the hash's value set from default_proc for missing keys" do
hsh = {}
- hsh.default_proc = lambda { |k,v| 'lamb' }
+ hsh.default_proc = -> k, v { 'lamb' }
"food!".sub(/./, hsh).should == "lambood!"
end
@@ -452,26 +433,6 @@ describe "String#sub with pattern and Hash" do
"hello".sub(/(.+)/, 'hello' => repl ).should == repl
end
- it "untrusts the result if the original string is untrusted" do
- str = "Ghana".untrust
- str.sub(/[Aa]na/, 'ana' => '').untrusted?.should be_true
- end
-
- it "untrusts the result if a hash value is untrusted" do
- str = "Ghana"
- str.sub(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
- end
-
- it "taints the result if the original string is tainted" do
- str = "Ghana".taint
- str.sub(/[Aa]na/, 'ana' => '').tainted?.should be_true
- end
-
- it "taints the result if a hash value is tainted" do
- str = "Ghana"
- str.sub(/a$/, 'a' => 'di'.taint).tainted?.should be_true
- end
-
end
describe "String#sub! with pattern and Hash" do
@@ -513,7 +474,7 @@ describe "String#sub! with pattern and Hash" do
it "uses the hash's value set from default_proc for missing keys" do
hsh = {}
- hsh.default_proc = lambda { |k,v| 'lamb' }
+ hsh.default_proc = -> k, v { 'lamb' }
"food!".sub!(/./, hsh).should == "lambood!"
end
@@ -536,36 +497,16 @@ describe "String#sub! with pattern and Hash" do
repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
"hello".sub!(/(.+)/, 'hello' => repl ).should == repl
end
-
- it "keeps untrusted state" do
- str = "Ghana".untrust
- str.sub!(/[Aa]na/, 'ana' => '').untrusted?.should be_true
- end
-
- it "untrusts self if a hash value is untrusted" do
- str = "Ghana"
- str.sub!(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
- end
-
- it "keeps tainted state" do
- str = "Ghana".taint
- str.sub!(/[Aa]na/, 'ana' => '').tainted?.should be_true
- end
-
- it "taints self if a hash value is tainted" do
- str = "Ghana"
- str.sub!(/a$/, 'a' => 'di'.taint).tainted?.should be_true
- end
end
describe "String#sub with pattern and without replacement and block" do
it "raises a ArgumentError" do
- lambda { "abca".sub(/a/) }.should raise_error(ArgumentError)
+ -> { "abca".sub(/a/) }.should raise_error(ArgumentError)
end
end
describe "String#sub! with pattern and without replacement and block" do
it "raises a ArgumentError" do
- lambda { "abca".sub!(/a/) }.should raise_error(ArgumentError)
+ -> { "abca".sub!(/a/) }.should raise_error(ArgumentError)
end
end