summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface/action_builder_spec.rb
blob: 602daaacac3c631094f2e95281ea46538dfc05b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/interface/action_builder'
require 'puppet/network/format_handler'

describe Puppet::Interface::ActionBuilder 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(face, :foo) do
      when_invoked do |options| true end
    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 { |options| "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

  it "should require an invocation block" do
    expect {
      Puppet::Interface::ActionBuilder.build(face, :foo) {}
    }.to raise_error(/actions need to know what to do when_invoked; please add the block/)
  end

  describe "when handling options" do
    it "should have a #option DSL function" do
      method = nil
      Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        method = self.method(:option)
      end
      method.should be_an_instance_of Method
    end

    it "should define an option without a block" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        option "--bar"
      end
      action.should be_option :bar
    end

    it "should accept an empty block" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        option "--bar" do
          # This space left deliberately blank.
        end
      end
      action.should be_option :bar
    end
  end

  context "inline documentation" do
    it "should set the summary" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        summary "this is some text"
      end
      action.summary.should == "this is some text"
    end
  end

  context "action defaulting" do
    it "should set the default to true" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        default
      end
      action.default.should be_true
    end

    it "should not be default by, er, default. *cough*" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
      end
      action.default.should be_false
    end
  end

  context "#when_rendering" do
    it "should fail if no rendering format is given" do
      expect {
        Puppet::Interface::ActionBuilder.build(face, :foo) do
          when_invoked do |options| true end
          when_rendering do true end
        end
      }.to raise_error ArgumentError, /must give a rendering format to when_rendering/
    end

    it "should fail if no block is given" do
      expect {
        Puppet::Interface::ActionBuilder.build(face, :foo) do
          when_invoked do |options| true end
          when_rendering :json
        end
      }.to raise_error ArgumentError, /must give a block to when_rendering/
    end

    it "should fail if the block takes no arguments" do
      expect {
        Puppet::Interface::ActionBuilder.build(face, :foo) do
          when_invoked do |options| true end
          when_rendering :json do true end
        end
      }.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_invoked do |options| true end
          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_invoked do |options| true end
          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_invoked do |options| true end
        when_rendering :json do |a| true end
      end
      action.when_rendering(:json).should be_an_instance_of Method
    end

    it "should fail if you try to set the same rendering twice" do
      expect {
        Puppet::Interface::ActionBuilder.build(face, :foo) do
          when_invoked do |options| true end
          when_rendering :json do |a| true end
          when_rendering :json do |a| true end
        end
      }.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_invoked do |options| true end
        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

    it "should be bound to the face when called" do
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
        when_invoked do |options| true end
        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
        when_invoked do |options| true end
      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
          when_invoked do |options| true end
          render_as
        end
      }.to raise_error ArgumentError, /must give a rendering format to render_as/
    end

    Puppet::Network::FormatHandler.formats.each do |name|
      it "should accept #{name.inspect} format" do
        action = Puppet::Interface::ActionBuilder.build(face, :foo) do
          when_invoked do |options| true end
          render_as name
        end
        action.render_as.should == name
      end
    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
            when_invoked do |options| true end
            render_as input
          end
        }.to raise_error ArgumentError, /#{input.inspect} is not a valid rendering format/
      end
    end
  end
end