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 | |
| 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')
| -rwxr-xr-x | spec/unit/application/face_base_spec.rb | 67 | ||||
| -rwxr-xr-x | spec/unit/face/facts_spec.rb | 6 | ||||
| -rwxr-xr-x | spec/unit/face/help_spec.rb | 2 | ||||
| -rwxr-xr-x | spec/unit/face/node_spec.rb | 4 | ||||
| -rwxr-xr-x | spec/unit/interface/action_builder_spec.rb | 198 | ||||
| -rwxr-xr-x | spec/unit/interface/action_spec.rb | 9 | ||||
| -rwxr-xr-x | spec/unit/interface_spec.rb | 10 |
7 files changed, 231 insertions, 65 deletions
diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb index 7e13d4be6..5403608cf 100755 --- a/spec/unit/application/face_base_spec.rb +++ b/spec/unit/application/face_base_spec.rb @@ -59,10 +59,6 @@ describe Puppet::Application::FaceBase do app.face.name.should == :basetest end - it "should set the format based on the face default" do - app.format.should == :pson - end - it "should find the action" do app.action.should be app.action.name.should == :foo @@ -192,7 +188,6 @@ describe Puppet::Application::FaceBase do app.face = Puppet::Face[:basetest, '0.0.1'] app.action = app.face.get_action(:foo) - app.format = :pson app.arguments = ["myname", "myarg"] end @@ -214,4 +209,66 @@ describe Puppet::Application::FaceBase do app.main end end + + describe "#render" do + before :each do + app.face = Puppet::Face[:basetest, '0.0.1'] + app.action = app.face.get_action(:foo) + end + + ["hello", 1, 1.0].each do |input| + it "should just return a #{input.class.name}" do + app.render(input).should == input + end + end + + [[1, 2], ["one"], [{ 1 => 1 }]].each do |input| + it "should render #{input.class} using the 'pp' library" do + app.render(input).should == input.pretty_inspect + end + end + + it "should render a non-trivially-keyed Hash with the 'pp' library" do + hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 } + app.render(hash).should == hash.pretty_inspect + end + + it "should render a {String,Numeric}-keyed Hash into a table" do + object = Object.new + hash = { "one" => 1, "two" => [], "three" => {}, "four" => object, + 5 => 5, 6.0 => 6 } + + # Gotta love ASCII-betical sort order. Hope your objects are better + # structured for display than my test one is. --daniel 2011-04-18 + app.render(hash).should == <<EOT +5 5 +6.0 6 +four #{object.pretty_inspect.chomp} +one 1 +three {} +two [] +EOT + end + + it "should render a hash nicely with a multi-line value" do + hash = { + "number" => { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 }, + "text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 } + } + app.render(hash).should == <<EOT +number {"1"=>"1111111111111111111111111111111111111111", + "2"=>"2222222222222222222222222222222222222222", + "3"=>"3333333333333333333333333333333333333333"} +text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "c"=>"cccccccccccccccccccccccccccccccccccccccc"} +EOT + end + + it "should invoke the action rendering hook while rendering" do + app.action.set_rendering_method_for(:for_humans, proc { |value| "bi-winning!" }) + app.action.render_as = :for_humans + app.render("bi-polar?").should == "bi-winning!" + end + end end diff --git a/spec/unit/face/facts_spec.rb b/spec/unit/face/facts_spec.rb index e6411f836..6ab6ad5be 100755 --- a/spec/unit/face/facts_spec.rb +++ b/spec/unit/face/facts_spec.rb @@ -2,14 +2,10 @@ require 'spec_helper' describe Puppet::Face[:facts, '0.0.1'] do - it "should define an 'upload' fact" do + it "should define an 'upload' action" do subject.should be_action(:upload) end - it "should set its default format to :yaml" do - subject.default_format.should == :yaml - end - describe "when uploading" do it "should set the terminus_class to :facter" diff --git a/spec/unit/face/help_spec.rb b/spec/unit/face/help_spec.rb index b5205afd6..faa5f9617 100755 --- a/spec/unit/face/help_spec.rb +++ b/spec/unit/face/help_spec.rb @@ -7,7 +7,7 @@ describe Puppet::Face[:help, '0.0.1'] do end it "should have a default action of help" do - pending "REVISIT: we don't support default actions yet" + subject.get_action('help').should be_default end it "should accept a call with no arguments" do diff --git a/spec/unit/face/node_spec.rb b/spec/unit/face/node_spec.rb index 90d258db9..d19312c58 100755 --- a/spec/unit/face/node_spec.rb +++ b/spec/unit/face/node_spec.rb @@ -2,7 +2,5 @@ require 'spec_helper' describe Puppet::Face[:node, '0.0.1'] do - it "should set its default format to :yaml" do - subject.default_format.should == :yaml - end + it "REVISIT: really should have some tests" end 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 diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb index 50ae9c711..b4fef0307 100755 --- a/spec/unit/interface_spec.rb +++ b/spec/unit/interface_spec.rb @@ -93,16 +93,6 @@ describe Puppet::Interface do subject.new(:me, '0.0.1').to_s.should =~ /\bme\b/ end - it "should allow overriding of the default format" do - face = subject.new(:me, '0.0.1') - face.set_default_format :foo - face.default_format.should == :foo - end - - it "should default to :pson for its format" do - subject.new(:me, '0.0.1').default_format.should == :pson - end - # Why? it "should create a class-level autoloader" do subject.autoloader.should be_instance_of(Puppet::Util::Autoload) |
