summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface/option_builder_spec.rb
blob: 3e91c683be84805a5826b9b9e78951b74b87e88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'puppet/interface/option_builder'

describe Puppet::Interface::OptionBuilder do
  let :face do Puppet::Interface.new(:option_builder_testing, '0.0.1') end

  it "should be able to construct an option without a block" do
    Puppet::Interface::OptionBuilder.build(face, "--foo").
      should be_an_instance_of Puppet::Interface::Option
  end

  it "should work with an empty block" do
    option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
      # This block deliberately left blank.
    end

    option.should be_an_instance_of Puppet::Interface::Option
  end

  [:description, :summary].each do |doc|
    it "should support #{doc} declarations" do
      text = "this is the #{doc}"
      option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        self.send doc, text
      end
      option.should be_an_instance_of Puppet::Interface::Option
      option.send(doc).should == text
    end
  end

  context "before_action hook" do
    it "should support a before_action hook" do
      option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        before_action do |a,b,c| :whatever end
      end
      option.before_action.should be_an_instance_of UnboundMethod
    end

    it "should fail if the hook block takes too few arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |one, two| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should fail if the hook block takes too many arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |one, two, three, four| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should fail if the hook block takes a variable number of arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |*blah| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should support simple required declarations" do
      opt = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        required
      end
      opt.should be_required
    end

    it "should support arguments to the required property" do
      opt = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        required(false)
      end
      opt.should_not be_required
    end

  end
end