2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
describe 'String#-@' do
|
|
|
|
it 'returns self if the String is frozen' do
|
|
|
|
input = 'foo'.freeze
|
|
|
|
output = -input
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2018-06-13 17:41:45 -04:00
|
|
|
output.should equal(input)
|
2018-04-28 15:50:06 -04:00
|
|
|
output.frozen?.should == true
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
it 'returns a frozen copy if the String is not frozen' do
|
|
|
|
input = 'foo'
|
|
|
|
output = -input
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
output.frozen?.should == true
|
2018-06-13 17:41:45 -04:00
|
|
|
output.should_not equal(input)
|
2018-04-28 15:50:06 -04:00
|
|
|
output.should == 'foo'
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
ruby_version_is "2.5" do
|
|
|
|
it "returns the same object for equal unfrozen strings" do
|
|
|
|
origin = "this is a string"
|
|
|
|
dynamic = %w(this is a string).join(' ')
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
origin.should_not equal(dynamic)
|
|
|
|
(-origin).should equal(-dynamic)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the same object when it's called on the same String literal" do
|
|
|
|
(-"unfrozen string").should equal(-"unfrozen string")
|
|
|
|
(-"unfrozen string").should_not equal(-"another unfrozen string")
|
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is "2.5"..."2.6" do
|
|
|
|
it "does not deduplicate already frozen strings" do
|
|
|
|
dynamic = %w(this string is frozen).join(' ').freeze
|
|
|
|
|
|
|
|
dynamic.should_not equal("this string is frozen".freeze)
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2018-06-13 17:41:45 -04:00
|
|
|
(-dynamic).should_not equal("this string is frozen".freeze)
|
|
|
|
(-dynamic).should_not equal(-"this string is frozen".freeze)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is "2.6" do
|
2018-06-04 19:26:03 -04:00
|
|
|
it "deduplicates frozen strings" do
|
2018-04-28 15:50:06 -04:00
|
|
|
dynamic = %w(this string is frozen).join(' ').freeze
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
dynamic.should_not equal("this string is frozen".freeze)
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2018-06-04 19:26:03 -04:00
|
|
|
(-dynamic).should equal("this string is frozen".freeze)
|
|
|
|
(-dynamic).should equal(-"this string is frozen".freeze)
|
2018-01-29 11:08:16 -05:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|