3
require 'puppet/interface/action_builder'
4
require 'puppet/network/format_handler'
6
describe Puppet::Interface::ActionBuilder do
7
let :face do Puppet::Interface.new(:puppet_interface_actionbuilder, '0.0.1') end
9
it "should build an action" do
10
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
11
when_invoked do |options| true end
13
action.should be_a(Puppet::Interface::Action)
14
action.name.should == :foo
17
it "should define a method on the face which invokes the action" do
18
face = Puppet::Interface.new(:action_builder_test_interface, '0.0.1') do
19
action(:foo) { when_invoked { |options| "invoked the method" } }
22
face.foo.should == "invoked the method"
25
it "should require a block" do
26
expect { Puppet::Interface::ActionBuilder.build(nil, :foo) }.
27
should raise_error("Action :foo must specify a block")
30
it "should require an invocation block" do
32
Puppet::Interface::ActionBuilder.build(face, :foo) {}
33
}.to raise_error(/actions need to know what to do when_invoked; please add the block/)
36
describe "when handling options" do
37
it "should have a #option DSL function" do
39
Puppet::Interface::ActionBuilder.build(face, :foo) do
40
when_invoked do |options| true end
41
method = self.method(:option)
43
method.should be_an_instance_of Method
46
it "should define an option without a block" do
47
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
48
when_invoked do |options| true end
51
action.should be_option :bar
54
it "should accept an empty block" do
55
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
56
when_invoked do |options| true end
58
# This space left deliberately blank.
61
action.should be_option :bar
65
context "inline documentation" do
66
it "should set the summary" do
67
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
68
when_invoked do |options| true end
69
summary "this is some text"
71
action.summary.should == "this is some text"
75
context "action defaulting" do
76
it "should set the default to true" do
77
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
78
when_invoked do |options| true end
81
action.default.should be_true
84
it "should not be default by, er, default. *cough*" do
85
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
86
when_invoked do |options| true end
88
action.default.should be_false
92
context "#when_rendering" do
93
it "should fail if no rendering format is given" do
95
Puppet::Interface::ActionBuilder.build(face, :foo) do
96
when_invoked do |options| true end
97
when_rendering do true end
99
}.to raise_error ArgumentError, /must give a rendering format to when_rendering/
102
it "should fail if no block is given" do
104
Puppet::Interface::ActionBuilder.build(face, :foo) do
105
when_invoked do |options| true end
108
}.to raise_error ArgumentError, /must give a block to when_rendering/
111
it "should fail if the block takes no arguments" do
113
Puppet::Interface::ActionBuilder.build(face, :foo) do
114
when_invoked do |options| true end
115
when_rendering :json do true end
117
}.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
120
it "should fail if the block takes more than one argument" do
122
Puppet::Interface::ActionBuilder.build(face, :foo) do
123
when_invoked do |options| true end
124
when_rendering :json do |a, b, c| true end
126
}.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
129
it "should fail if the block takes a variable number of arguments" do
131
Puppet::Interface::ActionBuilder.build(face, :foo) do
132
when_invoked do |options| true end
133
when_rendering :json do |*args| true end
135
}.to raise_error(ArgumentError,
136
/when_rendering methods take one argument, the result, not/)
139
it "should stash a rendering block" do
140
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
141
when_invoked do |options| true end
142
when_rendering :json do |a| true end
144
action.when_rendering(:json).should be_an_instance_of Method
147
it "should fail if you try to set the same rendering twice" do
149
Puppet::Interface::ActionBuilder.build(face, :foo) do
150
when_invoked do |options| true end
151
when_rendering :json do |a| true end
152
when_rendering :json do |a| true end
154
}.to raise_error ArgumentError, /You can't define a rendering method for json twice/
157
it "should work if you set two different renderings" do
158
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
159
when_invoked do |options| true end
160
when_rendering :json do |a| true end
161
when_rendering :yaml do |a| true end
163
action.when_rendering(:json).should be_an_instance_of Method
164
action.when_rendering(:yaml).should be_an_instance_of Method
167
it "should be bound to the face when called" do
168
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
169
when_invoked do |options| true end
170
when_rendering :json do |a| self end
172
action.when_rendering(:json).call(true).should == face
176
context "#render_as" do
177
it "should default to nil (eg: based on context)" do
178
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
179
when_invoked do |options| true end
181
action.render_as.should be_nil
184
it "should fail if not rendering format is given" do
186
Puppet::Interface::ActionBuilder.build(face, :foo) do
187
when_invoked do |options| true end
190
}.to raise_error ArgumentError, /must give a rendering format to render_as/
193
Puppet::Network::FormatHandler.formats.each do |name|
194
it "should accept #{name.inspect} format" do
195
action = Puppet::Interface::ActionBuilder.build(face, :foo) do
196
when_invoked do |options| true end
199
action.render_as.should == name
203
[:if_you_define_this_format_you_frighten_me, "json", 12].each do |input|
204
it "should fail if given #{input.inspect}" do
206
Puppet::Interface::ActionBuilder.build(face, :foo) do
207
when_invoked do |options| true end
210
}.to raise_error ArgumentError, /#{input.inspect} is not a valid rendering format/