cri 2.6.1 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,488 +1,565 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Cri::CommandTestCase < Cri::TestCase
4
-
5
- def simple_cmd
6
- Cri::Command.define do
7
- name 'moo'
8
- usage 'moo [options] arg1 arg2 ...'
9
- summary 'does stuff'
10
- description 'This command does a lot of stuff.'
11
-
12
- option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
13
- $stdout.puts "#{cmd.name}:#{value}"
14
- end
15
- required :b, :bbb, 'opt b'
16
- optional :c, :ccc, 'opt c'
17
- flag :d, :ddd, 'opt d'
18
- forbidden :e, :eee, 'opt e'
3
+ module Cri
4
+ class CommandTestCase < Cri::TestCase
5
+ def simple_cmd
6
+ Cri::Command.define do
7
+ name 'moo'
8
+ usage 'moo [options] arg1 arg2 ...'
9
+ summary 'does stuff'
10
+ description 'This command does a lot of stuff.'
11
+
12
+ option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
13
+ $stdout.puts "#{cmd.name}:#{value}"
14
+ end
15
+ required :b, :bbb, 'opt b'
16
+ optional :c, :ccc, 'opt c'
17
+ flag :d, :ddd, 'opt d'
18
+ forbidden :e, :eee, 'opt e'
19
19
 
20
- run do |opts, args, c|
21
- $stdout.puts "Awesome #{c.name}!"
20
+ run do |opts, args, c|
21
+ $stdout.puts "Awesome #{c.name}!"
22
22
 
23
- $stdout.puts args.join(',')
23
+ $stdout.puts args.join(',')
24
24
 
25
- opts_strings = []
26
- opts.each_pair { |k,v| opts_strings << "#{k}=#{v}" }
27
- $stdout.puts opts_strings.sort.join(',')
25
+ opts_strings = []
26
+ opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
27
+ $stdout.puts opts_strings.sort.join(',')
28
+ end
28
29
  end
29
30
  end
30
- end
31
31
 
32
- def bare_cmd
33
- Cri::Command.define do
34
- name 'moo'
32
+ def bare_cmd
33
+ Cri::Command.define do
34
+ name 'moo'
35
35
 
36
- run do |opts, args|
36
+ run do |_opts, _args|
37
+ end
37
38
  end
38
39
  end
39
- end
40
40
 
41
- def nested_cmd
42
- super_cmd = Cri::Command.define do
43
- name 'super'
44
- usage 'super [command] [options] [arguments]'
45
- summary 'does super stuff'
46
- description 'This command does super stuff.'
41
+ def nested_cmd
42
+ super_cmd = Cri::Command.define do
43
+ name 'super'
44
+ usage 'super [command] [options] [arguments]'
45
+ summary 'does super stuff'
46
+ description 'This command does super stuff.'
47
47
 
48
- option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
49
- $stdout.puts "#{cmd.name}:#{value}"
48
+ option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
49
+ $stdout.puts "#{cmd.name}:#{value}"
50
+ end
51
+ required :b, :bbb, 'opt b'
52
+ optional :c, :ccc, 'opt c'
53
+ flag :d, :ddd, 'opt d'
54
+ forbidden :e, :eee, 'opt e'
50
55
  end
51
- required :b, :bbb, 'opt b'
52
- optional :c, :ccc, 'opt c'
53
- flag :d, :ddd, 'opt d'
54
- forbidden :e, :eee, 'opt e'
55
- end
56
56
 
57
- super_cmd.define_command do
58
- name 'sub'
59
- aliases 'sup'
60
- usage 'sub [options]'
61
- summary 'does subby stuff'
62
- description 'This command does subby stuff.'
57
+ super_cmd.define_command do
58
+ name 'sub'
59
+ aliases 'sup'
60
+ usage 'sub [options]'
61
+ summary 'does subby stuff'
62
+ description 'This command does subby stuff.'
63
63
 
64
- option :m, :mmm, 'opt m', :argument => :optional
65
- required :n, :nnn, 'opt n'
66
- optional :o, :ooo, 'opt o'
67
- flag :p, :ppp, 'opt p'
68
- forbidden :q, :qqq, 'opt q'
64
+ option :m, :mmm, 'opt m', :argument => :optional
65
+ required :n, :nnn, 'opt n'
66
+ optional :o, :ooo, 'opt o'
67
+ flag :p, :ppp, 'opt p'
68
+ forbidden :q, :qqq, 'opt q'
69
69
 
