summaryrefslogtreecommitdiff
path: root/test/options_test.rb
blob: 2f83fc40843f55f45340283bd680c0f8d26abe6d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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 "#separator" do
    it "appends separators between options in order" do
      @options.separator("foo")
      @options.on("--foo")
      @options.separator("bar")

      assert_equal ["foo", "bar"], @options.separators
    end

    it "appends strings to the last separator if no options exist" do
      @options.separator("foo")
      @options.separator("bar")

      assert_equal ["foo\nbar"], @options.separators
    end

    it "includes separators in the help text" do
      @options.on("--foo")
      @options.separator("bar")

      help = @options.to_s.squeeze(" ")
      assert help.end_with?("--foo \nbar\n")
    end

    it "accepts a frozen argument, even when called multiple times for the same option" do
      @options.separator("foo".freeze)
      @options.separator("bar".freeze)
    end

    it "defaults to empty string" do
      @options.separator

      assert_equal [""], @options.separators
    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