summaryrefslogtreecommitdiff
path: root/spec/unit/dsl/plugin_spec.rb
diff options
context:
space:
mode:
authormcquin <claire@chef.io>2016-05-07 22:05:35 -0400
committermcquin <claire@chef.io>2016-06-09 09:20:05 -0700
commit01313416df8a3b6c8f48fc71f17c4516bcdef119 (patch)
tree2e61a29bfee6c0b332a4366a4012ec446024228f /spec/unit/dsl/plugin_spec.rb
parent17e5c748a70e5388a542a1cff5f86b630c5a629e (diff)
downloadohai-OHAI-794/set-attribute.tar.gz
Extend set_attribute to set sub-attributes.OHAI-794/set-attribute
Use: - set_attribute(:attr, value) assigns attribute 'attr' value. - set_attribute(:attr, :subattr, value) assigns attribute 'attr/subattr' value. Extends to subattributes of arbitrary depth. Raises a TypeError if some :subattr is not an Array, Hash, or Mash (e.g., a String value).
Diffstat (limited to 'spec/unit/dsl/plugin_spec.rb')
-rw-r--r--spec/unit/dsl/plugin_spec.rb59
1 files changed, 58 insertions, 1 deletions
diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb
index 2a35701c..c37b6fd2 100644
--- a/spec/unit/dsl/plugin_spec.rb
+++ b/spec/unit/dsl/plugin_spec.rb
@@ -90,11 +90,68 @@ shared_examples "Ohai::DSL::Plugin" do
end
end
- context "when setting attributes" do
+ describe "set_attribute" do
+ it "raises an ArgumentError when given one argument" do
+ expect { plugin.set_attribute(:tea) }.to raise_error(ArgumentError)
+ end
+
it "lets you set an attribute" do
plugin.set_attribute(:tea, "is soothing")
expect(plugin.data["tea"]).to eql("is soothing")
end
+
+ it "lets you set an attribute to an empty array" do
+ plugin.set_attribute(:tea, [])
+ expect(plugin.data["tea"]).to eql([])
+ end
+
+ it "lets you set an attribute to an empty Mash" do
+ plugin.set_attribute(:tea, {})
+ expect(plugin.data["tea"]).to eql({})
+ end
+
+ it "lets you modify an attribute" do
+ plugin.data["tea"] = "is soothing"
+ plugin.set_attribute(:tea, "tastes like plants")
+ expect(plugin.data["tea"]).to eql("tastes like plants")
+ end
+
+ it "lets you set a subattribute" do
+ plugin.data["tea"] = Mash.new
+ plugin.set_attribute(:tea, :green, "tastes like green")
+ expect(plugin.data["tea"]["green"]).to eql("tastes like green")
+ end
+
+ it "lets you set a subattribute to an empty array" do
+ plugin.set_attribute(:tea, :green, [])
+ expect(plugin.data["tea"]["green"]).to eql([])
+ end
+
+ it "lets you set a subattribute to an empty Mash" do
+ plugin.set_attribute(:tea, :green, {})
+ expect(plugin.data["tea"]["green"]).to eql({})
+ end
+
+ it "lets you modify a subattribute" do
+ plugin.data["tea"] = Mash.new
+ plugin.data["tea"]["green"] = "tastes like green"
+ plugin.set_attribute(:tea, :green, "made from leaves")
+ expect(plugin.data["tea"]["green"]).to eql("made from leaves")
+ end
+
+ it "raises a TypeError when modifying an attribute that is not a Mash" do
+ plugin.data["tea"] = Mash.new
+ plugin.data["tea"] = "is soothing"
+ expect { plugin.set_attribute(:tea, :green, "tastes like green") }.to raise_error(TypeError)
+ end
+
+ it "lets you modify a subattribute with an array" do
+ green = { flavor: "tastes like green" }
+ black = { flavor: "tastes like black" }
+ plugin.data[:tea] = [green, black]
+ plugin.set_attribute(:tea, 0, :source, "made from leaves")
+ expect(plugin.data[:tea][0][:source]).to eq("made from leaves")
+ end
end
context "when getting attributes" do