summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface/option_spec.rb
blob: 3bcd121e22e4c42f42fddfeab0de9b85111a9969 (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
require 'puppet/interface/option'

describe Puppet::Interface::Option do
  let :face do Puppet::Interface.new(:option_testing, '0.0.1') end

  describe "#optparse_to_name" do
    ["", "=BAR", " BAR", "=bar", " bar"].each do |postfix|
      { "--foo" => :foo, "-f" => :f }.each do |base, expect|
        input = base + postfix
        it "should map #{input.inspect} to #{expect.inspect}" do
          option = Puppet::Interface::Option.new(face, input)
          option.name.should == expect
        end
      end
    end

    [:foo, 12, nil, {}, []].each do |input|
      it "should fail sensible when given #{input.inspect}" do
        expect { Puppet::Interface::Option.new(face, input) }.
          should raise_error ArgumentError, /is not valid for an option argument/
      end
    end

    ["-foo", "-foo=BAR", "-foo BAR"].each do |input|
      it "should fail with a single dash for long option #{input.inspect}" do
        expect { Puppet::Interface::Option.new(face, input) }.
          should raise_error ArgumentError, /long options need two dashes \(--\)/
      end
    end
  end

  it "requires a face when created" do
    expect { Puppet::Interface::Option.new }.
      should raise_error ArgumentError, /wrong number of arguments/
  end

  it "also requires some declaration arguments when created" do
    expect { Puppet::Interface::Option.new(face) }.
      should raise_error ArgumentError, /No option declarations found/
  end

  it "should infer the name from an optparse string" do
    option = Puppet::Interface::Option.new(face, "--foo")
    option.name.should == :foo
  end

  it "should infer the name when multiple optparse string are given" do
    option = Puppet::Interface::Option.new(face, "--foo", "-f")
    option.name.should == :foo
  end

  it "should prefer the first long option name over a short option name" do
    option = Puppet::Interface::Option.new(face, "-f", "--foo")
    option.name.should == :foo
  end

  it "should create an instance when given a face and name" do
    Puppet::Interface::Option.new(face, "--foo").
      should be_instance_of Puppet::Interface::Option
  end

  describe "#to_s" do
    it "should transform a symbol into a string" do
      option = Puppet::Interface::Option.new(face, "--foo")
      option.name.should == :foo
      option.to_s.should == "foo"
    end

    it "should use - rather than _ to separate words in strings but not symbols" do
      option = Puppet::Interface::Option.new(face, "--foo-bar")
      option.name.should == :foo_bar
      option.to_s.should == "foo-bar"
    end
  end
end