diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-19 10:59:29 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-19 10:59:29 -0700 |
| commit | 7438723f6ae13605a48c5db63839a829a19f5127 (patch) | |
| tree | 723eaa606df756ae160c3f8512ccb6d5f5c0ee3d /spec/unit/interface | |
| parent | a594563919a2342e1ea542f8f18ed187ab9ecad3 (diff) | |
| parent | 0fed94fbbad45388c1f9d2451d946681d80643f8 (diff) | |
| download | puppet-7438723f6ae13605a48c5db63839a829a19f5127.tar.gz puppet-7438723f6ae13605a48c5db63839a829a19f5127.tar.xz puppet-7438723f6ae13605a48c5db63839a829a19f5127.zip | |
Merge branch 'bug/2.7.x/6752-allow-action-specific-render-methods'
Fix the conflicts over changes in my previous commit.
Diffstat (limited to 'spec/unit/interface')
| -rwxr-xr-x | spec/unit/interface/action_builder_spec.rb | 198 | ||||
| -rwxr-xr-x | spec/unit/interface/action_spec.rb | 9 |
2 files changed, 166 insertions, 41 deletions
diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb index 50784cc40..bf7afa74e 100755 --- a/spec/unit/interface/action_builder_spec.rb +++ b/spec/unit/interface/action_builder_spec.rb @@ -1,76 +1,192 @@ #!/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 + method.should be + end + + 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_a(Puppet::Interface::Action) - action.name.should == :foo + action.should be_option :bar end + 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" } } + 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 - face.foo.should == "invoked the method" + 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 - it "should require a block" do - expect { Puppet::Interface::ActionBuilder.build(nil, :foo) }. - should raise_error("Action :foo must specify a block") + 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 - describe "when handling options" do - let :face do Puppet::Interface.new(:option_handling, '0.0.1') end + context "#when_rendering" do + it "should fail if no rendering format is given" do + expect { + Puppet::Interface::ActionBuilder.build(face, :foo) do + when_rendering do true end + end + }.to raise_error ArgumentError, /must give a rendering format to when_rendering/ + end - it "should have a #option DSL function" do - method = nil + it "should fail if no block is given" do + expect { Puppet::Interface::ActionBuilder.build(face, :foo) do - method = self.method(:option) + when_rendering :json end - method.should be - end + }.to raise_error ArgumentError, /must give a block 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 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 + }.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/ + 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 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 Method + 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 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.should be_option :bar + }.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 Method + action.when_rendering(:yaml).should be_an_instance_of Method end - context "inline documentation" do - let :face do Puppet::Interface.new(:inline_action_docs, '0.0.1') end + it "should be bound to the face when called" do + action = Puppet::Interface::ActionBuilder.build(face, :foo) do + when_rendering :json do |a| self end + end + action.when_rendering(:json).call(true).should == face + end + end + + context "#render_as" do + it "should default to nil (eg: based on context)" do + action = Puppet::Interface::ActionBuilder.build(face, :foo) do end + action.render_as.should be_nil + 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 summary" do + Puppet::Network::FormatHandler.formats.each do |name| + it "should accept #{name.inspect} format" do action = Puppet::Interface::ActionBuilder.build(face, :foo) do - summary "this is some text" + render_as name end - action.summary.should == "this is some text" + action.render_as.should == name end end - context "action defaulting" do - let :face do Puppet::Interface.new(:default_action, '0.0.1') end + 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 - it "should set the default to true" do - action = Puppet::Interface::ActionBuilder.build(face, :foo) do - default - end - action.default.should == true + [: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 5058ffc7f..0eb450ee2 100755 --- a/spec/unit/interface/action_spec.rb +++ b/spec/unit/interface/action_spec.rb @@ -380,4 +380,13 @@ describe Puppet::Interface::Action do face.get_action(:documentation) 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 |
