1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
require_relative '../../spec_helper'
describe "chilled String" do
guard -> { ruby_version_is "3.4" and !"test".equal?("test") } do
describe "chilled string literals" do
describe "#frozen?" do
it "returns false" do
"chilled".frozen?.should == false
end
end
describe "#-@" do
it "returns a different instance" do
input = "chilled"
interned = (-input)
interned.frozen?.should == true
interned.object_id.should_not == input.object_id
end
end
describe "#+@" do
it "returns a different instance" do
input = "chilled"
duped = (+input)
duped.frozen?.should == false
duped.object_id.should_not == input.object_id
end
end
describe "#clone" do
it "preserves chilled status" do
input = "chilled".clone
-> {
input << "-mutated"
}.should complain(/literal string will be frozen in the future/)
input.should == "chilled-mutated"
end
end
describe "mutation" do
it "emits a warning" do
input = "chilled"
-> {
input << "-mutated"
}.should complain(/literal string will be frozen in the future/)
input.should == "chilled-mutated"
end
it "emits a warning on singleton_class creation" do
-> {
"chilled".singleton_class
}.should complain(/literal string will be frozen in the future/)
end
it "emits a warning on instance variable assignment" do
-> {
"chilled".instance_variable_set(:@ivar, 42)
}.should complain(/literal string will be frozen in the future/)
end
it "raises FrozenError after the string was explicitly frozen" do
input = "chilled"
input.freeze
-> {
-> {
input << "mutated"
}.should raise_error(FrozenError)
}.should_not complain(/literal string will be frozen in the future/)
end
end
end
describe "chilled strings returned by Symbol#to_s" do
describe "#frozen?" do
it "returns false" do
:chilled.to_s.frozen?.should == false
end
end
describe "#-@" do
it "returns a different instance" do
input = :chilled.to_s
interned = (-input)
interned.frozen?.should == true
interned.object_id.should_not == input.object_id
end
end
describe "#+@" do
it "returns a different instance" do
input = :chilled.to_s
duped = (+input)
duped.frozen?.should == false
duped.object_id.should_not == input.object_id
end
end
describe "#clone" do
it "preserves chilled status" do
input = :chilled.to_s.clone
-> {
input << "-mutated"
}.should complain(/string returned by :chilled\.to_s will be frozen in the future/)
input.should == "chilled-mutated"
end
end
describe "mutation" do
it "emits a warning" do
input = :chilled.to_s
-> {
input << "-mutated"
}.should complain(/string returned by :chilled\.to_s will be frozen in the future/)
input.should == "chilled-mutated"
end
it "emits a warning on singleton_class creation" do
-> {
:chilled.to_s.singleton_class
}.should complain(/string returned by :chilled\.to_s will be frozen in the future/)
end
it "emits a warning on instance variable assignment" do
-> {
:chilled.to_s.instance_variable_set(:@ivar, 42)
}.should complain(/string returned by :chilled\.to_s will be frozen in the future/)
end
it "raises FrozenError after the string was explicitly frozen" do
input = :chilled.to_s
input.freeze
-> {
-> {
input << "mutated"
}.should raise_error(FrozenError)
}.should_not complain(/string returned by :chilled\.to_s will be frozen in the future/)
end
end
end
end
end
|