File: command_group_spec.rb

package info (click to toggle)
ruby-clamp 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 364 kB
  • sloc: ruby: 2,851; makefile: 4
file content (438 lines) | stat: -rw-r--r-- 8,919 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# frozen_string_literal: true

require "spec_helper"

describe Clamp::Command do

  extend CommandFactory
  include OutputCapture

  context "with subcommands" do

    given_command "flipflop" do

      def execute
        puts message
      end

      subcommand "flip", "flip it" do
        def message
          "FLIPPED"
        end
      end

      subcommand "flop", "flop it\nfor extra flop" do
        def message
          "FLOPPED"
        end
      end

    end

    describe "flip command" do
      before do
        command.run(["flip"])
      end

      it "delegates to sub-commands" do
        expect(stdout).to match(/FLIPPED/)
      end
    end

    describe "flop command" do
      before do
        command.run(["flop"])
      end

      it "delegates to sub-commands" do
        expect(stdout).to match(/FLOPPED/)
      end
    end

    context "when executed with no subcommand" do

      it "triggers help" do
        expect do
          command.run([])
        end.to raise_error(Clamp::HelpWanted)
      end

    end

    describe "#help" do

      it "shows subcommand parameters in usage" do
        expect(command.help).to include("flipflop [OPTIONS] SUBCOMMAND [ARG] ...")
      end

      it "lists subcommands" do
        expect(command.help).to match(/Subcommands:\n +flip +flip it\n +flop +flop it/)
      end

      it "handles new lines in subcommand descriptions" do
        expect(command.help).to match(/flop +flop it\n +for extra flop/)
      end

    end

    describe ".find_subcommand_class" do

      it "finds subcommand classes" do
        flip_class = command_class.find_subcommand_class("flip")
        expect(flip_class.new("xx").message).to eq("FLIPPED")
      end

    end

  end

  context "with an aliased subcommand" do

    given_command "blah" do

      subcommand ["say", "talk"], "Say something" do

        parameter "WORD ...", "stuff to say"

        def execute
          puts word_list
        end

      end

    end

    describe "the first alias" do

      before do
        command.run(["say", "boo"])
      end

      it "responds to it" do
        expect(stdout).to match(/boo/)
      end

    end

    describe "the second alias" do

      before do
        command.run(["talk", "jive"])
      end

      it "responds to it" do
        expect(stdout).to match(/jive/)
      end

    end

    describe "#help" do

      it "lists all aliases" do
        help = command.help
        expect(help).to match(/say, talk .* Say something/)
      end

    end

  end

  context "with nested subcommands" do

    given_command "fubar" do

      subcommand "foo", "Foo!" do

        subcommand "bar", "Baaaa!" do

          def self.this_is_bar; end

          def execute
            puts "FUBAR"
          end

        end

      end

    end

    it "delegates multiple levels" do
      command.run(["foo", "bar"])
      expect(stdout).to match(/FUBAR/)
    end

    describe ".find_subcommand_class" do

      it "finds nested subcommands" do
        expect(command_class.find_subcommand_class("foo", "bar")).to respond_to(:this_is_bar)
      end

    end

  end

  context "with a default subcommand" do

    given_command "admin" do

      self.default_subcommand = "status"

      subcommand "status", "Show status" do

        def execute
          puts "All good!"
        end

      end

    end

    context "when executed with no subcommand" do

      it "invokes the default subcommand" do
        command.run([])
        expect(stdout).to match(/All good/)
      end

    end

  end

  context "with a default subcommand, declared the old way" do

    given_command "admin" do

      default_subcommand "status", "Show status" do

        def execute
          puts "All good!"
        end

      end

    end

    context "when executed with no subcommand" do

      it "invokes the default subcommand" do
        command.run([])
        expect(stdout).to match(/All good/)
      end

    end

  end

  context "when declaring a default subcommand after subcommands" do

    let(:command) do
      Class.new(Clamp::Command) do

        subcommand "status", "Show status" do

          def execute
            puts "All good!"
          end

        end

      end
    end

    it "is not supported" do

      expect do
        command.default_subcommand = "status"
      end.to raise_error(/default_subcommand must be defined before subcommands/)

    end

  end

  context "with subcommands, declared after a parameter" do

    given_command "with" do

      parameter "THING", "the thing"

      subcommand "spit", "spit it" do
        def execute
          puts "spat the #{thing}"
        end
      end

      subcommand "say", "say it" do
        subcommand "loud", "yell it" do
          def execute
            puts thing.upcase
          end
        end
      end

    end

    it "allows the parameter to be specified first" do
      command.run(["dummy", "spit"])
      expect(stdout.strip).to eq "spat the dummy"
    end

    it "passes the parameter down the stack" do
      command.run(["money", "say", "loud"])
      expect(stdout.strip).to eq "MONEY"
    end

    it "shows parameter in usage help" do
      command.run(["stuff", "say", "loud", "--help"])
    rescue Clamp::HelpWanted => e
      expect(e.command.invocation_path).to eq "with THING say loud"
    end

  end

  describe "each subcommand" do

    let(:command_class) do

      speed_options = Module.new do
        extend Clamp::Option::Declaration

        option "--speed", "SPEED", "how fast", default: "slowly"
      end

      Class.new(Clamp::Command) do

        option "--direction", "DIR", "which way", default: "home"

        include speed_options

        subcommand "move", "move in the appointed direction" do

          def execute
            motion = context[:motion] || "walking"
            puts "#{motion} #{direction} #{speed}"
          end

        end

      end
    end

    let(:command) do
      command_class.new("go")
    end

    it "accepts options defined in superclass (specified after the subcommand)" do
      command.run(["move", "--direction", "north"])
      expect(stdout).to match(/walking north/)
    end

    it "accepts options defined in superclass (specified before the subcommand)" do
      command.run(["--direction", "north", "move"])
      expect(stdout).to match(/walking north/)
    end

    it "accepts options defined in included modules" do
      command.run(["move", "--speed", "very quickly"])
      expect(stdout).to match(/walking home very quickly/)
    end

    it "has access to command context" do
      command = command_class.new("go", motion: "wandering")
      command.run(["move"])
      expect(stdout).to match(/wandering home/)
    end

  end

  context "with a subcommand, with options" do

    given_command "weeheehee" do
      option "--json", "JSON", "a json blob" do |option|
        print "parsing!"
        option
      end

      subcommand "woohoohoo", "like weeheehee but with more o" do
        def execute; end
      end
    end

    it "only parses options once" do
      command.run(["--json", '{"a":"b"}', "woohoohoo"])
      expect(stdout).to eq "parsing!"
    end

  end

  context "with an unknown subcommand" do

    let(:subcommand_missing) do
      Module.new do
        def subcommand_missing(_name)
          abort "there is no such thing"
        end
      end
    end

    let(:subcommand_missing_with_return) do
      Module.new do
        def subcommand_missing(_name)
          self.class.recognised_subcommands.first.subcommand_class
        end
      end
    end

    let(:command_class) do

      Class.new(Clamp::Command) do
        subcommand "test", "test subcommand" do
          def execute
            puts "known subcommand"
          end
        end

        def execute; end
      end
    end

    let(:command) do
      command_class.new("foo")
    end

    it "signals no such subcommand usage error" do
      expect { command.run(["foo"]) }.to raise_error(Clamp::UsageError, "No such sub-command 'foo'")
    end

    it "executes the subcommand missing method" do
      command.extend subcommand_missing
      expect { command.run(["foo"]) }.to raise_error(SystemExit, /there is no such thing/)
    end

    it "uses the subcommand class returned from subcommand_missing" do
      command.extend subcommand_missing_with_return
      command.run(["foo"])
      expect(stdout).to match(/known subcommand/)
    end

  end

  context "with a subcommand and required options" do

    given_command "movements" do
      option "--direction", "N|S|E|W", "bearing", required: true
      subcommand "hop", "Hop" do
        def execute
          puts "Hopping #{direction}"
        end
      end
    end

    it "allows options after the subcommand" do
      command.run(%w[hop --direction south])
      expect(stdout).to eq "Hopping south\n"
    end

  end

end