File: option_spec.rb

package info (click to toggle)
ruby-thor 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: ruby: 9,250; makefile: 8; sh: 1
file content (292 lines) | stat: -rw-r--r-- 9,548 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
require "helper"
require "thor/parser"

describe Thor::Option do
  def parse(key, value)
    Thor::Option.parse(key, value)
  end

  def option(name, options = {})
    @option ||= Thor::Option.new(name, options)
  end

  describe "#parse" do
    describe "with value as a symbol" do
      describe "and symbol is a valid type" do
        it "has type equals to the symbol" do
          expect(parse(:foo, :string).type).to eq(:string)
          expect(parse(:foo, :numeric).type).to eq(:numeric)
        end

        it "has no default value" do
          expect(parse(:foo, :string).default).to be nil
          expect(parse(:foo, :numeric).default).to be nil
        end
      end

      describe "equals to :required" do
        it "has type equals to :string" do
          expect(parse(:foo, :required).type).to eq(:string)
        end

        it "has no default value" do
          expect(parse(:foo, :required).default).to be nil
        end
      end

      describe "and symbol is not a reserved key" do
        it "has type equal to :string" do
          expect(parse(:foo, :bar).type).to eq(:string)
        end

        it "has no default value" do
          expect(parse(:foo, :bar).default).to be nil
        end
      end
    end

    describe "with value as hash" do
      it "has default type :hash" do
        expect(parse(:foo, a: :b).type).to eq(:hash)
      end

      it "has default value equal to the hash" do
        expect(parse(:foo, a: :b).default).to eq(a: :b)
      end
    end

    describe "with value as array" do
      it "has default type :array" do
        expect(parse(:foo, [:a, :b]).type).to eq(:array)
      end

      it "has default value equal to the array" do
        expect(parse(:foo, [:a, :b]).default).to eq([:a, :b])
      end
    end

    describe "with value as string" do
      it "has default type :string" do
        expect(parse(:foo, "bar").type).to eq(:string)
      end

      it "has default value equal to the string" do
        expect(parse(:foo, "bar").default).to eq("bar")
      end
    end

    describe "with value as numeric" do
      it "has default type :numeric" do
        expect(parse(:foo, 2.0).type).to eq(:numeric)
      end

      it "has default value equal to the numeric" do
        expect(parse(:foo, 2.0).default).to eq(2.0)
      end
    end

    describe "with value as boolean" do
      it "has default type :boolean" do
        expect(parse(:foo, true).type).to eq(:boolean)
        expect(parse(:foo, false).type).to eq(:boolean)
      end

      it "has default value equal to the boolean" do
        expect(parse(:foo, true).default).to eq(true)
        expect(parse(:foo, false).default).to eq(false)
      end
    end

    describe "with key as a symbol" do
      it "sets the name equal to the key" do
        expect(parse(:foo, true).name).to eq("foo")
      end
    end

    describe "with key as an array" do
      it "sets the first items in the array to the name" do
        expect(parse([:foo, :b, "--bar"], true).name).to eq("foo")
      end

      it "sets all other items as normalized aliases" do
        expect(parse([:foo, :b, "--bar"], true).aliases).to eq(["-b", "--bar"])
      end
    end
  end

  it "returns the switch name" do
    expect(option("foo").switch_name).to eq("--foo")
    expect(option("--foo").switch_name).to eq("--foo")
  end

  it "returns the human name" do
    expect(option("foo").human_name).to eq("foo")
    expect(option("--foo").human_name).to eq("foo")
  end

  it "converts underscores to dashes" do
    expect(option("foo_bar").switch_name).to eq("--foo-bar")
  end

  it "can be required and have default values" do
    option = option("foo", required: true, type: :string, default: "bar")
    expect(option.default).to eq("bar")
    expect(option).to be_required
  end

  it "raises an error if default is inconsistent with type and check_default_type is true" do
    expect do
      option("foo_bar", type: :numeric, default: "baz", check_default_type: true)
    end.to raise_error(ArgumentError, 'Expected numeric default value for \'--foo-bar\'; got "baz" (string)')
  end

  it "raises an error if repeatable and default is inconsistent with type and check_default_type is true" do
    expect do
      option("foo_bar", type: :numeric, repeatable: true, default: "baz", check_default_type: true)
    end.to raise_error(ArgumentError, 'Expected array default value for \'--foo-bar\'; got "baz" (string)')
  end

  it "raises an error type hash is repeatable and default is inconsistent with type and check_default_type is true" do
    expect do
      option("foo_bar", type: :hash, repeatable: true, default: "baz", check_default_type: true)
    end.to raise_error(ArgumentError, 'Expected hash default value for \'--foo-bar\'; got "baz" (string)')
  end

  it "does not raises an error if type hash is repeatable and default is consistent with type and check_default_type is true" do
    expect do
      option("foo_bar", type: :hash, repeatable: true, default: {}, check_default_type: true)
    end.not_to raise_error
  end

  it "does not raises an error if repeatable and default is consistent with type and check_default_type is true" do
    expect do
      option("foo_bar", type: :numeric, repeatable: true, default: [1], check_default_type: true)
    end.not_to raise_error
  end

  it "does not raises an error if default is an symbol and type string and check_default_type is true" do
    expect do
      option("foo", type: :string, default: :bar, check_default_type: true)
    end.not_to raise_error
  end

  it "does not raises an error if default is inconsistent with type and check_default_type is false" do
    expect do
      option("foo_bar", type: :numeric, default: "baz", check_default_type: false)
    end.not_to raise_error
  end

  it "boolean options cannot be required" do
    expect do
      option("foo", required: true, type: :boolean)
    end.to raise_error(ArgumentError, "An option cannot be boolean and required.")
  end

  it "does not raises an error if default is a boolean and it is required" do
    expect do
      option("foo", required: true, default: true)
    end.not_to raise_error
  end

  it "allows type predicates" do
    expect(parse(:foo, :string)).to be_string
    expect(parse(:foo, :boolean)).to be_boolean
    expect(parse(:foo, :numeric)).to be_numeric
  end

  it "raises an error on method missing" do
    expect do
      parse(:foo, :string).unknown?
    end.to raise_error(NoMethodError)
  end

  describe "#usage" do
    it "returns usage for string types" do
      expect(parse(:foo, :string).usage).to eq("[--foo=FOO]")
    end

    it "returns usage for numeric types" do
      expect(parse(:foo, :numeric).usage).to eq("[--foo=N]")
    end

    it "returns usage for array types" do
      expect(parse(:foo, :array).usage).to eq("[--foo=one two three]")
    end

    it "returns usage for hash types" do
      expect(parse(:foo, :hash).usage).to eq("[--foo=key:value]")
    end

    it "returns usage for boolean types" do
      expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo], [--skip-foo]")
    end

    it "does not use padding when no aliases are given" do
      expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo], [--skip-foo]")
    end

    it "documents a negative option when boolean" do
      expect(parse(:foo, :boolean).usage).to include("[--no-foo]")
    end

    it "does not document a negative option for a negative boolean" do
      expect(parse(:'no-foo', :boolean).usage).not_to include("[--no-no-foo]")
      expect(parse(:'no-foo', :boolean).usage).not_to include("[--skip-no-foo]")
      expect(parse(:'skip-foo', :boolean).usage).not_to include("[--no-skip-foo]")
      expect(parse(:'skip-foo', :boolean).usage).not_to include("[--skip-skip-foo]")
    end

    it "does not document a negative option for an underscored negative boolean" do
      expect(parse(:no_foo, :boolean).usage).not_to include("[--no-no-foo]")
    end

    it "documents a negative option for a positive boolean starting with 'no'" do
      expect(parse(:'nougat', :boolean).usage).to include("[--no-nougat]")
    end

    it "uses banner when supplied" do
      expect(option(:foo, required: false, type: :string, banner: "BAR").usage).to eq("[--foo=BAR]")
    end

    it "checks when banner is an empty string" do
      expect(option(:foo, required: false, type: :string, banner: "").usage).to eq("[--foo]")
    end

    describe "with required values" do
      it "does not show the usage between brackets" do
        expect(parse(:foo, :required).usage).to eq("--foo=FOO")
      end
    end

    describe "with aliases" do
      it "does not show the usage between brackets" do
        expect(parse([:foo, "-f", "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
      end

      it "does not negate the aliases" do
        expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo], [--skip-foo]")
      end

      it "normalizes the aliases" do
        expect(parse([:foo, :f, "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
      end
    end
  end

  describe "#print_default" do
    it "prints boolean with true default value" do
      expect(option(:foo, {
        required: false,
        type: :boolean,
        default: true
      }).print_default).to eq(true)
    end
    it "prints boolean with false default value" do
      expect(option(:foo, {
        required: false,
        type: :boolean,
        default: false
      }).print_default).to eq(false)
    end
  end
end