70
- run do |opts, args|
71
- $stdout.puts "Sub-awesome!"
70
+ run do |opts, args|
71
+ $stdout.puts 'Sub-awesome!'
72
72
 
73
- $stdout.puts args.join(',')
73
+ $stdout.puts args.join(',')
74
74
 
75
- opts_strings = []
76
- opts.each_pair { |k,v| opts_strings << "#{k}=#{v}" }
77
- $stdout.puts opts_strings.join(',')
75
+ opts_strings = []
76
+ opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
77
+ $stdout.puts opts_strings.join(',')
78
+ end
78
79
  end
79
- end
80
80
 
81
- super_cmd.define_command do
82
- name 'sink'
83
- usage 'sink thing_to_sink'
84
- summary 'sinks stuff'
85
- description 'Sinks stuff (like ships and the like).'
81
+ super_cmd.define_command do
82
+ name 'sink'
83
+ usage 'sink thing_to_sink'
84
+ summary 'sinks stuff'
85
+ description 'Sinks stuff (like ships and the like).'
86
86
 
87
- run do |opts, args|
87
+ run do |_opts, _args|
88
+ end
88
89
  end
89
- end
90
90
 
91
- super_cmd
92
- end
91
+ super_cmd
92
+ end
93
93
 
94
- def nested_cmd_with_run_block
95
- super_cmd = Cri::Command.define do
96
- name 'super'
97
- usage 'super [command] [options] [arguments]'
98
- summary 'does super stuff'
99
- description 'This command does super stuff.'
94
+ def nested_cmd_with_run_block
95
+ super_cmd = Cri::Command.define do
96
+ name 'super'
97
+ usage 'super [command] [options] [arguments]'
98
+ summary 'does super stuff'
99
+ description 'This command does super stuff.'
100
100
 
101
- run do |opts, args|
102
- $stdout.puts "super"
101
+ run do |_opts, _args|
102
+ $stdout.puts 'super'
103
+ end
103
104
  end
104
- end
105
105
 
106
- super_cmd.define_command do
107
- name 'sub'
108
- aliases 'sup'
109
- usage 'sub [options]'
110
- summary 'does subby stuff'
111
- description 'This command does subby stuff.'
106
+ super_cmd.define_command do
107
+ name 'sub'
108
+ aliases 'sup'
109
+ usage 'sub [options]'
110
+ summary 'does subby stuff'
111
+ description 'This command does subby stuff.'
112
112
 
113
- run do |opts, args|
114
- $stdout.puts "sub"
113
+ run do |_opts, _args|
114
+ $stdout.puts 'sub'
115
+ end
115
116
  end
117
+
118
+ super_cmd
116
119
  end
117
120
 
118
- super_cmd
119
- end
121
+ def test_invoke_simple_without_opts_or_args
122
+ out, err = capture_io_while do
123
+ simple_cmd.run(%w())
124
+ end
120
125
 
121
- def test_invoke_simple_without_opts_or_args
122
- out, err = capture_io_while do
123
- simple_cmd.run(%w())
126
+ assert_equal ['Awesome moo!', '', ''], lines(out)
127
+ assert_equal [], lines(err)
124
128
  end
125
129
 
126
- assert_equal [ 'Awesome moo!', '', '' ], lines(out)
127
- assert_equal [], lines(err)
128
- end
130
+ def test_invoke_simple_with_args
131
+ out, err = capture_io_while do
132
+ simple_cmd.run(%w(abc xyz))
133
+ end
129
134
 
130
- def test_invoke_simple_with_args
131
- out, err = capture_io_while do
132
- simple_cmd.run(%w(abc xyz))
135
+ assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out)
136
+ assert_equal [], lines(err)
133
137
  end
134
138
 
135
- assert_equal [ 'Awesome moo!', 'abc,xyz', '' ], lines(out)
136
- assert_equal [], lines(err)
137
- end
139
+ def test_invoke_simple_with_opts
140
+ out, err = capture_io_while do
141
+ simple_cmd.run(%w(-c -b x))
142
+ end
138
143
 
