diff options
author | Vivek Singh <vivek.singh@msystechnologies.com> | 2019-09-28 14:15:51 +0530 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2019-10-02 11:00:13 -0700 |
commit | 39cba20eaed01ee6202b6ab1cce6b4d23ba60c67 (patch) | |
tree | 56a591b32452719ac1b98603a7cc3cc8496cbdf7 | |
parent | 6417f07ff9b42eb437acf956cf9cfd393de1c755 (diff) | |
download | chef-39cba20eaed01ee6202b6ab1cce6b4d23ba60c67.tar.gz |
Refactor the specs with shared_examples
- Add specs for Node::Attribute to_json & to_yaml methods.
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 52 | ||||
-rw-r--r-- | spec/unit/node/immutable_collections_spec.rb | 240 |
2 files changed, 122 insertions, 170 deletions
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index 36827215f0..9b1687305f 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -1273,4 +1273,56 @@ describe Chef::Node::Attribute do expect { @attributes["foo"]["bar"][0] << "buzz" }.to raise_error(RuntimeError, "can't modify frozen String") end end + + describe "deep merging with nils" do + it "nils when deep merging between default levels knocks out values" do + @attributes.default["foo"] = "bar" + expect(@attributes["foo"]).to eql("bar") + @attributes.force_default["foo"] = nil + expect(@attributes["foo"]).to be nil + end + + it "nils when deep merging between override levels knocks out values" do + @attributes.override["foo"] = "bar" + expect(@attributes["foo"]).to eql("bar") + @attributes.force_override["foo"] = nil + expect(@attributes["foo"]).to be nil + end + + it "nils when deep merging between default+override levels knocks out values" do + @attributes.default["foo"] = "bar" + expect(@attributes["foo"]).to eql("bar") + @attributes.override["foo"] = nil + expect(@attributes["foo"]).to be nil + end + + it "nils when deep merging between normal+automatic levels knocks out values" do + @attributes.normal["foo"] = "bar" + expect(@attributes["foo"]).to eql("bar") + @attributes.automatic["foo"] = nil + expect(@attributes["foo"]).to be nil + end + end + + describe "to_json" do + it "should convert to a valid json string" do + json = @attributes["hot"].to_json + expect { JSON.parse(json) }.not_to raise_error + end + + it "should convert to a json based on current state" do + expect(@attributes["hot"].to_json).to eq("{\"day\":\"sunday\"}") + end + end + + describe "to_yaml" do + it "should convert to a valid yaml format" do + json = @attributes["hot"].to_yaml + expect { YAML.parse(json) }.not_to raise_error + end + + it "should convert to a yaml based on current state" do + expect(@attributes["hot"].to_yaml).to eq("---\nday: sunday\n") + end + end end diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb index c794553ab7..1d1d5a40ff 100644 --- a/spec/unit/node/immutable_collections_spec.rb +++ b/spec/unit/node/immutable_collections_spec.rb @@ -19,6 +19,64 @@ require "spec_helper" require "chef/node/immutable_collections" +shared_examples_for "ImmutableMash module" do |param| + let(:copy) { @immutable_mash.send(param) } + + it "converts an immutable mash to a new mutable hash" do + expect(copy).to be_is_a(Hash) + end + + it "converts an immutable nested mash to a new mutable hash" do + expect(copy["top_level_4"]["level2"]).to be_is_a(Hash) + 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 + +shared_examples_for "ImmutableArray module" do |param| + let(:copy) { @immutable_nested_array.send(param) } + + 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_is_a(Hash) + 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 + +shared_examples_for "Immutable#to_yaml" do + it "converts an immutable array to a new valid YAML mutable string" do + expect { YAML.parse(copy) }.not_to raise_error + end + + it "should create a YAML string with content" do + expect(copy).to eq(parsed_yaml) + end +end + describe Chef::Node::ImmutableMash do before do @data_in = { "top" => { "second_level" => "some value" }, @@ -67,96 +125,17 @@ describe Chef::Node::ImmutableMash do expect(@mash["test2"]).to eql("bar") end - describe "to_hash" do - before do - @copy = @immutable_mash.to_hash - end - - it "converts an immutable mash to a new mutable hash" do - expect(@copy).to be_instance_of(Hash) - end - - it "converts an immutable nested mash to a new mutable hash" do - expect(@copy["top_level_4"]["level2"]).to be_instance_of(Hash) - 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 "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 - end - - it "converts an immutable mash to a new mutable hash" do - expect(@copy).to be_instance_of(Hash) - end - - it "converts an immutable nested mash to a new mutable hash" do - expect(@copy["top_level_4"]["level2"]).to be_instance_of(Hash) - 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 + %w{to_h to_hash dup}.each do |immutable_meth| + describe "#{immutable_meth}" do + include_examples "ImmutableMash module", description end end describe "to_yaml" do - before do - @copy = @immutable_mash.to_yaml - end + let(:copy) { @immutable_mash.to_yaml } + let(:parsed_yaml) { "---\ntop:\n second_level: some value\ntop_level_2:\n- array\n- of\n- values\ntop_level_3:\n- hash_array: 1\n hash_array_b: 2\ntop_level_4:\n level2:\n key: value\n" } - it "converts an immutable mash to a new mutable YAML formatted string" do - expect(@copy).to be_instance_of(String) - end - - it "should create a YAML formatted string with the content" do - expect(@copy).to eq("---\ntop:\n second_level: some value\ntop_level_2:\n- array\n- of\n- values\ntop_level_3:\n- hash_array: 1\n hash_array_b: 2\ntop_level_4:\n level2:\n key: value\n") - end + include_examples "Immutable#to_yaml" end %i{ @@ -255,96 +234,17 @@ describe Chef::Node::ImmutableArray do expect(mutable[0]).to eq(:value) end - describe "to_a" do - before do - @copy = @immutable_nested_array.to_a - 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(Hash) - 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 "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 - 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(Hash) - 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 + %w{to_a to_array dup}.each do |immutable_meth| + describe "#{immutable_meth}" do + include_examples "ImmutableArray module", description end end describe "to_yaml" do - before do - @copy = @immutable_nested_array.to_yaml - end + let(:copy) { @immutable_nested_array.to_yaml } + let(:parsed_yaml) { "---\n- level1\n- - foo\n - bar\n - baz\n - 1\n - 2\n - 3\n - \n - true\n - false\n - - el\n - 0\n - \n- m: m\n" } - it "converts an immutable array to a new YAML formatted mutable string" do - expect(@copy).to be_instance_of(String) - end - - it "should create a YAML formatted string with content" do - expect(@copy).to eq("---\n- level1\n- - foo\n - bar\n - baz\n - 1\n - 2\n - 3\n - \n - true\n - false\n - - el\n - 0\n - \n- m: m\n") - end + include_examples "Immutable#to_yaml" end describe "#[]" do |