diff options
Diffstat (limited to 'spec/ruby/core/string/slice_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/slice_spec.rb | 240 |
1 files changed, 77 insertions, 163 deletions
diff --git a/spec/ruby/core/string/slice_spec.rb b/spec/ruby/core/string/slice_spec.rb index a3e9d6b74f..14e2251b3f 100644 --- a/spec/ruby/core/string/slice_spec.rb +++ b/spec/ruby/core/string/slice_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- - +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/slice' @@ -53,10 +53,10 @@ describe "String#slice! with index" do a.should == "hello" end - it "raises a #{frozen_error_class} if self is frozen" do - lambda { "hello".freeze.slice!(1) }.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(10) }.should raise_error(frozen_error_class) - lambda { "".freeze.slice!(0) }.should raise_error(frozen_error_class) + it "raises a FrozenError if self is frozen" do + -> { "hello".freeze.slice!(1) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10) }.should.raise(FrozenError) + -> { "".freeze.slice!(0) }.should.raise(FrozenError) end it "calls to_int on index" do @@ -72,15 +72,13 @@ describe "String#slice! with index" do "hello".slice!(obj).should == ?e end - with_feature :encoding do - - it "returns the character given by the character index" do - "hellö there".slice!(1).should == "e" - "hellö there".slice!(4).should == "ö" - "hellö there".slice!(6).should == "t" - end + it "returns the character given by the character index" do + "hellö there".slice!(1).should == "e" + "hellö there".slice!(4).should == "ö" + "hellö there".slice!(6).should == "t" end + end describe "String#slice! with index, length" do @@ -96,14 +94,6 @@ describe "String#slice! with index, length" do a.should == "h" end - it "always taints resulting strings when self is tainted" do - str = "hello world" - str.taint - - str.slice!(0, 0).tainted?.should == true - str.slice!(2, 1).tainted?.should == true - end - it "returns nil if the given position is out of self" do a = "hello" a.slice(10, 3).should == nil @@ -119,14 +109,14 @@ describe "String#slice! with index, length" do a.should == "hello" end - it "raises a #{frozen_error_class} if self is frozen" do - lambda { "hello".freeze.slice!(1, 2) }.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(10, 3) }.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(-10, 3)}.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(4, -3) }.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(10, 3) }.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(-10, 3)}.should raise_error(frozen_error_class) - lambda { "hello".freeze.slice!(4, -3) }.should raise_error(frozen_error_class) + it "raises a FrozenError if self is frozen" do + -> { "hello".freeze.slice!(1, 2) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError) + -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError) + -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError) end it "calls to_int on idx and length" do @@ -142,25 +132,22 @@ describe "String#slice! with index, length" do "hello".slice!(obj, obj).should == "ll" end - it "returns subclass instances" do + it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString) - s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString) + s.slice!(0, 0).should.instance_of?(String) + s.slice!(0, 4).should.instance_of?(String) end - with_feature :encoding do - - it "returns the substring given by the character offsets" do - "hellö there".slice!(1,0).should == "" - "hellö there".slice!(1,3).should == "ell" - "hellö there".slice!(1,6).should == "ellö t" - "hellö there".slice!(1,9).should == "ellö ther" - end + it "returns the substring given by the character offsets" do + "hellö there".slice!(1,0).should == "" + "hellö there".slice!(1,3).should == "ell" + "hellö there".slice!(1,6).should == "ellö t" + "hellö there".slice!(1,9).should == "ellö ther" + end - it "treats invalid bytes as single bytes" do - xE6xCB = [0xE6,0xCB].pack('CC').force_encoding('utf-8') - "a#{xE6xCB}b".slice!(1, 2).should == xE6xCB - end + it "treats invalid bytes as single bytes" do + xE6xCB = [0xE6,0xCB].pack('CC').force_encoding('utf-8') + "a#{xE6xCB}b".slice!(1, 2).should == xE6xCB end end @@ -188,18 +175,10 @@ describe "String#slice! Range" do b.should == "hello" end - it "always taints resulting strings when self is tainted" do - str = "hello world" - str.taint - - str.slice!(0..0).tainted?.should == true - str.slice!(2..3).tainted?.should == true - end - - it "returns subclass instances" do + it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString) - s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString) + s.slice!(0...0).should.instance_of?(String) + s.slice!(0..4).should.instance_of?(String) end it "calls to_int on range arguments" do @@ -236,27 +215,25 @@ describe "String#slice! Range" do a.slice!(range_incl).should == "OO" end - with_feature :encoding do - - it "returns the substring given by the character offsets of the range" do - "hellö there".slice!(1..1).should == "e" - "hellö there".slice!(1..3).should == "ell" - "hellö there".slice!(1...3).should == "el" - "hellö there".slice!(-4..-2).should == "her" - "hellö there".slice!(-4...-2).should == "he" - "hellö there".slice!(5..-1).should == " there" - "hellö there".slice!(5...-1).should == " ther" - end + it "returns the substring given by the character offsets of the range" do + "hellö there".slice!(1..1).should == "e" + "hellö there".slice!(1..3).should == "ell" + "hellö there".slice!(1...3).should == "el" + "hellö there".slice!(-4..-2).should == "her" + "hellö there".slice!(-4...-2).should == "he" + "hellö there".slice!(5..-1).should == " there" + "hellö there".slice!(5...-1).should == " ther" end - it "raises a #{frozen_error_class} on a frozen instance that is modified" do - lambda { "hello".freeze.slice!(1..3) }.should raise_error(frozen_error_class) + + it "raises a FrozenError on a frozen instance that is modified" do + -> { "hello".freeze.slice!(1..3) }.should.raise(FrozenError) end # see redmine #1551 - it "raises a #{frozen_error_class} on a frozen instance that would not be modified" do - lambda { "hello".freeze.slice!(10..20)}.should raise_error(frozen_error_class) + it "raises a FrozenError on a frozen instance that would not be modified" do + -> { "hello".freeze.slice!(10..20)}.should.raise(FrozenError) end end @@ -277,39 +254,15 @@ describe "String#slice! with Regexp" do s.should == "this is a string" end - it "always taints resulting strings when self or regexp is tainted" do - strs = ["hello world"] - strs += strs.map { |s| s.dup.taint } - - strs.each do |str| - str = str.dup - str.slice!(//).tainted?.should == str.tainted? - str.slice!(/hello/).tainted?.should == str.tainted? - - tainted_re = /./ - tainted_re.taint - - str.slice!(tainted_re).tainted?.should == true - end - end - - it "doesn't taint self when regexp is tainted" do - s = "hello" - s.slice!(/./.taint) - s.tainted?.should == false - end - - it "returns subclass instances" do + it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(//).should be_an_instance_of(StringSpecs::MyString) - s.slice!(/../).should be_an_instance_of(StringSpecs::MyString) + s.slice!(//).should.instance_of?(String) + s.slice!(/../).should.instance_of?(String) end - with_feature :encoding do - it "returns the matching portion of self with a multi byte character" do - "hëllo there".slice!(/[ë](.)\1/).should == "ëll" - "".slice!(//).should == "" - end + it "returns the matching portion of self with a multi byte character" do + "hëllo there".slice!(/[ë](.)\1/).should == "ëll" + "".slice!(//).should == "" end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -320,12 +273,12 @@ describe "String#slice! with Regexp" do $~.should == nil end - it "raises a #{frozen_error_class} on a frozen instance that is modified" do - lambda { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(frozen_error_class) + it "raises a FrozenError on a frozen instance that is modified" do + -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError) end - it "raises a #{frozen_error_class} on a frozen instance that would not be modified" do - lambda { "this is a string".freeze.slice!(/zzz/) }.should raise_error(frozen_error_class) + it "raises a FrozenError on a frozen instance that would not be modified" do + -> { "this is a string".freeze.slice!(/zzz/) }.should.raise(FrozenError) end end @@ -338,28 +291,6 @@ describe "String#slice! with Regexp, index" do str.should == "ho here" end - it "always taints resulting strings when self or regexp is tainted" do - strs = ["hello world"] - strs += strs.map { |s| s.dup.taint } - - strs.each do |str| - str = str.dup - str.slice!(//, 0).tainted?.should == str.tainted? - str.slice!(/hello/, 0).tainted?.should == str.tainted? - - tainted_re = /(.)(.)(.)/ - tainted_re.taint - - str.slice!(tainted_re, 1).tainted?.should == true - end - end - - it "doesn't taint self when regexp is tainted" do - s = "hello" - s.slice!(/(.)(.)/.taint, 1) - s.tainted?.should == false - end - it "returns nil if there was no match" do s = "this is a string" s.slice!(/x(zzz)/, 1).should == nil @@ -383,22 +314,20 @@ describe "String#slice! with Regexp, index" do "har".slice!(/(.)(.)(.)/, obj).should == "a" end - it "returns subclass instances" do + it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString) - s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString) + s.slice!(/(.)(.)/, 0).should.instance_of?(String) + s.slice!(/(.)(.)/, 1).should.instance_of?(String) end - with_feature :encoding do - it "returns the encoding aware capture for the given index" do - "hår".slice!(/(.)(.)(.)/, 0).should == "hår" - "hår".slice!(/(.)(.)(.)/, 1).should == "h" - "hår".slice!(/(.)(.)(.)/, 2).should == "å" - "hår".slice!(/(.)(.)(.)/, 3).should == "r" - "hår".slice!(/(.)(.)(.)/, -1).should == "r" - "hår".slice!(/(.)(.)(.)/, -2).should == "å" - "hår".slice!(/(.)(.)(.)/, -3).should == "h" - end + it "returns the encoding aware capture for the given index" do + "hår".slice!(/(.)(.)(.)/, 0).should == "hår" + "hår".slice!(/(.)(.)(.)/, 1).should == "h" + "hår".slice!(/(.)(.)(.)/, 2).should == "å" + "hår".slice!(/(.)(.)(.)/, 3).should == "r" + "hår".slice!(/(.)(.)(.)/, -1).should == "r" + "hår".slice!(/(.)(.)(.)/, -2).should == "å" + "hår".slice!(/(.)(.)(.)/, -3).should == "h" end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -412,10 +341,10 @@ describe "String#slice! with Regexp, index" do $~.should == nil end - it "raises a #{frozen_error_class} if self is frozen" do - lambda { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(frozen_error_class) - lambda { "this is a string".freeze.slice!(/zzz/, 0)}.should raise_error(frozen_error_class) - lambda { "this is a string".freeze.slice!(/(.)/, 2)}.should raise_error(frozen_error_class) + it "raises a FrozenError if self is frozen" do + -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError) + -> { "this is a string".freeze.slice!(/zzz/, 0)}.should.raise(FrozenError) + -> { "this is a string".freeze.slice!(/(.)/, 2)}.should.raise(FrozenError) end end @@ -426,21 +355,6 @@ describe "String#slice! with String" do c.should == "he hello" end - it "taints resulting strings when other is tainted" do - strs = ["", "hello world", "hello"] - strs += strs.map { |s| s.dup.taint } - - strs.each do |str| - str = str.dup - strs.each do |other| - other = other.dup - r = str.slice!(other) - - r.tainted?.should == !r.nil? & other.tainted? - end - end - end - it "doesn't set $~" do $~ = nil @@ -458,19 +372,19 @@ describe "String#slice! with String" do o = mock('x') o.should_not_receive(:to_str) - lambda { "hello".slice!(o) }.should raise_error(TypeError) + -> { "hello".slice!(o) }.should.raise(TypeError) end it "returns a subclass instance when given a subclass instance" do s = StringSpecs::MyString.new("el") r = "hello".slice!(s) r.should == "el" - r.should be_an_instance_of(StringSpecs::MyString) + r.should.instance_of?(String) end - it "raises a #{frozen_error_class} if self is frozen" do - lambda { "hello hello".freeze.slice!('llo') }.should raise_error(frozen_error_class) - lambda { "this is a string".freeze.slice!('zzz')}.should raise_error(frozen_error_class) - lambda { "this is a string".freeze.slice!('zzz')}.should raise_error(frozen_error_class) + it "raises a FrozenError if self is frozen" do + -> { "hello hello".freeze.slice!('llo') }.should.raise(FrozenError) + -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError) + -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError) end end |