139
- def test_invoke_simple_with_opts
140
- out, err = capture_io_while do
141
- simple_cmd.run(%w(-c -b x))
144
+ assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out)
145
+ assert_equal [], lines(err)
142
146
  end
143
147
 
144
- assert_equal [ 'Awesome moo!', '', 'bbb=x,ccc=true' ], lines(out)
145
- assert_equal [], lines(err)
146
- end
147
-
148
- def test_invoke_simple_with_missing_opt_arg
149
- out, err = capture_io_while do
150
- assert_raises SystemExit do
151
- simple_cmd.run(%w( -b ))
148
+ def test_invoke_simple_with_missing_opt_arg
149
+ out, err = capture_io_while do
150
+ assert_raises SystemExit do
151
+ simple_cmd.run(%w( -b ))
152
+ end
152
153
  end
153
- end
154
154
 
155
- assert_equal [], lines(out)
156
- assert_equal [ "moo: option requires an argument -- b" ], lines(err)
157
- end
155
+ assert_equal [], lines(out)
156
+ assert_equal ['moo: option requires an argument -- b'], lines(err)
157
+ end
158
158
 
159
- def test_invoke_simple_with_illegal_opt
160
- out, err = capture_io_while do
161
- assert_raises SystemExit do
162
- simple_cmd.run(%w( -z ))
159
+ def test_invoke_simple_with_illegal_opt
160
+ out, err = capture_io_while do
161
+ assert_raises SystemExit do
162
+ simple_cmd.run(%w( -z ))
163
+ end
163
164
  end
165
+
166
+ assert_equal [], lines(out)
167
+ assert_equal ['moo: illegal option -- z'], lines(err)
164
168
  end
165
169
 
166
- assert_equal [], lines(out)
167
- assert_equal [ "moo: illegal option -- z" ], lines(err)
168
- end
170
+ def test_invoke_simple_with_opt_with_block
171
+ out, err = capture_io_while do
172
+ simple_cmd.run(%w( -a 123 ))
173
+ end
169
174
 
170
- def test_invoke_simple_with_opt_with_block
171
- out, err = capture_io_while do
172
- simple_cmd.run(%w( -a 123 ))
175
+ assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
176
+ assert_equal [], lines(err)
173
177
  end
174
178
 
175
- assert_equal [ 'moo:123', 'Awesome moo!', '', 'aaa=123' ], lines(out)
176
- assert_equal [], lines(err)
177
- end
178
-
179
- def test_invoke_nested_without_opts_or_args
180
- out, err = capture_io_while do
181
- assert_raises SystemExit do
182
- nested_cmd.run(%w())
179
+ def test_invoke_nested_without_opts_or_args
180
+ out, err = capture_io_while do
181
+ assert_raises SystemExit do
182
+ nested_cmd.run(%w())
183
+ end
183
184
  end
185
+
186
+ assert_equal [], lines(out)
187
+ assert_equal ['super: no command given'], lines(err)
184
188
  end
185
189
 
186
- assert_equal [ ], lines(out)
187
- assert_equal [ 'super: no command given' ], lines(err)
188
- end
190
+ def test_invoke_nested_with_correct_command_name
191
+ out, err = capture_io_while do
192
+ nested_cmd.run(%w( sub ))
193
+ end
189
194
 
190
- def test_invoke_nested_with_correct_command_name
191
- out, err = capture_io_while do
192
- nested_cmd.run(%w( sub ))
195
+ assert_equal ['Sub-awesome!', '', ''], lines(out)
196
+ assert_equal [], lines(err)
193
197
  end
194
198
 
195
- assert_equal [ 'Sub-awesome!', '', '' ], lines(out)
196
- assert_equal [ ], lines(err)
197
- end
198
-
199
- def test_invoke_nested_with_incorrect_command_name
200
- out, err = capture_io_while do
201
- assert_raises SystemExit do
202
- nested_cmd.run(%w( oogabooga ))
199
+ def test_invoke_nested_with_incorrect_command_name
200
+ out, err = capture_io_while do
201
+ assert_raises SystemExit do
202
+ nested_cmd.run(%w( oogabooga ))
203
+ end
203
204
  end
204
- end
205
205
 
