diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-23 11:43:37 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-23 11:43:37 -0700 |
commit | dabb318ccb01ea15b9d577bf4f6eb0a05789aee0 (patch) | |
tree | 78e604c59a9ed980d8693c8b58aac913ddfa4d82 | |
parent | 93cf29ce9b2baa072f13da1f0a509a6909998737 (diff) | |
download | chef-dabb318ccb01ea15b9d577bf4f6eb0a05789aee0.tar.gz |
modify attributes and nodes tests for deeply converting_values
slightly more succinct and strict about types, also trying to keep
the attributes tests associated with attributes even though we mix
them all up like crazy anyway...
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 24 | ||||
-rw-r--r-- | spec/unit/node/vivid_mash_spec.rb | 27 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 44 |
3 files changed, 50 insertions, 45 deletions
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index e40f454c0b..00081a9fd9 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -1171,7 +1171,29 @@ describe Chef::Node::Attribute do Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { @attributes.new_key = "new value" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end - end + describe "deeply converting values" do + it "converts values through an array" do + @attributes.default[:foo] = [ { bar: true } ] + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"][0]["bar"]).to be true + end + + it "converts values through nested arrays" do + @attributes.default[:foo] = [ [ { bar: true } ] ] + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0][0].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"][0][0]["bar"]).to be true + end + + it "converts values through nested hashes" do + @attributes.default[:foo] = { baz: { bar: true } } + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"]["baz"].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"]["baz"]["bar"]).to be true + end + end end diff --git a/spec/unit/node/vivid_mash_spec.rb b/spec/unit/node/vivid_mash_spec.rb index 5319ba4a35..206b15ef6c 100644 --- a/spec/unit/node/vivid_mash_spec.rb +++ b/spec/unit/node/vivid_mash_spec.rb @@ -37,6 +37,33 @@ describe Chef::Node::VividMash do expect(root).to receive(:top_level_breadcrumb=).with(key).at_least(:once).and_call_original end + context "#[]=" do + it "deep converts values through arrays" do + allow(root).to receive(:reset_cache) + vivid[:foo] = [ { :bar => true } ] + expect(vivid["foo"].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0].class).to eql(Chef::Node::VividMash) + expect(vivid["foo"][0]["bar"]).to be true + end + + it "deep converts values through nested arrays" do + allow(root).to receive(:reset_cache) + vivid[:foo] = [ [ { :bar => true } ] ] + expect(vivid["foo"].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0][0].class).to eql(Chef::Node::VividMash) + expect(vivid["foo"][0][0]["bar"]).to be true + end + + it "deep converts values through hashes" do + allow(root).to receive(:reset_cache) + vivid[:foo] = { baz: { :bar => true } } + expect(vivid["foo"]).to be_an_instance_of(Chef::Node::VividMash) + expect(vivid["foo"]["baz"]).to be_an_instance_of(Chef::Node::VividMash) + expect(vivid["foo"]["baz"]["bar"]).to be true + end + end + context "#read" do before do # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index d1ad30c47a..2c8fc4408b 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -784,50 +784,6 @@ describe Chef::Node do expect(node["passenger"]["root_path_2"]).to eql("passenger-4.0.57") expect(node[:passenger]["root_path_2"]).to eql("passenger-4.0.57") end - - it "should deep merge array attributes defined as literals" do - node.default["attr_literal_hash"] = { "key" => { "inner" => "value" } } - expect(node[:attr_literal_hash][:key]).not_to be_nil - expect(node[:attr_literal_hash][:key]).to be_a_kind_of(Mash) - expect(node[:attr_literal_hash]["key"]).not_to be_nil - expect(node["attr_literal_hash"]["key"]).not_to be_nil - expect(node["attr_literal_hash"][:key]).not_to be_nil - expect(node[:attr_literal_hash][:key][:inner]).to eql("value") - expect(node[:attr_literal_hash]["key"][:inner]).to eql("value") - expect(node[:attr_literal_hash]["key"]["inner"]).to eql("value") - expect(node[:attr_literal_hash][:key]["inner"]).to eql("value") - expect(node["attr_literal_hash"][:key][:inner]).to eql("value") - expect(node["attr_literal_hash"]["key"][:inner]).to eql("value") - expect(node["attr_literal_hash"]["key"]["inner"]).to eql("value") - expect(node["attr_literal_hash"][:key]["inner"]).to eql("value") - expect(node["attr_literal_hash"][:key]["not_existing"]).to be_nil - - node.default["attr_literal_array"] = [{ "key" => { "inner" => "value" } }] - expect(node[:attr_literal_array]).to be_a_kind_of(Array) - expect(node[:attr_literal_array].first[:key]).not_to be_nil - expect(node[:attr_literal_array].first["key"]).not_to be_nil - expect(node["attr_literal_array"].first["key"]).not_to be_nil - expect(node["attr_literal_array"].first[:key]).not_to be_nil - expect(node[:attr_literal_array].first[:key][:inner]).to eql("value") - expect(node[:attr_literal_array].first["key"][:inner]).to eql("value") - expect(node[:attr_literal_array].first["key"]["inner"]).to eql("value") - expect(node[:attr_literal_array].first[:key]["inner"]).to eql("value") - expect(node["attr_literal_array"].first[:key][:inner]).to eql("value") - expect(node["attr_literal_array"].first["key"][:inner]).to eql("value") - expect(node["attr_literal_array"].first["key"]["inner"]).to eql("value") - expect(node["attr_literal_array"].first[:key]["inner"]).to eql("value") - expect(node["attr_literal_array"].first[:key]["not_existing"]).to be_nil - - node.default["nested_array_literal"] = [[ { "key" => "value" } ], [ { "key" => "value" } ]] - expect(node[:nested_array_literal]).to be_a_kind_of(Array) - expect(node[:nested_array_literal].first).to be_a_kind_of(Array) - expect(node[:nested_array_literal].last).to be_a_kind_of(Array) - expect(node[:nested_array_literal].first.first[:key]).not_to be_nil - expect(node[:nested_array_literal].first.first["key"]).to eql("value") - expect(node[:nested_array_literal].first.last[:key]).not_to be_nil - expect(node[:nested_array_literal].first.last["key"]).to eql("value") - expect(node[:nested_array_literal].first.last[:key]["not_existing"]).to be_nil - end end it "should raise an ArgumentError if you ask for an attribute that doesn't exist via method_missing" do |