summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-18 13:29:47 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-19 10:53:36 -0700
commit86801b580101315706b1b02a00a36840eabd75cd (patch)
tree9d0db179dabc6b113ff63965df0f01a2e2dadeb2 /spec
parentbe23b8423ba77a5935586e277bf543cd54b9dec7 (diff)
downloadpuppet-86801b580101315706b1b02a00a36840eabd75cd.tar.gz
puppet-86801b580101315706b1b02a00a36840eabd75cd.tar.xz
puppet-86801b580101315706b1b02a00a36840eabd75cd.zip
(#7013) Support 'when_rendering' and 'render_as' in actions.
These define the API used by folks writing actions that supports their rendering hooks. 'when_rendering' defines a helper method on the interface, which runs the users code in their expected context. 'render_as' just sets the default rendering format; by default this is :for_humans. Reviewed-By: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/interface/action_builder_spec.rb194
-rwxr-xr-xspec/unit/interface/action_spec.rb9
2 files changed, 158 insertions, 45 deletions
diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb
index 8f8e8d1d5..38a23a64e 100755
--- a/spec/unit/interface/action_builder_spec.rb
+++ b/spec/unit/interface/action_builder_spec.rb
@@ -1,81 +1,185 @@
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/interface/action_builder'
+require 'puppet/network/format_handler'
describe Puppet::Interface::ActionBuilder do
- describe "::build" do
- it "should build an action" do
- action = Puppet::Interface::ActionBuilder.build(nil, :foo) do
+ let :face do Puppet::Interface.new(:puppet_interface_actionbuilder, '0.0.1') end
+
+ it "should build an action" do
+ action = Puppet::Interface::ActionBuilder.build(nil, :foo) do
+ end
+ action.should be_a(Puppet::Interface::Action)
+ action.name.should == :foo
+ end
+
+ it "should define a method on the face which invokes the action" do
+ face = Puppet::Interface.new(:action_builder_test_interface, '0.0.1') do
+ action(:foo) { when_invoked { "invoked the method" } }
+ end
+
+ face.foo.should == "invoked the method"
+ end
+
+ it "should require a block" do
+ expect { Puppet::Interface::ActionBuilder.build(nil, :foo) }.
+ should raise_error("Action :foo must specify a block")
+ end
+
+ describe "when handling options" do
+ it "should have a #option DSL function" do
+ method = nil
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ method = self.method(:option)
end
- action.should be_a(Puppet::Interface::Action)
- action.name.should == :foo
+ method.should be
end
- it "should define a method on the face which invokes the action" do
- face = Puppet::Interface.new(:action_builder_test_interface, '0.0.1') do
- action(:foo) { when_invoked { "invoked the method" } }
+ it "should define an option without a block" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ option "--bar"
+ end
+ action.should be_option :bar
+ end
+
+ it "should accept an empty block" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ option "--bar" do
+ # This space left deliberately blank.
+ end
end
+ action.should be_option :bar
+ end
+ end
- face.foo.should == "invoked the method"
+ context "inline documentation" do
+ it "should set the summary" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ summary "this is some text"
+ end
+ action.summary.should == "this is some text"
end
+ end
- it "should require a block" do
- expect { Puppet::Interface::ActionBuilder.build(nil, :foo) }.
- should raise_error("Action :foo must specify a block")
+ context "action defaulting" do
+ it "should set the default to true" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ default
+ end
+ action.default.should be_true
end
- describe "when handling options" do
- let :face do Puppet::Interface.new(:option_handling, '0.0.1') end
+ it "should not be default by, er, default. *cough*" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do end
+ action.default.should be_false
+ end
+ end
- it "should have a #option DSL function" do
- method = nil
+ context "#when_rendering" do
+ it "should fail if no rendering format is given" do
+ expect {
Puppet::Interface::ActionBuilder.build(face, :foo) do
- method = self.method(:option)
+ when_rendering do true end
end
- method.should be
- end
+ }.to raise_error ArgumentError, /must give a rendering format to when_rendering/
+ end
- it "should define an option without a block" do
- action = Puppet::Interface::ActionBuilder.build(face, :foo) do
- option "--bar"
+ it "should fail if no block is given" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json
end
- action.should be_option :bar
- end
+ }.to raise_error ArgumentError, /must give a block to when_rendering/
+ end
- it "should accept an empty block" do
- action = Puppet::Interface::ActionBuilder.build(face, :foo) do
- option "--bar" do
- # This space left deliberately blank.
- end
+ it "should fail if the block takes no arguments" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do true end
end
- action.should be_option :bar
- end
+ }.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
end
- context "inline documentation" do
- let :face do Puppet::Interface.new(:inline_action_docs, '0.0.1') end
+ it "should fail if the block takes more than one argument" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do |a, b, c| true end
+ end
+ }.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
+ end
- it "should set the summary" do
- action = Puppet::Interface::ActionBuilder.build(face, :foo) do
- summary "this is some text"
+ it "should fail if the block takes a variable number of arguments" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do |*args| true end
+ end
+ }.to raise_error(ArgumentError,
+ /when_rendering methods take one argument, the result, not/)
+ end
+
+ it "should stash a rendering block" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do |a| true end
+ end
+ action.when_rendering(:json).should be_an_instance_of UnboundMethod
+ end
+
+ it "should fail if you try to set the same rendering twice" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do |a| true end
+ when_rendering :json do |a| true end
end
- action.summary.should == "this is some text"
+ }.to raise_error ArgumentError, /You can't define a rendering method for json twice/
+ end
+
+ it "should work if you set two different renderings" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ when_rendering :json do |a| true end
+ when_rendering :yaml do |a| true end
end
+ action.when_rendering(:json).should be_an_instance_of UnboundMethod
+ action.when_rendering(:yaml).should be_an_instance_of UnboundMethod
end
+ end
- context "action defaulting" do
- let :face do Puppet::Interface.new(:default_action, '0.0.1') end
+ context "#render_as" do
+ it "should default to :for_humans" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do end
+ action.render_as.should == :for_humans
+ end
+
+ it "should fail if not rendering format is given" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ render_as
+ end
+ }.to raise_error ArgumentError, /must give a rendering format to render_as/
+ end
- it "should set the default to true" do
+ Puppet::Network::FormatHandler.formats.each do |name|
+ it "should accept #{name.inspect} format" do
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
- default
+ render_as name
end
- action.default.should be_true
+ action.render_as.should == name
end
+ end
- it "should not be default by, er, default. *cough*" do
- action = Puppet::Interface::ActionBuilder.build(face, :foo) do end
- action.default.should be_false
+ it "should accept :for_humans format" do
+ action = Puppet::Interface::ActionBuilder.build(face, :foo) do
+ render_as :for_humans
+ end
+ action.render_as.should == :for_humans
+ end
+
+ [:if_you_define_this_format_you_frighten_me, "json", 12].each do |input|
+ it "should fail if given #{input.inspect}" do
+ expect {
+ Puppet::Interface::ActionBuilder.build(face, :foo) do
+ render_as input
+ end
+ }.to raise_error ArgumentError, /#{input.inspect} is not a valid rendering format/
end
end
end
diff --git a/spec/unit/interface/action_spec.rb b/spec/unit/interface/action_spec.rb
index fe2409b35..07853e7b7 100755
--- a/spec/unit/interface/action_spec.rb
+++ b/spec/unit/interface/action_spec.rb
@@ -371,4 +371,13 @@ describe Puppet::Interface::Action do
end
end
end
+
+ context "#when_rendering" do
+ it "should fail if no type is given when_rendering"
+ it "should accept a when_rendering block"
+ it "should accept multiple when_rendering blocks"
+ it "should fail if when_rendering gets a non-symbol identifier"
+ it "should fail if a second block is given for the same type"
+ it "should return the block if asked"
+ end
end