summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface/action_manager_spec.rb
blob: 50bea5f89ec0acef0cc4fd1b93af147aa5685dd3 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')

# This is entirely an internal class for Interface, so we have to load it instead of our class.
require 'puppet/interface'

class ActionManagerTester
  include Puppet::Interface::ActionManager
end

describe Puppet::Interface::ActionManager do
  subject { ActionManagerTester.new }

  describe "when included in a class" do
    it "should be able to define an action" do
      subject.action(:foo) do
        when_invoked { "something "}
      end
    end

    it "should be able to define a 'script' style action" do
      subject.script :bar do
        "a bar is where beer is found"
      end
    end

    it "should be able to list defined actions" do
      subject.action(:foo) do
        when_invoked { "something" }
      end
      subject.action(:bar) do
        when_invoked { "something" }
      end

      subject.actions.should =~ [:foo, :bar]
    end

    it "should list 'script' actions" do
      subject.script :foo do "foo" end
      subject.actions.should =~ [:foo]
    end

    it "should list both script and normal actions" do
      subject.action :foo do
        when_invoked do "foo" end
      end
      subject.script :bar do "a bar is where beer is found" end

      subject.actions.should =~ [:foo, :bar]
    end

    it "should be able to indicate when an action is defined" do
      subject.action(:foo) do
        when_invoked { "something" }
      end

      subject.should be_action(:foo)
    end

    it "should indicate an action is defined for script actions" do
      subject.script :foo do "foo" end
      subject.should be_action :foo
    end

    it "should correctly treat action names specified as strings" do
      subject.action(:foo) do
        when_invoked { "something" }
      end

      subject.should be_action("foo")
    end
  end

  describe "when used to extend a class" do
    subject { Class.new.extend(Puppet::Interface::ActionManager) }

    it "should be able to define an action" do
      subject.action(:foo) do
        when_invoked { "something "}
      end
    end

    it "should be able to list defined actions" do
      subject.action(:foo) do
        when_invoked { "something" }
      end
      subject.action(:bar) do
        when_invoked { "something" }
      end

      subject.actions.should include(:bar)
      subject.actions.should include(:foo)
    end

    it "should be able to indicate when an action is defined" do
      subject.action(:foo) { "something" }
      subject.should be_action(:foo)
    end
  end

  describe "when used both at the class and instance level" do
    before do
      @klass = Class.new do
        include Puppet::Interface::ActionManager
        extend Puppet::Interface::ActionManager
      end
      @instance = @klass.new
    end

    it "should be able to define an action at the class level" do
      @klass.action(:foo) do
        when_invoked { "something "}
      end
    end

    it "should create an instance method when an action is defined at the class level" do
      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @instance.foo.should == "something"
    end

    it "should be able to define an action at the instance level" do
      @instance.action(:foo) do
        when_invoked { "something "}
      end
    end

    it "should create an instance method when an action is defined at the instance level" do
      @instance.action(:foo) do
        when_invoked { "something" }
      end
      @instance.foo.should == "something"
    end

    it "should be able to list actions defined at the class level" do
      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @klass.action(:bar) do
        when_invoked { "something" }
      end

      @klass.actions.should include(:bar)
      @klass.actions.should include(:foo)
    end

    it "should be able to list actions defined at the instance level" do
      @instance.action(:foo) do
        when_invoked { "something" }
      end
      @instance.action(:bar) do
        when_invoked { "something" }
      end

      @instance.actions.should include(:bar)
      @instance.actions.should include(:foo)
    end

    it "should be able to list actions defined at both instance and class level" do
      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @instance.action(:bar) do
        when_invoked { "something" }
      end

      @instance.actions.should include(:bar)
      @instance.actions.should include(:foo)
    end

    it "should be able to indicate when an action is defined at the class level" do
      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @instance.should be_action(:foo)
    end

    it "should be able to indicate when an action is defined at the instance level" do
      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @instance.should be_action(:foo)
    end

    it "should list actions defined in superclasses" do
      @subclass = Class.new(@klass)
      @instance = @subclass.new

      @klass.action(:parent) do
        when_invoked { "a" }
      end
      @subclass.action(:sub) do
        when_invoked { "a" }
      end
      @instance.action(:instance) do
        when_invoked { "a" }
      end

      @instance.should be_action(:parent)
      @instance.should be_action(:sub)
      @instance.should be_action(:instance)
    end

    it "should create an instance method when an action is defined in a superclass" do
      @subclass = Class.new(@klass)
      @instance = @subclass.new

      @klass.action(:foo) do
        when_invoked { "something" }
      end
      @instance.foo.should == "something"
    end
  end

  describe "#get_action" do
    let :parent_class do
      parent_class = Class.new(Puppet::Interface)
      parent_class.action(:foo) {}
      parent_class
    end

    it "should check that we can find inherited actions when we are a class" do
      Class.new(parent_class).get_action(:foo).name.should == :foo
    end

    it "should check that we can find inherited actions when we are an instance" do
      instance = parent_class.new(:foo, '0.0.0')
      instance.get_action(:foo).name.should == :foo
    end
  end
end