File: definition_spec.rb

package info (click to toggle)
ruby-clamp 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: ruby: 3,824; sh: 69; makefile: 4
file content (343 lines) | stat: -rw-r--r-- 7,745 bytes parent folder | download
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# frozen_string_literal: true

require "spec_helper"

describe Clamp::Option::Definition do

  context "with String argument" do

    let(:option) do
      described_class.new("--key-file", "FILE", "SSH identity")
    end

    it "has a long_switch" do
      expect(option.long_switch).to eq "--key-file"
    end

    it "has a type" do
      expect(option.type).to eq "FILE"
    end

    it "has a description" do
      expect(option.description).to eq "SSH identity"
    end

    describe "#attribute_name" do

      it "is derived from the (long) switch" do
        expect(option.attribute_name).to eq "key_file"
      end

      it "can be overridden" do
        option = described_class.new("--key-file", "FILE", "SSH identity", attribute_name: "ssh_identity")
        expect(option.attribute_name).to eq "ssh_identity"
      end

    end

    describe "#write_method" do

      it "is derived from the attribute_name" do
        expect(option.write_method).to eq "key_file="
      end

    end

    describe "#default_value" do

      it "defaults to nil" do
        option = described_class.new("-n", "N", "iterations")
        expect(option.default_value).to be_nil
      end

      it "can be overridden" do
        option = described_class.new("-n", "N", "iterations", default: 1)
        expect(option.default_value).to eq 1
      end

    end

    describe "#help" do

      it "combines switch, type and description" do
        expect(option.help).to eq ["--key-file FILE", "SSH identity"]
      end

    end

  end

  context "when flag" do

    let(:option) do
      described_class.new("--verbose", :flag, "Blah blah blah")
    end

    describe "#default_conversion_block" do

      context "with 'true' value" do
        it "converts to true" do
          expect(option.default_conversion_block.call("true")).to be true
        end
      end

      context "with 'yes' value" do
        it "converts to true" do
          expect(option.default_conversion_block.call("yes")).to be true
        end
      end

      context "with 'false' value" do
        it "converts to false" do
          expect(option.default_conversion_block.call("false")).to be false
        end
      end

      context "with 'no' value" do
        it "converts to false" do
          expect(option.default_conversion_block.call("no")).to be false
        end
      end

    end

    describe "#help" do

      it "excludes option argument" do
        expect(option.help).to eq ["--verbose", "Blah blah blah"]
      end

    end

  end

  context "when negatable flag" do

    let(:option) do
      described_class.new("--[no-]force", :flag, "Force installation")
    end

    describe "positive form" do
      it "handles this" do
        expect(option.handles?("--force")).to be true
      end
    end

    describe "negative form" do
      it "handles this" do
        expect(option.handles?("--no-force")).to be true
      end
    end

    describe "#flag_set?" do

      describe "positive variant" do
        it "returns true" do
          expect(option.flag_set?("--force")).to be true
        end
      end

      describe "negative variant" do
        it "returns false" do
          expect(option.flag_set?("--no-force")).to be false
        end
      end

    end

    describe "#attribute_name" do

      it "is derived from the (long) switch" do
        expect(option.attribute_name).to eq "force"
      end

    end

  end

  context "with both short and long switches" do

    let(:option) do
      described_class.new(["-k", "--key-file"], "FILE", "SSH identity")
    end

    describe "long switch" do
      it "handles this" do
        expect(option.handles?("--key-file")).to be true
      end
    end

    describe "short switch" do
      it "handles this" do
        expect(option.handles?("-k")).to be true
      end
    end

    describe "#help" do

      it "includes both switches" do
        expect(option.help).to eq ["-k, --key-file FILE", "SSH identity"]
      end

    end

  end

  context "with an associated environment variable" do

    let(:option) do
      described_class.new("-x", "X", "mystery option", environment_variable: "APP_X")
    end

    describe "#help" do

      it "describes environment variable" do
        expect(option.help).to eq ["-x X", "mystery option (default: $APP_X)"]
      end

    end

    context "with a default value" do

      let(:option) do
        described_class.new("-x", "X", "mystery option", environment_variable: "APP_X", default: "xyz")
      end

      describe "#help" do

        it "describes both environment variable and default" do
          expect(option.help).to eq ["-x X", %{mystery option (default: $APP_X, or "xyz")}]
        end

      end

    end

    context "when it is required" do

      let(:option) do
        described_class.new("-x", "X", "mystery option", environment_variable: "APP_X", required: true)
      end

      describe "#help" do

        it "describes the environment variable as the default" do
          expect(option.help).to eql ["-x X", %{mystery option (required, default: $APP_X)}]
        end

      end

    end

  end

  context "when multivalued" do

    let(:option) do
      described_class.new(["-H", "--header"], "HEADER", "extra header", multivalued: true)
    end

    it "is multivalued" do
      expect(option).to be_multivalued
    end

    describe "#default_value" do

      it "defaults to an empty Array" do
        expect(option.default_value).to be_empty
      end

      it "can be overridden" do
        option = described_class.new("-H", "HEADER", "extra header", multivalued: true, default: [1, 2, 3])
        expect(option.default_value).to eq [1, 2, 3]
      end

    end

    describe "#attribute_name" do

      it "gets a _list suffix" do
        expect(option.attribute_name).to eq "header_list"
      end

    end

    describe "#append_method" do

      it "is derived from the attribute_name" do
        expect(option.append_method).to eq "append_to_header_list"
      end

    end

  end

  describe "in subcommand" do

    let(:command_class) do

      Class.new(Clamp::Command) do
        subcommand "foo", "FOO!" do
          option "--bar", "BAR", "Bars foo."
        end
      end

    end

    describe "Command#help" do

      it "includes help for each option exactly once" do
        subcommand = command_class.send(:find_subcommand, "foo")
        subcommand_help = subcommand.subcommand_class.help("")
        expect(subcommand_help.lines.grep(/--bar BAR/).count).to eq 1
      end

    end

  end

  describe "a required option" do
    it "rejects :default" do
      expect do
        described_class.new("--key-file", "FILE", "SSH identity",
                            required: true, default: "hello")
      end.to raise_error(ArgumentError)
    end

    it "rejects :flag options" do
      expect do
        described_class.new("--awesome", :flag, "Be awesome?", required: true)
      end.to raise_error(ArgumentError)
    end
  end

  describe "a hidden option" do
    let(:option) { described_class.new("--unseen", :flag, "Something", hidden: true) }

    it "is hidden" do
      expect(option).to be_hidden
    end
  end

  describe "a hidden option in a command" do
    let(:command_class) do
      Class.new(Clamp::Command) do
        option "--unseen", :flag, "Something", hidden: true

        def execute
          # this space intentionally left blank
        end
      end
    end

    it "is not shown in the help" do
      expect(command_class.help("foo")).not_to match(/^ +--unseen +Something$/)
    end

    it "sets the expected accessor" do
      command = command_class.new("foo")
      command.run(["--unseen"])
      expect(command).to be_unseen
    end
  end
end