summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-07-20 11:58:55 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-07-20 17:06:34 -0700
commitb75b1c19ecf6c278b065d203ac8486fa598caa8b (patch)
tree14378b3efc4a99db382ca4bf035967001c0ed646 /spec/unit/interface
parentda504b3a0004666e9014ffbd0bf108acf07f9dce (diff)
downloadpuppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.tar.gz
puppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.tar.xz
puppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.zip
(#6787) Add `default_to` for options.
This implement support for options with default values, allowing faces to set those values when not invoked. This can eliminate substantial duplicate code from actions, especially when there are face-level options in use. Reviewed-By: Pieter van de Bruggen <pieter@puppetlabs.com>
Diffstat (limited to 'spec/unit/interface')
-rwxr-xr-xspec/unit/interface/action_spec.rb45
-rwxr-xr-xspec/unit/interface/option_spec.rb44
2 files changed, 89 insertions, 0 deletions
diff --git a/spec/unit/interface/action_spec.rb b/spec/unit/interface/action_spec.rb
index 23216e7b4..dbbf84778 100755
--- a/spec/unit/interface/action_spec.rb
+++ b/spec/unit/interface/action_spec.rb
@@ -552,4 +552,49 @@ describe Puppet::Interface::Action do
to raise_error ArgumentError, /Multiple aliases for the same option/
end
end
+
+ context "default option values" do
+ subject do
+ Puppet::Interface.new(:default_option_values, '1.0.0') do
+ action :foo do
+ option "--foo" do end
+ option "--bar" do end
+ when_invoked do |options| options end
+ end
+ end
+ end
+
+ let :action do subject.get_action :foo end
+ let :option do action.get_option :foo end
+
+ it "should not add options without defaults" do
+ subject.foo.should == {}
+ end
+
+ it "should not add options without defaults, if options are given" do
+ subject.foo(:bar => 1).should == { :bar => 1 }
+ end
+
+ it "should add the option default value when set" do
+ option.default = proc { 12 }
+ subject.foo.should == { :foo => 12 }
+ end
+
+ it "should add the option default value when set, if other options are given" do
+ option.default = proc { 12 }
+ subject.foo(:bar => 1).should == { :foo => 12, :bar => 1 }
+ end
+
+ it "should invoke the same default proc every time called" do
+ option.default = proc { @foo ||= {} }
+ subject.foo[:foo].object_id.should == subject.foo[:foo].object_id
+ end
+
+ [nil, 0, 1, true, false, {}, []].each do |input|
+ it "should not override a passed option (#{input.inspect})" do
+ option.default = proc { :fail }
+ subject.foo(:foo => input).should == { :foo => input }
+ end
+ end
+ end
end
diff --git a/spec/unit/interface/option_spec.rb b/spec/unit/interface/option_spec.rb
index e77b46e79..e73561fba 100755
--- a/spec/unit/interface/option_spec.rb
+++ b/spec/unit/interface/option_spec.rb
@@ -97,4 +97,48 @@ describe Puppet::Interface::Option do
end
end
end
+
+ context "defaults" do
+ subject { Puppet::Interface::Option.new(face, "--foo") }
+
+ it "should work sanely if member variables are used for state" do
+ subject.default = proc { @foo ||= 0; @foo += 1 }
+ subject.default.should == 1
+ subject.default.should == 2
+ subject.default.should == 3
+ end
+
+ context "with no default" do
+ it { should_not be_has_default }
+ its :default do should be_nil end
+
+ it "should set a proc as default" do
+ expect { subject.default = proc { 12 } }.should_not raise_error
+ end
+
+ [1, {}, [], Object.new, "foo"].each do |input|
+ it "should reject anything but a proc (#{input.class})" do
+ expect { subject.default = input }.to raise_error ArgumentError, /not a proc/
+ end
+ end
+ end
+
+ context "with a default" do
+ before :each do subject.default = proc { [:foo] } end
+
+ it { should be_has_default }
+ its :default do should == [:foo] end
+
+ it "should invoke the block every time" do
+ subject.default.object_id.should_not == subject.default.object_id
+ subject.default.should == subject.default
+ end
+
+ it "should allow replacing the default proc" do
+ subject.default.should == [:foo]
+ subject.default = proc { :bar }
+ subject.default.should == :bar
+ end
+ end
+ end
end