diff options
Diffstat (limited to 'spec/ruby/core/string/insert_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/insert_spec.rb | 63 |
1 files changed, 30 insertions, 33 deletions
diff --git a/spec/ruby/core/string/insert_spec.rb b/spec/ruby/core/string/insert_spec.rb index 2c72bb5ffd..483f3c9367 100644 --- a/spec/ruby/core/string/insert_spec.rb +++ b/spec/ruby/core/string/insert_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- - +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -23,8 +23,8 @@ describe "String#insert with index, other" do end it "raises an IndexError if the index is beyond string" do - lambda { "abcd".insert(5, 'X') }.should raise_error(IndexError) - lambda { "abcd".insert(-6, 'X') }.should raise_error(IndexError) + -> { "abcd".insert(5, 'X') }.should raise_error(IndexError) + -> { "abcd".insert(-6, 'X') }.should raise_error(IndexError) end it "converts index to an integer using to_int" do @@ -41,44 +41,41 @@ describe "String#insert with index, other" do "abcd".insert(-3, other).should == "abXYZcd" end - it "taints self if string to insert is tainted" do - str = "abcd" - str.insert(0, "T".taint).tainted?.should == true - - str = "abcd" - other = mock('T') - def other.to_str() "T".taint end - str.insert(0, other).tainted?.should == true - end - it "raises a TypeError if other can't be converted to string" do - lambda { "abcd".insert(-6, Object.new)}.should raise_error(TypeError) - lambda { "abcd".insert(-6, []) }.should raise_error(TypeError) - lambda { "abcd".insert(-6, mock('x')) }.should raise_error(TypeError) + -> { "abcd".insert(-6, Object.new)}.should raise_error(TypeError) + -> { "abcd".insert(-6, []) }.should raise_error(TypeError) + -> { "abcd".insert(-6, mock('x')) }.should raise_error(TypeError) end - it "raises a #{frozen_error_class} if self is frozen" do + it "raises a FrozenError if self is frozen" do str = "abcd".freeze - lambda { str.insert(4, '') }.should raise_error(frozen_error_class) - lambda { str.insert(4, 'X') }.should raise_error(frozen_error_class) + -> { str.insert(4, '') }.should raise_error(FrozenError) + -> { str.insert(4, 'X') }.should raise_error(FrozenError) end - with_feature :encoding do - it "inserts a character into a multibyte encoded string" do - "ありがとう".insert(1, 'ü').should == "あüりがとう" - end + it "inserts a character into a multibyte encoded string" do + "ありがとう".insert(1, 'ü').should == "あüりがとう" + end - it "returns a String in the compatible encoding" do - str = "".force_encoding(Encoding::US_ASCII) - str.insert(0, "ありがとう") - str.encoding.should == Encoding::UTF_8 - end + it "returns a String in the compatible encoding" do + str = "".force_encoding(Encoding::US_ASCII) + str.insert(0, "ありがとう") + str.encoding.should == Encoding::UTF_8 + end + + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + pat = "ア".encode Encoding::EUC_JP + -> do + "あれ".insert 0, pat + end.should raise_error(Encoding::CompatibilityError) + end - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - pat = "ア".encode Encoding::EUC_JP - lambda do - "あれ".insert 0, pat - end.should raise_error(Encoding::CompatibilityError) + it "should not call subclassed string methods" do + cls = Class.new(String) do + def replace(arg) + raise "should not call replace" + end end + cls.new("abcd").insert(0, 'X').should == "Xabcd" end end |