206
- assert_equal [ ], lines(out)
207
- assert_equal [ "super: unknown command 'oogabooga'" ], lines(err)
208
- end
206
+ assert_equal [], lines(out)
207
+ assert_equal ["super: unknown command 'oogabooga'"], lines(err)
208
+ end
209
209
 
210
- def test_invoke_nested_with_ambiguous_command_name
211
- out, err = capture_io_while do
212
- assert_raises SystemExit do
213
- nested_cmd.run(%w( s ))
210
+ def test_invoke_nested_with_ambiguous_command_name
211
+ out, err = capture_io_while do
212
+ assert_raises SystemExit do
213
+ nested_cmd.run(%w( s ))
214
+ end
214
215
  end
216
+
217
+ assert_equal [], lines(out)
218
+ assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
215
219
  end
216
220
 
217
- assert_equal [ ], lines(out)
218
- assert_equal [ "super: 's' is ambiguous:", " sink sub" ], lines(err)
219
- end
221
+ def test_invoke_nested_with_alias
222
+ out, err = capture_io_while do
223
+ nested_cmd.run(%w( sup ))
224
+ end
220
225
 
221
- def test_invoke_nested_with_alias
222
- out, err = capture_io_while do
223
- nested_cmd.run(%w( sup ))
226
+ assert_equal ['Sub-awesome!', '', ''], lines(out)
227
+ assert_equal [], lines(err)
224
228
  end
225
229
 
226
- assert_equal [ 'Sub-awesome!', '', '' ], lines(out)
227
- assert_equal [ ], lines(err)
228
- end
230
+ def test_invoke_nested_with_options_before_command
231
+ out, err = capture_io_while do
232
+ nested_cmd.run(%w( -a 666 sub ))
233
+ end
229
234
 
230
- def test_invoke_nested_with_options_before_command
231
- out, err = capture_io_while do
232
- nested_cmd.run(%w( -a 666 sub ))
235
+ assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
236
+ assert_equal [], lines(err)
233
237
  end
234
238
 
235
- assert_equal [ 'super:666', 'Sub-awesome!', '', 'aaa=666' ], lines(out)
236
- assert_equal [ ], lines(err)
237
- end
239
+ def test_invoke_nested_with_run_block
240
+ out, err = capture_io_while do
241
+ nested_cmd_with_run_block.run(%w())
242
+ end
238
243
 
239
- def test_invoke_nested_with_run_block
240
- out, err = capture_io_while do
241
- nested_cmd_with_run_block.run(%w())
242
- end
244
+ assert_equal ['super'], lines(out)
245
+ assert_equal [], lines(err)
243
246
 
244
- assert_equal [ 'super' ], lines(out)
245
- assert_equal [ ], lines(err)
247
+ out, err = capture_io_while do
248
+ nested_cmd_with_run_block.run(%w( sub ))
249
+ end
246
250
 
247
- out, err = capture_io_while do
248
- nested_cmd_with_run_block.run(%w( sub ))
251
+ assert_equal ['sub'], lines(out)
252
+ assert_equal [], lines(err)
249
253
  end
250
254
 
251
- assert_equal [ 'sub' ], lines(out)
252
- assert_equal [ ], lines(err)
253
- end
255
+ def test_help_nested
256
+ def $stdout.tty?
257
+ true
258
+ end
254
259
 
255
- def test_help_nested
256
- def $stdout.tty? ; true ; end
260
+ help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
257
261
 
258
- help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
262
+ assert help.include?("USAGE\e[0m\e[0m\n \e[32msuper\e[0m \e[32msub\e[0m [options]\n")
263
+ end
259
264
 
260
- assert help.include?("USAGE\e[0m\e[0m\n \e[32msuper\e[0m \e[32msub\e[0m [options]\n")
261
- end
265
+ def test_help_with_and_without_colors
266
+ def $stdout.tty?
267
+ true
268
+ end
269
+ help_on_tty = simple_cmd.help
270
+ def $stdout.tty?
271
+ false
272
+ end
273
+ help_not_on_tty = simple_cmd.help
262
274
 
263
- def test_help_with_and_without_colors
264
- def $stdout.tty? ; true ; end
265
- help_on_tty = simple_cmd.help
266
- def $stdout.tty? ; false ; end
267
- help_not_on_tty = simple_cmd.help
275
+ assert_includes help_on_tty, "\e[31mUSAGE\e[0m\e[0m\n \e[32mmoo"
276
+ assert_includes help_not_on_tty, "USAGE\n moo"
277
+ end
268
278
 
