1
require 'puppet/interface'
2
require 'puppet/interface/option'
4
describe Puppet::Interface::Option do
5
let :face do Puppet::Interface.new(:option_testing, '0.0.1') end
7
describe "#optparse_to_name" do
8
["", "=BAR", " BAR", "=bar", " bar"].each do |postfix|
9
{ "--foo" => :foo, "-f" => :f }.each do |base, expect|
10
input = base + postfix
11
it "should map #{input.inspect} to #{expect.inspect}" do
12
option = Puppet::Interface::Option.new(face, input)
13
option.name.should == expect
18
[:foo, 12, nil, {}, []].each do |input|
19
it "should fail sensible when given #{input.inspect}" do
20
expect { Puppet::Interface::Option.new(face, input) }.
21
should raise_error ArgumentError, /is not valid for an option argument/
25
["-foo", "-foo=BAR", "-foo BAR"].each do |input|
26
it "should fail with a single dash for long option #{input.inspect}" do
27
expect { Puppet::Interface::Option.new(face, input) }.
28
should raise_error ArgumentError, /long options need two dashes \(--\)/
33
it "requires a face when created" do
34
expect { Puppet::Interface::Option.new }.
35
should raise_error ArgumentError, /wrong number of arguments/
38
it "also requires some declaration arguments when created" do
39
expect { Puppet::Interface::Option.new(face) }.
40
should raise_error ArgumentError, /No option declarations found/
43
it "should infer the name from an optparse string" do
44
option = Puppet::Interface::Option.new(face, "--foo")
45
option.name.should == :foo
48
it "should infer the name when multiple optparse string are given" do
49
option = Puppet::Interface::Option.new(face, "--foo", "-f")
50
option.name.should == :foo
53
it "should prefer the first long option name over a short option name" do
54
option = Puppet::Interface::Option.new(face, "-f", "--foo")
55
option.name.should == :foo
58
it "should create an instance when given a face and name" do
59
Puppet::Interface::Option.new(face, "--foo").
60
should be_instance_of Puppet::Interface::Option
64
it "should transform a symbol into a string" do
65
option = Puppet::Interface::Option.new(face, "--foo")
66
option.name.should == :foo
67
option.to_s.should == "foo"
70
it "should use - rather than _ to separate words in strings but not symbols" do
71
option = Puppet::Interface::Option.new(face, "--foo-bar")
72
option.name.should == :foo_bar
73
option.to_s.should == "foo-bar"
77
%w{before after}.each do |side|
78
describe "#{side} hooks" do
79
subject { Puppet::Interface::Option.new(face, "--foo") }
80
let :proc do Proc.new do :from_proc end end
82
it { should respond_to "#{side}_action" }
83
it { should respond_to "#{side}_action=" }
85
it "should set the #{side}_action hook" do
86
subject.send("#{side}_action").should be_nil
87
subject.send("#{side}_action=", proc)
88
subject.send("#{side}_action").should be_an_instance_of UnboundMethod
91
data = [1, "foo", :foo, Object.new, method(:hash), method(:hash).unbind]
93
it "should fail if a #{input.class} is added to the #{side} hooks" do
94
expect { subject.send("#{side}_action=", input) }.
95
to raise_error ArgumentError, /not a proc/