summaryrefslogtreecommitdiff
path: root/test/options_test.rb
blob: bb19210a9ef60b2ea654d3b94b48bdd8d2ec82ea (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'test_helper'

describe Slop::Options do
  before do
    @options = Slop::Options.new
  end

  describe "#on" do
    it "defaults to null type" do
      assert_kind_of Slop::NullOption, @options.on("--foo")
    end

    it "accepts custom types" do
      module Slop; class FooOption < Option; end; end
      assert_kind_of Slop::FooOption, @options.on("--foo", type: :foo)
    end

    it "adds multiple flags" do
      option = @options.on("-f", "-F", "--foo")
      assert_equal %w(-f -F --foo), option.flags
    end

    it "accepts a trailing description" do
      option = @options.on("--foo", "fooey")
      assert_equal "fooey", option.desc
    end

    it "adds the option" do
      option = @options.on("--foo")
      assert_equal [option], @options.to_a
    end

    it "raises an error when a duplicate flag is used" do
      @options.on("--foo")
      assert_raises(ArgumentError) { @options.on("--foo") }
    end
  end

  describe "#method_missing" do
    it "uses the method name as an option type" do
      option = @options.string("--name")
      assert_kind_of Slop::StringOption, option
    end

    it "raises if a type doesn't exist" do
      assert_raises(NoMethodError) { @options.unknown }
    end
  end

  describe "#respond_to?" do
    it "handles custom types" do
      module Slop; class BarOption < Option; end; end
      assert @options.respond_to?(:bar)
    end
  end

  describe "#to_s" do
    it "is prefixed with the default banner" do
      assert_match(/^usage/, @options.to_s)
    end

    it "aligns option strings" do
      @options.on "-f", "--foo", "fooey"
      @options.on "-s", "short"
      assert_match(/^    -f, --foo  fooey/, @options.to_s)
      assert_match(/^    -s         short/, @options.to_s)
    end

    it "can use a custom prefix" do
      @options.on "-f", "--foo"
      assert_match(/^ -f, --foo/, @options.to_s(prefix: " "))
    end

    it "ignores options with help: false" do
      @options.on "-x", "something", help: false
      refute_match(/something/, @options.to_s)
    end

    it "adds 'tail' options to the bottom of the help text" do
      @options.on "-h", "--help", tail: true
      @options.on "-f", "--foo"
      assert_match(/^    -h, --help/, @options.to_s.lines.last)
    end
  end

  describe "custom banner" do
    it "is prefixed with defined banner" do
      @options_config = Slop::Options.new({banner: "custom banner"})
      assert_match(/^custom banner/, @options_config.to_s) 
    end
    it "banner is disabled" do
      @options_config = Slop::Options.new({banner: false})
      assert_match("", @options_config.to_s)
    end
  end
end