~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to spec/unit/interface/option_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/interface'
 
2
require 'puppet/interface/option'
 
3
 
 
4
describe Puppet::Interface::Option do
 
5
  let :face do Puppet::Interface.new(:option_testing, '0.0.1') end
 
6
 
 
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
 
14
        end
 
15
      end
 
16
    end
 
17
 
 
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/
 
22
      end
 
23
    end
 
24
 
 
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 \(--\)/
 
29
      end
 
30
    end
 
31
  end
 
32
 
 
33
  it "requires a face when created" do
 
34
    expect { Puppet::Interface::Option.new }.
 
35
      should raise_error ArgumentError, /wrong number of arguments/
 
36
  end
 
37
 
 
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/
 
41
  end
 
42
 
 
43
  it "should infer the name from an optparse string" do
 
44
    option = Puppet::Interface::Option.new(face, "--foo")
 
45
    option.name.should == :foo
 
46
  end
 
47
 
 
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
 
51
  end
 
52
 
 
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
 
56
  end
 
57
 
 
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
 
61
  end
 
62
 
 
63
  describe "#to_s" do
 
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"
 
68
    end
 
69
 
 
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"
 
74
    end
 
75
  end
 
76
 
 
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
 
81
 
 
82
      it { should respond_to "#{side}_action" }
 
83
      it { should respond_to "#{side}_action=" }
 
84
 
 
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
 
89
      end
 
90
 
 
91
      data = [1, "foo", :foo, Object.new, method(:hash), method(:hash).unbind]
 
92
      data.each do |input|
 
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/
 
96
        end
 
97
      end
 
98
    end
 
99
  end
 
100
end