269
- assert_includes help_on_tty, "\e[31mUSAGE\e[0m\e[0m\n \e[32mmoo"
270
- assert_includes help_not_on_tty, "USAGE\n moo"
271
- end
279
+ def test_help_for_bare_cmd
280
+ bare_cmd.help
281
+ end
272
282
 
273
- def test_help_for_bare_cmd
274
- bare_cmd.help
275
- end
283
+ def test_help_with_optional_options
284
+ def $stdout.tty?
285
+ true
286
+ end
276
287
 
277
- def test_help_with_optional_options
278
- def $stdout.tty? ; true ; end
288
+ cmd = Cri::Command.define do
289
+ name 'build'
290
+ flag :s, nil, 'short'
291
+ flag nil, :long, 'long'
292
+ end
293
+ help = cmd.help
279
294
 
280
- cmd = Cri::Command.define do
281
- name 'build'
282
- flag :s, nil, 'short'
283
- flag nil, :long, 'long'
295
+ assert_match(/--long.*-s/m, help)
296
+ assert_match(/^ \e\[33m--long\e\[0m long$/, help)
297
+ assert_match(/^ \e\[33m-s\e\[0m short$/, help)
284
298
  end
285
- help = cmd.help
286
-
287
- assert_match(/--long.*-s/m, help)
288
- assert_match(/^\e\[33m --long \e\[0mlong$/, help)
289
- assert_match(/^\e\[33m -s \e\[0mshort$/, help)
290
- end
291
299
 
292
- def test_help_with_multiple_groups
293
- help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
300
+ def test_help_with_different_option_types_short_and_long
301
+ def $stdout.tty?
302
+ true
303
+ end
294
304
 
295
- assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
296
- end
305
+ cmd = Cri::Command.define do
306
+ name 'build'
307
+ required :r, :required, 'required value'
308
+ flag :f, :flag, 'forbidden value'
309
+ optional :o, :optional, 'optional value'
310
+ end
311
+ help = cmd.help
297
312
 
298
- def test_modify_with_block_argument
299
- cmd = Cri::Command.define do |c|
300
- c.name 'build'
313
+ assert_match(/^ \e\[33m-r\e\[0m \e\[33m--required\e\[0m=<value> required value$/, help)
314
+ assert_match(/^ \e\[33m-f\e\[0m \e\[33m--flag\e\[0m forbidden value$/, help)
315
+ assert_match(/^ \e\[33m-o\e\[0m \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
301
316
  end
302
- assert_equal 'build', cmd.name
303
317
 
304
- cmd.modify do |c|
305
- c.name 'compile'
306
- end
318
+ def test_help_with_different_option_types_short
319
+ def $stdout.tty?
320
+ true
321
+ end
307
322
 
308
- assert_equal 'compile', cmd.name
309
- end
323
+ cmd = Cri::Command.define do
324
+ name 'build'
325
+ required :r, nil, 'required value'
326
+ flag :f, nil, 'forbidden value'
327
+ optional :o, nil, 'optional value'
328
+ end
329
+ help = cmd.help
310
330
 
311
- def test_modify_without_block_argument
312
- cmd = Cri::Command.define do
313
- name 'build'
331
+ assert_match(/^ \e\[33m-r\e\[0m <value> required value$/, help)
332
+ assert_match(/^ \e\[33m-f\e\[0m forbidden value$/, help)
333
+ assert_match(/^ \e\[33m-o\e\[0m \[<value>\] optional value$/, help)
314
334
  end
315
- assert_equal 'build', cmd.name
316
335
 
317
- cmd.modify do
318
- name 'compile'
336
+ def test_help_with_different_option_types_long
337
+ def $stdout.tty?
338
+ true
339
+ end
340
+
341
+ cmd = Cri::Command.define do
342
+ name 'build'
343
+ required nil, :required, 'required value'
344
+ flag nil, :flag, 'forbidden value'
345
+ optional nil, :optional, 'optional value'
346
+ end
347
+ help = cmd.help
348
+
349
+ assert_match(/^ \e\[33m--required\e\[0m=<value> required value$/, help)
350
+ assert_match(/^ \e\[33m--flag\e\[0m forbidden value$/, help)
351
+ assert_match(/^ \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
319
352
  end
320
353
 
321
- assert_equal 'compile', cmd.name
322
- end
354
+ def test_help_with_multiple_groups
355
+ help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
323
356
 
324
- def test_new_basic_root
325
- cmd = Cri::Command.new_basic_root.modify do
326
- name 'mytool'
357
+ assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
327
358
  end
328
359
 
329
- # Check option definitions
330
- assert_equal 1, cmd.option_definitions.size
331
- opt_def = cmd.option_definitions.to_a[0]
332
- assert_equal 'help', opt_def[:long]
360
+ def test_modify_with_block_argument
361
+ cmd = Cri::Command.define do |c|
362
+ c.name 'build'
363
+ end
364
+ assert_equal 'build', cmd.name
333
365
 
334
- # Check subcommand
335
- assert_equal 1, cmd.subcommands.size
336
- assert_equal 'help', cmd.subcommands.to_a[0].name
337
- end
366
+ cmd.modify do |c|
367
+ c.name 'compile'
368
+ end
338
369
 
339
- def test_define_with_block_argument
340
- cmd = Cri::Command.define do |c|
341
- c.name 'moo'
370
+ assert_equal 'compile', cmd.name
342
371
  end
343
372
 
344
- assert_equal 'moo', cmd.name
345
- end
373
+ def test_help_with_wrapped_options
374
+ def $stdout.tty?
375
+ true
376
+ end
377
+
378
+ cmd = Cri::Command.define do
379
+ name 'build'
380
+ flag nil, :longflag, 'This is an option with a very long description that should be wrapped'
381
+ end
382
+ help = cmd.help
346
383
 
347
- def test_define_without_block_argument
348
- cmd = Cri::Command.define do
349
- name 'moo'
384
+ assert_match(/^ \e\[33m--longflag\e\[0m This is an option with a very long description that$/, help)
385
+ assert_match(/^ should be wrapped$/, help)
350
386
  end
351
387
 
352
- assert_equal 'moo', cmd.name
353
- end
388
+ def test_modify_without_block_argument
389
+ cmd = Cri::Command.define do
390
+ name 'build'
391
+ end
392
+ assert_equal 'build', cmd.name
393
+
394
+ cmd.modify do
395
+ name 'compile'
396
+ end
354
397
 
355
- def test_define_subcommand_with_block_argument
356
- cmd = bare_cmd
357
- cmd.define_command do |c|
358
- c.name 'baresub'
398
+ assert_equal 'compile', cmd.name
359
399
  end
360
400
 
361
- assert_equal 'baresub', cmd.subcommands.to_a[0].name
362
- end
401
+ def test_new_basic_root
402
+ cmd = Cri::Command.new_basic_root.modify do
403
+ name 'mytool'
404
+ end
405
+
406
+ # Check option definitions
407
+ assert_equal 1, cmd.option_definitions.size
408
+ opt_def = cmd.option_definitions.to_a[0]
409
+ assert_equal 'help', opt_def[:long]
363
410
 
364
- def test_define_subcommand_without_block_argument
365
- cmd = bare_cmd
366
- cmd.define_command do
367
- name 'baresub'
411
+ # Check subcommand
412
+ assert_equal 1, cmd.subcommands.size
413
+ assert_equal 'help', cmd.subcommands.to_a[0].name
368
414
  end
369
415
 
370
- assert_equal 'baresub', cmd.subcommands.to_a[0].name
371
- end
416
+ def test_define_with_block_argument
417
+ cmd = Cri::Command.define do |c|
418
+ c.name 'moo'
419
+ end
372
420
 
373
- def test_backtrace_includes_filename
374
- error = assert_raises RuntimeError do
375
- Cri::Command.define('raise "boom"', 'mycommand.rb')
421
+ assert_equal 'moo', cmd.name
376
422
  end
377
423
 
378
- assert_match /mycommand.rb/, error.backtrace.join("\n")
379
- end
424
+ def test_define_without_block_argument
425
+ cmd = Cri::Command.define do
426
+ name 'moo'
427
+ end
380
428
 
381
- def test_hidden_commands_single
382
- cmd = nested_cmd
383
- subcmd = simple_cmd
384
- cmd.add_command subcmd
385
- subcmd.modify do |c|
386
- c.name 'old-and-deprecated'
387
- c.summary 'does stuff the ancient, totally deprecated way'
388
- c.be_hidden
429
+ assert_equal 'moo', cmd.name
389
430
  end
390
431
 
391
- refute cmd.help.include?('hidden commands omitted')
392
- assert cmd.help.include?('hidden command omitted')
393
- refute cmd.help.include?('old-and-deprecated')
432
+ def test_define_subcommand_with_block_argument
433
+ cmd = bare_cmd
434
+ cmd.define_command do |c|
435
+ c.name 'baresub'
436
+ end
394
437
 
395
- refute cmd.help(:verbose => true).include?('hidden commands omitted')
396
- refute cmd.help(:verbose => true).include?('hidden command omitted')
397
- assert cmd.help(:verbose => true).include?('old-and-deprecated')
398
- end
438
+ assert_equal 'baresub', cmd.subcommands.to_a[0].name
439
+ end
399
440
 
400
- def test_hidden_commands_multiple
401
- cmd = nested_cmd
441
+ def test_define_subcommand_without_block_argument
442
+ cmd = bare_cmd
443
+ cmd.define_command do
444
+ name 'baresub'
445
+ end
402
446
 
403
- subcmd = simple_cmd
404
- cmd.add_command subcmd
405
- subcmd.modify do |c|
406
- c.name 'first'
407
- c.summary 'does stuff first'
447
+ assert_equal 'baresub', cmd.subcommands.to_a[0].name
408
448
  end
409
449
 
410
- subcmd = simple_cmd
411
- cmd.add_command subcmd
412
- subcmd.modify do |c|
413
- c.name 'old-and-deprecated'
414
- c.summary 'does stuff the old, deprecated way'
415
- c.be_hidden
450
+ def test_backtrace_includes_filename
451
+ error = assert_raises RuntimeError do
452
+ Cri::Command.define('raise "boom"', 'mycommand.rb')
453
+ end
454
+
455
+ assert_match(/mycommand.rb/, error.backtrace.join("\n"))
416
456
  end
417
457
 
418
- subcmd = simple_cmd
419
- cmd.add_command subcmd
420
- subcmd.modify do |c|
421
- c.name 'ancient-and-deprecated'
422
- c.summary 'does stuff the ancient, reallydeprecated way'
423
- c.be_hidden
458
+ def test_hidden_commands_single
459
+ cmd = nested_cmd
460
+ subcmd = simple_cmd
461
+ cmd.add_command subcmd
462
+ subcmd.modify do |c|
463
+ c.name 'old-and-deprecated'
464
+ c.summary 'does stuff the ancient, totally deprecated way'
465
+ c.be_hidden
466
+ end
467
+
468
+ refute cmd.help.include?('hidden commands omitted')
469
+ assert cmd.help.include?('hidden command omitted')
470
+ refute cmd.help.include?('old-and-deprecated')
471
+
472
+ refute cmd.help(:verbose => true).include?('hidden commands omitted')
473
+ refute cmd.help(:verbose => true).include?('hidden command omitted')
474
+ assert cmd.help(:verbose => true).include?('old-and-deprecated')
424
475
  end
425
476
 
426
- assert cmd.help.include?('hidden commands omitted')
427
- refute cmd.help.include?('hidden command omitted')
428
- refute cmd.help.include?('old-and-deprecated')
429
- refute cmd.help.include?('ancient-and-deprecated')
477
+ def test_hidden_commands_multiple
478
+ cmd = nested_cmd
430
479
 
431
- refute cmd.help(:verbose => true).include?('hidden commands omitted')
432
- refute cmd.help(:verbose => true).include?('hidden command omitted')
433
- assert cmd.help(:verbose => true).include?('old-and-deprecated')
434
- assert cmd.help(:verbose => true).include?('ancient-and-deprecated')
480
+ subcmd = simple_cmd
481
+ cmd.add_command subcmd
482
+ subcmd.modify do |c|
483
+ c.name 'first'
484
+ c.summary 'does stuff first'
485
+ end
435
486
 
436
- pattern = /ancient-and-deprecated.*first.*old-and-deprecated/m
437
- assert_match(pattern, cmd.help(:verbose => true))
438
- end
487
+ subcmd = simple_cmd
488
+ cmd.add_command subcmd
489
+ subcmd.modify do |c|
490
+ c.name 'old-and-deprecated'
491
+ c.summary 'does stuff the old, deprecated way'
492
+ c.be_hidden
493
+ end
439
494
 
440
- def test_run_with_raw_args
441
- cmd = Cri::Command.define do
442
- name 'moo'
443
- run do |opts, args|
444
- puts "args=#{args.join(',')} args.raw=#{args.raw.join(',')}"
495
+ subcmd = simple_cmd
496
+ cmd.add_command subcmd
497
+ subcmd.modify do |c|
498
+ c.name 'ancient-and-deprecated'
499
+ c.summary 'does stuff the ancient, reallydeprecated way'
500
+ c.be_hidden
445
501
  end
446
- end
447
502
 
448
- out, err = capture_io_while do
449
- cmd.run(%w( foo -- bar ))
450
- end
451
- assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
452
- end
503
+ assert cmd.help.include?('hidden commands omitted')
504
+ refute cmd.help.include?('hidden command omitted')
505
+ refute cmd.help.include?('old-and-deprecated')
506
+ refute cmd.help.include?('ancient-and-deprecated')
453
507
 
454
- def test_run_without_block
455
- cmd = Cri::Command.define do
456
- name 'moo'
457
- end
508
+ refute cmd.help(:verbose => true).include?('hidden commands omitted')
509
+ refute cmd.help(:verbose => true).include?('hidden command omitted')
510
+ assert cmd.help(:verbose => true).include?('old-and-deprecated')
511
+ assert cmd.help(:verbose => true).include?('ancient-and-deprecated')
458
512
 
459
- assert_raises(Cri::NotImplementedError) do
460
- cmd.run([])
513
+ pattern = /ancient-and-deprecated.*first.*old-and-deprecated/m
514
+ assert_match(pattern, cmd.help(:verbose => true))
461
515
  end
462
- end
463
516
 
464
- def test_runner_with_raw_args
465
- cmd = Cri::Command.define do
466
- name 'moo'
467
- runner(Class.new(Cri::CommandRunner) do
468
- def run
469
- puts "args=#{arguments.join(',')} args.raw=#{arguments.raw.join(',')}"
517
+ def test_run_with_raw_args
518
+ cmd = Cri::Command.define do
519
+ name 'moo'
520
+ run do |_opts, args|
521
+ puts "args=#{args.join(',')} args.raw=#{args.raw.join(',')}"
470
522
  end
471
- end)
523
+ end
524
+
525
+ out, _err = capture_io_while do
526
+ cmd.run(%w( foo -- bar ))
527
+ end
528
+ assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
529
+ end
530
+
531
+ def test_run_without_block
532
+ cmd = Cri::Command.define do
533
+ name 'moo'
534
+ end
535
+
536
+ assert_raises(Cri::NotImplementedError) do
537
+ cmd.run([])
538
+ end
472
539
  end
473
540
 
474
- out, err = capture_io_while do
475
- cmd.run(%w( foo -- bar ))
541
+ def test_runner_with_raw_args
542
+ cmd = Cri::Command.define do
543
+ name 'moo'
544
+ runner(Class.new(Cri::CommandRunner) do
545
+ def run
546
+ puts "args=#{arguments.join(',')} args.raw=#{arguments.raw.join(',')}"
547
+ end
548
+ end)
549
+ end
550
+
551
+ out, _err = capture_io_while do
552
+ cmd.run(%w( foo -- bar ))
553
+ end
554
+ assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
476
555
  end
477
- assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
478
- end
479
556
 
480
- def test_compare
481
- foo = Cri::Command.define { name 'foo' }
482
- bar = Cri::Command.define { name 'bar' }
483
- qux = Cri::Command.define { name 'qux' }
557
+ def test_compare
558
+ foo = Cri::Command.define { name 'foo' }
559
+ bar = Cri::Command.define { name 'bar' }
560
+ qux = Cri::Command.define { name 'qux' }
484
561
 
485
- assert_equal [ bar, foo, qux ], [ foo, bar, qux ].sort
562
+ assert_equal [bar, foo, qux], [foo, bar, qux].sort
563
+ end
486
564
  end
487
-
488
565
  end