1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/core/string/shared/concat.rb
Jeremy Evans ffd0820ab3 Deprecate taint/trust and related methods, and make the methods no-ops
This removes the related tests, and puts the related specs behind
version guards.  This affects all code in lib, including some
libraries that may want to support older versions of Ruby.
2019-11-18 01:00:25 +02:00

162 lines
5.9 KiB
Ruby

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.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 #{frozen_error_class} when self is frozen" do
a = "hello"
a.freeze
-> { a.send(@method, "") }.should raise_error(frozen_error_class)
-> { a.send(@method, "test") }.should raise_error(frozen_error_class)
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)
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).tainted?.should == true
"x".send(@method, "y".taint).tainted?.should == true
end
it "untrusts self if other is untrusted" do
"x".send(@method, "".untrust).untrusted?.should == true
"x".send(@method, "y".untrust).untrusted?.should == true
end
end
describe "with Integer" do
it "concatenates the argument interpreted as a codepoint" do
b = "".send(@method, 33)
b.should == "!"
b.encode!(Encoding::UTF_8)
b.send(@method, 0x203D)
b.should == "!\u203D"
end
# #5855
it "returns a BINARY string if self is US-ASCII and the argument is between 128-255 (inclusive)" do
a = ("".encode(Encoding::US_ASCII).send(@method, 128))
a.encoding.should == Encoding::BINARY
a.should == 128.chr
a = ("".encode(Encoding::US_ASCII).send(@method, 255))
a.encoding.should == Encoding::BINARY
a.should == 255.chr
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)
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)
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)
end
it "raises a #{frozen_error_class} when self is frozen" do
a = "hello"
a.freeze
-> { a.send(@method, 0) }.should raise_error(frozen_error_class)
-> { a.send(@method, 33) }.should raise_error(frozen_error_class)
end
end
end
describe :string_concat_encoding, shared: true do
describe "when self is in an ASCII-incompatible encoding incompatible with the argument's encoding" do
it "uses self's encoding if both are empty" do
"".encode("UTF-16LE").send(@method, "").encoding.should == Encoding::UTF_16LE
end
it "uses self's encoding if the argument is empty" do
"x".encode("UTF-16LE").send(@method, "").encoding.should == Encoding::UTF_16LE
end
it "uses the argument's encoding if self is empty" do
"".encode("UTF-16LE").send(@method, "x".encode("UTF-8")).encoding.should == Encoding::UTF_8
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)
end
end
describe "when the argument is in an ASCII-incompatible encoding incompatible with self's encoding" do
it "uses self's encoding if both are empty" do
"".encode("UTF-8").send(@method, "".encode("UTF-16LE")).encoding.should == Encoding::UTF_8
end
it "uses self's encoding if the argument is empty" do
"x".encode("UTF-8").send(@method, "".encode("UTF-16LE")).encoding.should == Encoding::UTF_8
end
it "uses the argument's encoding if self is empty" do
"".encode("UTF-8").send(@method, "x".encode("UTF-16LE")).encoding.should == Encoding::UTF_16LE
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)
end
end
describe "when self and the argument are in different ASCII-compatible encodings" do
it "uses self's encoding if both are ASCII-only" do
"abc".encode("UTF-8").send(@method, "123".encode("SHIFT_JIS")).encoding.should == Encoding::UTF_8
end
it "uses self's encoding if the argument is ASCII-only" do
"\u00E9".encode("UTF-8").send(@method, "123".encode("ISO-8859-1")).encoding.should == Encoding::UTF_8
end
it "uses the argument's encoding if self is ASCII-only" do
"abc".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")).encoding.should == Encoding::ISO_8859_1
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)
end
end
describe "when self is BINARY and argument is US-ASCII" do
it "uses BINARY encoding" do
"abc".encode("BINARY").send(@method, "123".encode("US-ASCII")).encoding.should == Encoding::BINARY
end
end
end