diff options
Diffstat (limited to 'spec/ruby/core/string/shared/concat.rb')
| -rw-r--r-- | spec/ruby/core/string/shared/concat.rb | 75 |
1 files changed, 36 insertions, 39 deletions
diff --git a/spec/ruby/core/string/shared/concat.rb b/spec/ruby/core/string/shared/concat.rb index d6ffad7d4d..60cd0e12d4 100644 --- a/spec/ruby/core/string/shared/concat.rb +++ b/spec/ruby/core/string/shared/concat.rb @@ -1,54 +1,31 @@ +# frozen_string_literal: false describe :string_concat, shared: true do it "concatenates the given argument to self and returns self" do str = 'hello ' - str.send(@method, 'world').should equal(str) + str.send(@method, 'world').should.equal?(str) str.should == "hello world" end - it "converts the given argument to a String using to_str" do - obj = mock('world!') - obj.should_receive(:to_str).and_return("world!") - a = 'hello '.send(@method, obj) - a.should == 'hello world!' - end - - it "raises a TypeError if the given argument can't be converted to a String" do - -> { 'hello '.send(@method, []) }.should raise_error(TypeError) - -> { 'hello '.send(@method, mock('x')) }.should raise_error(TypeError) - end - it "raises a FrozenError when self is frozen" do a = "hello" a.freeze - -> { a.send(@method, "") }.should raise_error(FrozenError) - -> { a.send(@method, "test") }.should raise_error(FrozenError) + -> { a.send(@method, "") }.should.raise(FrozenError) + -> { a.send(@method, "test") }.should.raise(FrozenError) end it "returns a String when given a subclass instance" do a = "hello" a.send(@method, StringSpecs::MyString.new(" world")) a.should == "hello world" - a.should be_an_instance_of(String) + a.should.instance_of?(String) end it "returns an instance of same class when called on a subclass" do str = StringSpecs::MyString.new("hello") str.send(@method, " world") str.should == "hello world" - str.should be_an_instance_of(StringSpecs::MyString) - end - - ruby_version_is ''...'2.7' do - it "taints self if other is tainted" do - "x".send(@method, "".taint).should.tainted? - "x".send(@method, "y".taint).should.tainted? - end - - it "untrusts self if other is untrusted" do - "x".send(@method, "".untrust).should.untrusted? - "x".send(@method, "y".untrust).should.untrusted? - end + str.should.instance_of?(StringSpecs::MyString) end describe "with Integer" do @@ -73,28 +50,28 @@ describe :string_concat, shared: true do end it "raises RangeError if the argument is an invalid codepoint for self's encoding" do - -> { "".encode(Encoding::US_ASCII).send(@method, 256) }.should raise_error(RangeError) - -> { "".encode(Encoding::EUC_JP).send(@method, 0x81) }.should raise_error(RangeError) + -> { "".encode(Encoding::US_ASCII).send(@method, 256) }.should.raise(RangeError) + -> { "".encode(Encoding::EUC_JP).send(@method, 0x81) }.should.raise(RangeError) end it "raises RangeError if the argument is negative" do - -> { "".send(@method, -200) }.should raise_error(RangeError) - -> { "".send(@method, -bignum_value) }.should raise_error(RangeError) + -> { "".send(@method, -200) }.should.raise(RangeError) + -> { "".send(@method, -bignum_value) }.should.raise(RangeError) end it "doesn't call to_int on its argument" do x = mock('x') x.should_not_receive(:to_int) - -> { "".send(@method, x) }.should raise_error(TypeError) + -> { "".send(@method, x) }.should.raise(TypeError) end it "raises a FrozenError when self is frozen" do a = "hello" a.freeze - -> { a.send(@method, 0) }.should raise_error(FrozenError) - -> { a.send(@method, 33) }.should raise_error(FrozenError) + -> { a.send(@method, 0) }.should.raise(FrozenError) + -> { a.send(@method, 33) }.should.raise(FrozenError) end end end @@ -114,7 +91,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are empty" do - -> { "x".encode("UTF-16LE").send(@method, "y".encode("UTF-8")) }.should raise_error(Encoding::CompatibilityError) + -> { "x".encode("UTF-16LE").send(@method, "y".encode("UTF-8")) }.should.raise(Encoding::CompatibilityError) end end @@ -132,7 +109,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are empty" do - -> { "x".encode("UTF-8").send(@method, "y".encode("UTF-16LE")) }.should raise_error(Encoding::CompatibilityError) + -> { "x".encode("UTF-8").send(@method, "y".encode("UTF-16LE")) }.should.raise(Encoding::CompatibilityError) end end @@ -150,7 +127,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are ASCII-only" do - -> { "\u00E9".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")) }.should raise_error(Encoding::CompatibilityError) + -> { "\u00E9".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")) }.should.raise(Encoding::CompatibilityError) end end @@ -160,3 +137,23 @@ describe :string_concat_encoding, shared: true do end end end + +describe :string_concat_type_coercion, shared: true do + it "converts the given argument to a String using to_str" do + obj = mock('world!') + obj.should_receive(:to_str).and_return("world!") + a = 'hello '.send(@method, obj) + a.should == 'hello world!' + end + + it "raises a TypeError if the given argument can't be converted to a String" do + -> { 'hello '.send(@method, []) }.should.raise(TypeError) + -> { 'hello '.send(@method, mock('x')) }.should.raise(TypeError) + end + + it "raises a NoMethodError if the given argument raises a NoMethodError during type coercion to a String" do + obj = mock('world!') + obj.should_receive(:to_str).and_raise(NoMethodError) + -> { 'hello '.send(@method, obj) }.should.raise(NoMethodError) + end +end |
