diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 18 | ||||
-rw-r--r-- | spec/unit/node/immutable_collections_spec.rb | 52 |
2 files changed, 70 insertions, 0 deletions
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index 25594ca20b..0869b83e43 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -483,6 +483,24 @@ describe Chef::Node::Attribute do @attributes.default[:foo] = %w{foo bar baz} + Array(1..3) + [nil, true, false, [ "el", 0, nil ] ] @attributes.default[:foo].dup end + + it "mutating strings should not mutate the attributes in a hash" do + @attributes.default["foo"]["bar"]["baz"] = "fizz" + hash = @attributes["foo"].dup + expect(hash).to eql({ "bar" => { "baz" => "fizz" } }) + hash["bar"]["baz"] << "buzz" + expect(hash).to eql({ "bar" => { "baz" => "fizzbuzz" } }) + expect(@attributes.default["foo"]).to eql({ "bar" => { "baz" => "fizz" } }) + end + + it "mutating array elements should not mutate the attributes" do + @attributes.default["foo"]["bar"] = [ "fizz" ] + hash = @attributes["foo"].dup + expect(hash).to eql({ "bar" => [ "fizz" ] }) + hash["bar"][0] << "buzz" + expect(hash).to eql({ "bar" => [ "fizzbuzz" ] }) + expect(@attributes.default["foo"]).to eql({ "bar" => [ "fizz" ] }) + end end describe "has_key?" do diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb index a8078f1093..520bc1ba42 100644 --- a/spec/unit/node/immutable_collections_spec.rb +++ b/spec/unit/node/immutable_collections_spec.rb @@ -80,6 +80,32 @@ describe Chef::Node::ImmutableMash do end end + describe "dup" do + before do + @copy = @immutable_mash.dup + end + + it "converts an immutable mash to a new mutable hash" do + expect(@copy).to be_instance_of(Mash) + end + + it "converts an immutable nested mash to a new mutable hash" do + expect(@copy["top_level_4"]["level2"]).to be_instance_of(Mash) + end + + it "converts an immutable nested array to a new mutable array" do + expect(@copy["top_level_2"]).to be_instance_of(Array) + end + + it "should create a mash with the same content" do + expect(@copy).to eq(@immutable_mash) + end + + it "should allow mutation" do + expect { @copy["m"] = "m" }.not_to raise_error + end + end + describe "to_h" do before do @copy = @immutable_mash.to_h @@ -223,6 +249,32 @@ describe Chef::Node::ImmutableArray do end end + describe "dup" do + before do + @copy = @immutable_nested_array.dup + end + + it "converts an immutable array to a new mutable array" do + expect(@copy).to be_instance_of(Array) + end + + it "converts an immutable nested array to a new mutable array" do + expect(@copy[1]).to be_instance_of(Array) + end + + it "converts an immutable nested mash to a new mutable hash" do + expect(@copy[2]).to be_instance_of(Mash) + end + + it "should create an array with the same content" do + expect(@copy).to eq(@immutable_nested_array) + end + + it "should allow mutation" do + expect { @copy << "m" }.not_to raise_error + end + end + describe "to_array" do before do @copy = @immutable_nested_array.to_array |