summaryrefslogtreecommitdiffstats
path: root/spec/shared_behaviours
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/shared_behaviours
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/shared_behaviours')
-rwxr-xr-xspec/shared_behaviours/things_that_declare_options.rb113
1 files changed, 113 insertions, 0 deletions
diff --git a/spec/shared_behaviours/things_that_declare_options.rb b/spec/shared_behaviours/things_that_declare_options.rb
index 19bba66c1..ecdbfcaea 100755
--- a/spec/shared_behaviours/things_that_declare_options.rb
+++ b/spec/shared_behaviours/things_that_declare_options.rb
@@ -146,4 +146,117 @@ shared_examples_for "things that declare options" do
end
end
end
+
+ describe "#default_to" do
+ it "should not have a default value by default" do
+ option = add_options_to do option "--foo" end.get_option(:foo)
+ option.should_not be_has_default
+ end
+
+ it "should accept a block for the default value" do
+ option = add_options_to do
+ option "--foo" do
+ default_to do
+ 12
+ end
+ end
+ end.get_option(:foo)
+
+ option.should be_has_default
+ end
+
+ it "should invoke the block when asked for the default value" do
+ invoked = false
+ option = add_options_to do
+ option "--foo" do
+ default_to do
+ invoked = true
+ end
+ end
+ end.get_option(:foo)
+
+ option.should be_has_default
+ option.default.should be_true
+ invoked.should be_true
+ end
+
+ it "should return the value of the block when asked for the default" do
+ option = add_options_to do
+ option "--foo" do
+ default_to do
+ 12
+ end
+ end
+ end.get_option(:foo)
+
+ option.should be_has_default
+ option.default.should == 12
+ end
+
+ it "should invoke the block every time the default is requested" do
+ option = add_options_to do
+ option "--foo" do
+ default_to do
+ {}
+ end
+ end
+ end.get_option(:foo)
+
+ first = option.default.object_id
+ second = option.default.object_id
+ third = option.default.object_id
+
+ first.should_not == second
+ first.should_not == third
+ second.should_not == third
+ end
+
+ it "should fail if the option has a default and is required" do
+ expect {
+ add_options_to do
+ option "--foo" do
+ required
+ default_to do 12 end
+ end
+ end
+ }.to raise_error ArgumentError, /can't be optional and have a default value/
+
+ expect {
+ add_options_to do
+ option "--foo" do
+ default_to do 12 end
+ required
+ end
+ end
+ }.to raise_error ArgumentError, /can't be optional and have a default value/
+ end
+
+ it "should fail if default_to has no block" do
+ expect { add_options_to do option "--foo" do default_to end end }.
+ to raise_error ArgumentError, /default_to requires a block/
+ end
+
+ it "should fail if default_to is invoked twice" do
+ expect {
+ add_options_to do
+ option "--foo" do
+ default_to do 12 end
+ default_to do "fun" end
+ end
+ end
+ }.to raise_error ArgumentError, /already has a default value/
+ end
+
+ [ "one", "one, two", "one, *two" ].each do |input|
+ it "should fail if the block has the wrong arity (#{input})" do
+ expect {
+ add_options_to do
+ option "--foo" do
+ eval "default_to do |#{input}| 12 end"
+ end
+ end
+ }.to raise_error ArgumentError, /should not take any arguments/
+ end
+ end
+ end
end