summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-03-22 12:54:52 -0700
committerNick Lewis <nick@puppetlabs.com>2011-03-22 14:16:32 -0700
commite3d24865c89bccd0221f3d6d475d350f577ed3fb (patch)
tree01e2f42e8342767aa7f9e63e961a61bb81046b98 /spec/unit/interface
parent45613e0f192778cd16f945d5d1eb109e6c8dee2d (diff)
(#6814) Create a dedicated Action class
This class will represents an action, and allows us to store metadata for an action, and programmatically introspect and invoke them. A helper class ActionBuilder represents the DSL for defining an action. Also defined an "invoke" DSL method to handle the functionality of defining the method for an action. Reviewed-By: Daniel Pittman
Diffstat (limited to 'spec/unit/interface')
-rw-r--r--spec/unit/interface/action_builder_spec.rb30
-rw-r--r--spec/unit/interface/action_manager_spec.rb113
-rw-r--r--spec/unit/interface/action_spec.rb75
3 files changed, 185 insertions, 33 deletions
diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb
new file mode 100644
index 000000000..39b2386dc
--- /dev/null
+++ b/spec/unit/interface/action_builder_spec.rb
@@ -0,0 +1,30 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/interface/action_builder'
+
+describe Puppet::Interface::ActionBuilder do
+ describe "::build" do
+ 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 interface which invokes the action" do
+ interface = Puppet::Interface.new(:action_builder_test_interface)
+ action = Puppet::Interface::ActionBuilder.build(interface, :foo) do
+ invoke do
+ "invoked the method"
+ end
+ end
+
+ interface.foo.should == "invoked the method"
+ end
+
+ it "should require a block" do
+ lambda { Puppet::Interface::ActionBuilder.build(nil,:foo) }.should raise_error("Action 'foo' must specify a block")
+ end
+ end
+end
diff --git a/spec/unit/interface/action_manager_spec.rb b/spec/unit/interface/action_manager_spec.rb
index b71aecaa2..0b12db317 100644
--- a/spec/unit/interface/action_manager_spec.rb
+++ b/spec/unit/interface/action_manager_spec.rb
@@ -16,19 +16,28 @@ describe Puppet::Interface::ActionManager do
describe "when included in a class" do
it "should be able to define an action" do
- @tester.action(:foo) { "something "}
+ @tester.action(:foo) do
+ invoke { "something "}
+ end
end
it "should be able to list defined actions" do
- @tester.action(:foo) { "something" }
- @tester.action(:bar) { "something" }
+ @tester.action(:foo) do
+ invoke { "something" }
+ end
+ @tester.action(:bar) do
+ invoke { "something" }
+ end
- @tester.actions.should be_include(:bar)
- @tester.actions.should be_include(:foo)
+ @tester.actions.should include(:bar)
+ @tester.actions.should include(:foo)
end
it "should be able to indicate when an action is defined" do
- @tester.action(:foo) { "something" }
+ @tester.action(:foo) do
+ invoke { "something" }
+ end
+
@tester.should be_action(:foo)
end
end
@@ -40,15 +49,21 @@ describe Puppet::Interface::ActionManager do
end
it "should be able to define an action" do
- @tester.action(:foo) { "something "}
+ @tester.action(:foo) do
+ invoke { "something "}
+ end
end
it "should be able to list defined actions" do
- @tester.action(:foo) { "something" }
- @tester.action(:bar) { "something" }
+ @tester.action(:foo) do
+ invoke { "something" }
+ end
+ @tester.action(:bar) do
+ invoke { "something" }
+ end
- @tester.actions.should be_include(:bar)
- @tester.actions.should be_include(:foo)
+ @tester.actions.should include(:bar)
+ @tester.actions.should include(:foo)
end
it "should be able to indicate when an action is defined" do
@@ -67,54 +82,78 @@ describe Puppet::Interface::ActionManager do
end
it "should be able to define an action at the class level" do
- @klass.action(:foo) { "something "}
+ @klass.action(:foo) do
+ invoke { "something "}
+ end
end
it "should create an instance method when an action is defined at the class level" do
- @klass.action(:foo) { "something" }
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
@instance.foo.should == "something"
end
it "should be able to define an action at the instance level" do
- @instance.action(:foo) { "something "}
+ @instance.action(:foo) do
+ invoke { "something "}
+ end
end
it "should create an instance method when an action is defined at the instance level" do
- @instance.action(:foo) { "something" }
+ @instance.action(:foo) do
+ invoke { "something" }
+ end
@instance.foo.should == "something"
end
it "should be able to list actions defined at the class level" do
- @klass.action(:foo) { "something" }
- @klass.action(:bar) { "something" }
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @klass.action(:bar) do
+ invoke { "something" }
+ end
- @klass.actions.should be_include(:bar)
- @klass.actions.should be_include(:foo)
+ @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) { "something" }
- @instance.action(:bar) { "something" }
+ @instance.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.action(:bar) do
+ invoke { "something" }
+ end
- @instance.actions.should be_include(:bar)
- @instance.actions.should be_include(:foo)
+ @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) { "something" }
- @instance.action(:bar) { "something" }
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.action(:bar) do
+ invoke { "something" }
+ end
- @instance.actions.should be_include(:bar)
- @instance.actions.should be_include(:foo)
+ @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) { "something" }
+ @klass.action(:foo) do
+ invoke { "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) { "something" }
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
@instance.should be_action(:foo)
end
@@ -122,9 +161,15 @@ describe Puppet::Interface::ActionManager do
@subclass = Class.new(@klass)
@instance = @subclass.new
- @klass.action(:parent) { "a" }
- @subclass.action(:sub) { "a" }
- @instance.action(:instance) { "a" }
+ @klass.action(:parent) do
+ invoke { "a" }
+ end
+ @subclass.action(:sub) do
+ invoke { "a" }
+ end
+ @instance.action(:instance) do
+ invoke { "a" }
+ end
@instance.should be_action(:parent)
@instance.should be_action(:sub)
@@ -135,7 +180,9 @@ describe Puppet::Interface::ActionManager do
@subclass = Class.new(@klass)
@instance = @subclass.new
- @klass.action(:foo) { "something" }
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
@instance.foo.should == "something"
end
end
diff --git a/spec/unit/interface/action_spec.rb b/spec/unit/interface/action_spec.rb
new file mode 100644
index 000000000..e74fa9fcc
--- /dev/null
+++ b/spec/unit/interface/action_spec.rb
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/interface/action'
+
+describe Puppet::Interface::Action do
+ describe "when validating the action name" do
+ it "should require a name" do
+ lambda { Puppet::Interface::Action.new(nil,nil) }.should raise_error("'' is an invalid action name")
+ end
+
+ it "should not allow empty names" do
+ lambda { Puppet::Interface::Action.new(nil,'') }.should raise_error("'' is an invalid action name")
+ end
+
+ it "should not allow names with whitespace" do
+ lambda { Puppet::Interface::Action.new(nil,'foo bar') }.should raise_error("'foo bar' is an invalid action name")
+ end
+
+ it "should not allow names beginning with dashes" do
+ lambda { Puppet::Interface::Action.new(nil,'-foobar') }.should raise_error("'-foobar' is an invalid action name")
+ end
+ end
+
+ describe "when invoking" do
+ it "should be able to call other actions on the same object" do
+ interface = Puppet::Interface.new(:my_interface) do
+ action(:foo) do
+ invoke { 25 }
+ end
+
+ action(:bar) do
+ invoke { "the value of foo is '#{foo}'" }
+ end
+ end
+ interface.foo.should == 25
+ interface.bar.should == "the value of foo is '25'"
+ end
+
+ # bar is a class action calling a class action
+ # quux is a class action calling an instance action
+ # baz is an instance action calling a class action
+ # qux is an instance action calling an instance action
+ it "should be able to call other actions on the same object when defined on a class" do
+ class Puppet::Interface::MyInterfaceBaseClass < Puppet::Interface
+ action(:foo) do
+ invoke { 25 }
+ end
+
+ action(:bar) do
+ invoke { "the value of foo is '#{foo}'" }
+ end
+
+ action(:quux) do
+ invoke { "qux told me #{qux}" }
+ end
+ end
+
+ interface = Puppet::Interface::MyInterfaceBaseClass.new(:my_inherited_interface) do
+ action(:baz) do
+ invoke { "the value of foo in baz is '#{foo}'" }
+ end
+
+ action(:qux) do
+ invoke { baz }
+ end
+ end
+ interface.foo.should == 25
+ interface.bar.should == "the value of foo is '25'"
+ interface.quux.should == "qux told me the value of foo in baz is '25'"
+ interface.baz.should == "the value of foo in baz is '25'"
+ interface.qux.should == "the value of foo in baz is '25'"
+ end
+ end
+end