OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library args_test; | 5 library args_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
9 | 9 |
10 main() { | 10 main() { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); | 53 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); |
54 }); | 54 }); |
55 | 55 |
56 test('throws ArgumentError if the abbreviation is longer ' | 56 test('throws ArgumentError if the abbreviation is longer ' |
57 'than one character', () { | 57 'than one character', () { |
58 var parser = new ArgParser(); | 58 var parser = new ArgParser(); |
59 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); | 59 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); |
60 }); | 60 }); |
61 }); | 61 }); |
62 | 62 |
63 group('ArgParser.parse()', () { | 63 group('ArgParser.getDefault()', () { |
64 group('flags', () { | 64 test('returns the default value for an option', () { |
65 test('are true if present', () { | 65 var parser = new ArgParser(); |
66 var parser = new ArgParser(); | 66 parser.addOption('mode', defaultsTo: 'debug'); |
67 parser.addFlag('verbose'); | 67 expect(parser.getDefault('mode'), 'debug'); |
68 | |
69 var args = parser.parse(['--verbose']); | |
70 expect(args['verbose'], isTrue); | |
71 }); | |
72 | |
73 test('default if missing', () { | |
74 var parser = new ArgParser(); | |
75 parser.addFlag('a', defaultsTo: true); | |
76 parser.addFlag('b', defaultsTo: false); | |
77 | |
78 var args = parser.parse([]); | |
79 expect(args['a'], isTrue); | |
80 expect(args['b'], isFalse); | |
81 }); | |
82 | |
83 test('are false if missing with no default', () { | |
84 var parser = new ArgParser(); | |
85 parser.addFlag('verbose'); | |
86 | |
87 var args = parser.parse([]); | |
88 expect(args['verbose'], isFalse); | |
89 }); | |
90 | |
91 test('throws if given a value', () { | |
92 var parser = new ArgParser(); | |
93 parser.addFlag('verbose'); | |
94 | |
95 throwsFormat(parser, ['--verbose=true']); | |
96 }); | |
97 }); | 68 }); |
98 | 69 |
99 group('flags negated with "no-"', () { | 70 test('throws if the option is unknown', () { |
100 test('set the flag to false', () { | 71 var parser = new ArgParser(); |
101 var parser = new ArgParser(); | 72 parser.addOption('mode', defaultsTo: 'debug'); |
102 parser.addFlag('verbose'); | 73 expect(()=>parser.getDefault('undefined'), |
103 | 74 throwsArgumentError); |
104 var args = parser.parse(['--no-verbose']); | |
105 expect(args['verbose'], isFalse); | |
106 }); | |
107 | |
108 test('set the flag to true if the flag actually starts with "no-"', () { | |
109 var parser = new ArgParser(); | |
110 parser.addFlag('no-body'); | |
111 | |
112 var args = parser.parse(['--no-body']); | |
113 expect(args['no-body'], isTrue); | |
114 }); | |
115 | |
116 test('are not preferred over a colliding one without', () { | |
117 var parser = new ArgParser(); | |
118 parser.addFlag('no-strum'); | |
119 parser.addFlag('strum'); | |
120 | |
121 var args = parser.parse(['--no-strum']); | |
122 expect(args['no-strum'], isTrue); | |
123 expect(args['strum'], isFalse); | |
124 }); | |
125 | |
126 test('fail for non-negatable flags', () { | |
127 var parser = new ArgParser(); | |
128 parser.addFlag('strum', negatable: false); | |
129 | |
130 throwsFormat(parser, ['--no-strum']); | |
131 }); | |
132 }); | |
133 | |
134 group('callbacks', () { | |
135 test('for present flags are invoked with the value', () { | |
136 var a; | |
137 var parser = new ArgParser(); | |
138 parser.addFlag('a', callback: (value) => a = value); | |
139 | |
140 var args = parser.parse(['--a']); | |
141 expect(a, isTrue); | |
142 }); | |
143 | |
144 test('for absent flags are invoked with the default value', () { | |
145 var a; | |
146 var parser = new ArgParser(); | |
147 parser.addFlag('a', defaultsTo: false, | |
148 callback: (value) => a = value); | |
149 | |
150 var args = parser.parse([]); | |
151 expect(a, isFalse); | |
152 }); | |
153 | |
154 test('are invoked even if the flag is not present', () { | |
155 var a = 'not called'; | |
156 var parser = new ArgParser(); | |
157 parser.addFlag('a', callback: (value) => a = value); | |
158 | |
159 var args = parser.parse([]); | |
160 expect(a, isFalse); | |
161 }); | |
162 | |
163 test('for present options are invoked with the value', () { | |
164 var a; | |
165 var parser = new ArgParser(); | |
166 parser.addOption('a', callback: (value) => a = value); | |
167 | |
168 var args = parser.parse(['--a=v']); | |
169 expect(a, equals('v')); | |
170 }); | |
171 | |
172 test('for absent options are invoked with the default value', () { | |
173 var a; | |
174 var parser = new ArgParser(); | |
175 parser.addOption('a', defaultsTo: 'v', | |
176 callback: (value) => a = value); | |
177 | |
178 var args = parser.parse([]); | |
179 expect(a, equals('v')); | |
180 }); | |
181 | |
182 test('are invoked even if the option is not present', () { | |
183 var a = 'not called'; | |
184 var parser = new ArgParser(); | |
185 parser.addOption('a', callback: (value) => a = value); | |
186 | |
187 var args = parser.parse([]); | |
188 expect(a, isNull); | |
189 }); | |
190 | |
191 test('for multiple present, allowMultiple, options are invoked with ' | |
192 'value as a list', () { | |
193 var a; | |
194 var parser = new ArgParser(); | |
195 parser.addOption('a', allowMultiple: true, | |
196 callback: (value) => a = value); | |
197 | |
198 var args = parser.parse(['--a=v', '--a=x']); | |
199 expect(a, equals(['v', 'x'])); | |
200 }); | |
201 | |
202 test('for single present, allowMultiple, options are invoked with ' | |
203 ' value as a single element list', () { | |
204 var a; | |
205 var parser = new ArgParser(); | |
206 parser.addOption('a', allowMultiple: true, | |
207 callback: (value) => a = value); | |
208 | |
209 var args = parser.parse(['--a=v']); | |
210 expect(a, equals(['v'])); | |
211 }); | |
212 | |
213 test('for absent, allowMultiple, options are invoked with default ' | |
214 'value as a list.', () { | |
215 var a; | |
216 var parser = new ArgParser(); | |
217 parser.addOption('a', allowMultiple: true, defaultsTo: 'v', | |
218 callback: (value) => a = value); | |
219 | |
220 var args = parser.parse([]); | |
221 expect(a, equals(['v'])); | |
222 }); | |
223 | |
224 test('for absent, allowMultiple, options are invoked with value ' | |
225 'as an empty list.', () { | |
226 var a; | |
227 var parser = new ArgParser(); | |
228 parser.addOption('a', allowMultiple: true, | |
229 callback: (value) => a = value); | |
230 | |
231 var args = parser.parse([]); | |
232 expect(a, isEmpty); | |
233 }); | |
234 }); | |
235 | |
236 group('abbreviations', () { | |
237 test('are parsed with a preceding "-"', () { | |
238 var parser = new ArgParser(); | |
239 parser.addFlag('arg', abbr: 'a'); | |
240 | |
241 var args = parser.parse(['-a']); | |
242 expect(args['arg'], isTrue); | |
243 }); | |
244 | |
245 test('can use multiple after a single "-"', () { | |
246 var parser = new ArgParser(); | |
247 parser.addFlag('first', abbr: 'f'); | |
248 parser.addFlag('second', abbr: 's'); | |
249 parser.addFlag('third', abbr: 't'); | |
250 | |
251 var args = parser.parse(['-tf']); | |
252 expect(args['first'], isTrue); | |
253 expect(args['second'], isFalse); | |
254 expect(args['third'], isTrue); | |
255 }); | |
256 | |
257 test('can have multiple "-" args', () { | |
258 var parser = new ArgParser(); | |
259 parser.addFlag('first', abbr: 'f'); | |
260 parser.addFlag('second', abbr: 's'); | |
261 parser.addFlag('third', abbr: 't'); | |
262 | |
263 var args = parser.parse(['-s', '-tf']); | |
264 expect(args['first'], isTrue); | |
265 expect(args['second'], isTrue); | |
266 expect(args['third'], isTrue); | |
267 }); | |
268 | |
269 test('can take arguments without a space separating', () { | |
270 var parser = new ArgParser(); | |
271 parser.addOption('file', abbr: 'f'); | |
272 | |
273 var args = parser.parse(['-flip']); | |
274 expect(args['file'], equals('lip')); | |
275 }); | |
276 | |
277 test('can take arguments with a space separating', () { | |
278 var parser = new ArgParser(); | |
279 parser.addOption('file', abbr: 'f'); | |
280 | |
281 var args = parser.parse(['-f', 'name']); | |
282 expect(args['file'], equals('name')); | |
283 }); | |
284 | |
285 test('allow non-option characters in the value', () { | |
286 var parser = new ArgParser(); | |
287 parser.addOption('apple', abbr: 'a'); | |
288 | |
289 var args = parser.parse(['-ab?!c']); | |
290 expect(args['apple'], equals('b?!c')); | |
291 }); | |
292 | |
293 test('throw if unknown', () { | |
294 var parser = new ArgParser(); | |
295 throwsFormat(parser, ['-f']); | |
296 }); | |
297 | |
298 test('throw if the value is missing', () { | |
299 var parser = new ArgParser(); | |
300 parser.addOption('file', abbr: 'f'); | |
301 | |
302 throwsFormat(parser, ['-f']); | |
303 }); | |
304 | |
305 test('throw if the value looks like an option', () { | |
306 var parser = new ArgParser(); | |
307 parser.addOption('file', abbr: 'f'); | |
308 parser.addOption('other'); | |
309 | |
310 throwsFormat(parser, ['-f', '--other']); | |
311 throwsFormat(parser, ['-f', '--unknown']); | |
312 throwsFormat(parser, ['-f', '-abbr']); | |
313 }); | |
314 | |
315 test('throw if the value is not allowed', () { | |
316 var parser = new ArgParser(); | |
317 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); | |
318 | |
319 throwsFormat(parser, ['-mprofile']); | |
320 }); | |
321 | |
322 test('throw if any but the first is not a flag', () { | |
323 var parser = new ArgParser(); | |
324 parser.addFlag('apple', abbr: 'a'); | |
325 parser.addOption('banana', abbr: 'b'); // Takes an argument. | |
326 parser.addFlag('cherry', abbr: 'c'); | |
327 | |
328 throwsFormat(parser, ['-abc']); | |
329 }); | |
330 | |
331 test('throw if it has a value but the option is a flag', () { | |
332 var parser = new ArgParser(); | |
333 parser.addFlag('apple', abbr: 'a'); | |
334 parser.addFlag('banana', abbr: 'b'); | |
335 | |
336 // The '?!' means this can only be understood as '--apple b?!c'. | |
337 throwsFormat(parser, ['-ab?!c']); | |
338 }); | |
339 }); | |
340 | |
341 group('options', () { | |
342 test('are parsed if present', () { | |
343 var parser = new ArgParser(); | |
344 parser.addOption('mode'); | |
345 var args = parser.parse(['--mode=release']); | |
346 expect(args['mode'], equals('release')); | |
347 }); | |
348 | |
349 test('are null if not present', () { | |
350 var parser = new ArgParser(); | |
351 parser.addOption('mode'); | |
352 var args = parser.parse([]); | |
353 expect(args['mode'], isNull); | |
354 }); | |
355 | |
356 test('default if missing', () { | |
357 var parser = new ArgParser(); | |
358 parser.addOption('mode', defaultsTo: 'debug'); | |
359 var args = parser.parse([]); | |
360 expect(args['mode'], equals('debug')); | |
361 }); | |
362 | |
363 test('allow the value to be separated by whitespace', () { | |
364 var parser = new ArgParser(); | |
365 parser.addOption('mode'); | |
366 var args = parser.parse(['--mode', 'release']); | |
367 expect(args['mode'], equals('release')); | |
368 }); | |
369 | |
370 test('throw if unknown', () { | |
371 var parser = new ArgParser(); | |
372 throwsFormat(parser, ['--unknown']); | |
373 throwsFormat(parser, ['--nobody']); // Starts with "no". | |
374 }); | |
375 | |
376 test('throw if the arg does not include a value', () { | |
377 var parser = new ArgParser(); | |
378 parser.addOption('mode'); | |
379 throwsFormat(parser, ['--mode']); | |
380 }); | |
381 | |
382 test('throw if the value looks like an option', () { | |
383 var parser = new ArgParser(); | |
384 parser.addOption('mode'); | |
385 parser.addOption('other'); | |
386 | |
387 throwsFormat(parser, ['--mode', '--other']); | |
388 throwsFormat(parser, ['--mode', '--unknown']); | |
389 throwsFormat(parser, ['--mode', '-abbr']); | |
390 }); | |
391 | |
392 test('do not throw if the value is in the allowed set', () { | |
393 var parser = new ArgParser(); | |
394 parser.addOption('mode', allowed: ['debug', 'release']); | |
395 var args = parser.parse(['--mode=debug']); | |
396 expect(args['mode'], equals('debug')); | |
397 }); | |
398 | |
399 test('throw if the value is not in the allowed set', () { | |
400 var parser = new ArgParser(); | |
401 parser.addOption('mode', allowed: ['debug', 'release']); | |
402 throwsFormat(parser, ['--mode=profile']); | |
403 }); | |
404 | |
405 test('returns last provided value', () { | |
406 var parser = new ArgParser(); | |
407 parser.addOption('define'); | |
408 var args = parser.parse(['--define=1', '--define=2']); | |
409 expect(args['define'], equals('2')); | |
410 }); | |
411 | |
412 test('returns a List if multi-valued', () { | |
413 var parser = new ArgParser(); | |
414 parser.addOption('define', allowMultiple: true); | |
415 var args = parser.parse(['--define=1']); | |
416 expect(args['define'], equals(['1'])); | |
417 args = parser.parse(['--define=1', '--define=2']); | |
418 expect(args['define'], equals(['1','2'])); | |
419 }); | |
420 | |
421 test('returns the default value for multi-valued arguments ' | |
422 'if not explicitly set', () { | |
423 var parser = new ArgParser(); | |
424 parser.addOption('define', defaultsTo: '0', allowMultiple: true); | |
425 var args = parser.parse(['']); | |
426 expect(args['define'], equals(['0'])); | |
427 }); | |
428 }); | |
429 | |
430 group('query default values', () { | |
431 test('queries the default value', () { | |
432 var parser = new ArgParser(); | |
433 parser.addOption('define', defaultsTo: '0'); | |
434 expect(()=>parser.getDefault('undefine'), | |
435 throwsArgumentError); | |
436 }); | |
437 | |
438 test('queries the default value for an unknown option', () { | |
439 var parser = new ArgParser(); | |
440 parser.addOption('define', defaultsTo: '0'); | |
441 expect(()=>parser.getDefault('undefine'), | |
442 throwsArgumentError); | |
443 }); | |
444 }); | |
445 | |
446 group('gets the option names from an ArgsResult', () { | |
447 test('queries the set options', () { | |
448 var parser = new ArgParser(); | |
449 parser.addFlag('woof', defaultsTo: false); | |
450 parser.addOption('meow', defaultsTo: 'kitty'); | |
451 var args = parser.parse([]); | |
452 expect(args.options, hasLength(2)); | |
453 expect(args.options.any((o) => o == 'woof'), isTrue); | |
454 expect(args.options.any((o) => o == 'meow'), isTrue); | |
455 }); | |
456 }); | |
457 | |
458 group('remaining args', () { | |
459 test('stops parsing args when a non-option-like arg is encountered', () { | |
460 var parser = new ArgParser(); | |
461 parser.addFlag('woof'); | |
462 parser.addOption('meow'); | |
463 parser.addOption('tweet', defaultsTo: 'bird'); | |
464 | |
465 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); | |
466 expect(results['woof'], isTrue); | |
467 expect(results['meow'], equals('v')); | |
468 expect(results['tweet'], equals('bird')); | |
469 expect(results.rest, orderedEquals(['not', 'option'])); | |
470 }); | |
471 | |
472 test('stops parsing at "--"', () { | |
473 var parser = new ArgParser(); | |
474 parser.addFlag('woof', defaultsTo: false); | |
475 parser.addOption('meow', defaultsTo: 'kitty'); | |
476 | |
477 var results = parser.parse(['--woof', '--', '--meow']); | |
478 expect(results['woof'], isTrue); | |
479 expect(results['meow'], equals('kitty')); | |
480 expect(results.rest, orderedEquals(['--meow'])); | |
481 }); | |
482 | |
483 test('handles options with case-sensitivity', () { | |
484 var parser = new ArgParser(); | |
485 parser.addFlag('recurse', defaultsTo: false, abbr:'R'); | |
486 var results = parser.parse(['-R']); | |
487 expect(results['recurse'], isTrue); | |
488 expect(results.rest, [ ]); | |
489 throwsFormat(parser, ['-r']); | |
490 }); | |
491 }); | 75 }); |
492 }); | 76 }); |
493 | 77 |
494 group('ArgParser.getUsage()', () { | 78 group('ArgResults.options', () { |
495 test('negatable flags show "no-" in title', () { | 79 test('returns the provided options', () { |
496 var parser = new ArgParser(); | 80 var parser = new ArgParser(); |
497 parser.addFlag('mode', help: 'The mode'); | 81 parser.addFlag('woof'); |
498 | 82 parser.addOption('meow'); |
499 validateUsage(parser, | 83 var args = parser.parse(['--woof', '--meow', 'kitty']); |
500 ''' | 84 expect(args.options, hasLength(2)); |
501 --[no-]mode The mode | 85 expect(args.options.any((o) => o == 'woof'), isTrue); |
502 '''); | 86 expect(args.options.any((o) => o == 'meow'), isTrue); |
503 }); | 87 }); |
504 | 88 |
505 test('non-negatable flags don\'t show "no-" in title', () { | 89 test('includes defaulted options', () { |
506 var parser = new ArgParser(); | 90 var parser = new ArgParser(); |
507 parser.addFlag('mode', negatable: false, help: 'The mode'); | 91 parser.addFlag('woof', defaultsTo: false); |
508 | 92 parser.addOption('meow', defaultsTo: 'kitty'); |
509 validateUsage(parser, | 93 var args = parser.parse([]); |
510 ''' | 94 expect(args.options, hasLength(2)); |
511 --mode The mode | 95 expect(args.options.any((o) => o == 'woof'), isTrue); |
512 '''); | 96 expect(args.options.any((o) => o == 'meow'), isTrue); |
513 }); | |
514 | |
515 test('if there are no abbreviations, there is no column for them', () { | |
516 var parser = new ArgParser(); | |
517 parser.addFlag('mode', help: 'The mode'); | |
518 | |
519 validateUsage(parser, | |
520 ''' | |
521 --[no-]mode The mode | |
522 '''); | |
523 }); | |
524 | |
525 test('options are lined up past abbreviations', () { | |
526 var parser = new ArgParser(); | |
527 parser.addFlag('mode', abbr: 'm', help: 'The mode'); | |
528 parser.addOption('long', help: 'Lacks an abbreviation'); | |
529 | |
530 validateUsage(parser, | |
531 ''' | |
532 -m, --[no-]mode The mode | |
533 --long Lacks an abbreviation | |
534 '''); | |
535 }); | |
536 | |
537 test('help text is lined up past the longest option', () { | |
538 var parser = new ArgParser(); | |
539 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); | |
540 parser.addOption('a-really-long-name', help: 'Its help text'); | |
541 | |
542 validateUsage(parser, | |
543 ''' | |
544 -m, --[no-]mode Lined up with below | |
545 --a-really-long-name Its help text | |
546 '''); | |
547 }); | |
548 | |
549 test('leading empty lines are ignored in help text', () { | |
550 var parser = new ArgParser(); | |
551 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); | |
552 | |
553 validateUsage(parser, | |
554 ''' | |
555 --[no-]mode After newlines | |
556 '''); | |
557 }); | |
558 | |
559 test('trailing empty lines are ignored in help text', () { | |
560 var parser = new ArgParser(); | |
561 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); | |
562 | |
563 validateUsage(parser, | |
564 ''' | |
565 --[no-]mode Before newlines | |
566 '''); | |
567 }); | |
568 | |
569 test('options are documented in the order they were added', () { | |
570 var parser = new ArgParser(); | |
571 parser.addFlag('zebra', help: 'First'); | |
572 parser.addFlag('monkey', help: 'Second'); | |
573 parser.addFlag('wombat', help: 'Third'); | |
574 | |
575 validateUsage(parser, | |
576 ''' | |
577 --[no-]zebra First | |
578 --[no-]monkey Second | |
579 --[no-]wombat Third | |
580 '''); | |
581 }); | |
582 | |
583 test('the default value for a flag is shown if on', () { | |
584 var parser = new ArgParser(); | |
585 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); | |
586 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); | |
587 | |
588 validateUsage(parser, | |
589 ''' | |
590 --[no-]affirm Should be on | |
591 (defaults to on) | |
592 | |
593 --[no-]negate Should be off | |
594 '''); | |
595 }); | |
596 | |
597 test('the default value for an option with no allowed list is shown', () { | |
598 var parser = new ArgParser(); | |
599 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); | |
600 | |
601 validateUsage(parser, | |
602 ''' | |
603 --any Can be anything | |
604 (defaults to "whatevs") | |
605 '''); | |
606 }); | |
607 | |
608 test('the allowed list is shown', () { | |
609 var parser = new ArgParser(); | |
610 parser.addOption('suit', help: 'Like in cards', | |
611 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | |
612 | |
613 validateUsage(parser, | |
614 ''' | |
615 --suit Like in cards | |
616 [spades, clubs, hearts, diamonds] | |
617 '''); | |
618 }); | |
619 | |
620 test('the default is highlighted in the allowed list', () { | |
621 var parser = new ArgParser(); | |
622 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | |
623 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | |
624 | |
625 validateUsage(parser, | |
626 ''' | |
627 --suit Like in cards | |
628 [spades, clubs (default), hearts, diamonds] | |
629 '''); | |
630 }); | |
631 | |
632 test('the allowed help is shown', () { | |
633 var parser = new ArgParser(); | |
634 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | |
635 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], | |
636 allowedHelp: { | |
637 'spades': 'Swords of a soldier', | |
638 'clubs': 'Weapons of war', | |
639 'diamonds': 'Money for this art', | |
640 'hearts': 'The shape of my heart' | |
641 }); | |
642 | |
643 validateUsage(parser, | |
644 ''' | |
645 --suit Like in cards | |
646 | |
647 [clubs] Weapons of war | |
648 [diamonds] Money for this art | |
649 [hearts] The shape of my heart | |
650 [spades] Swords of a soldier | |
651 '''); | |
652 }); | 97 }); |
653 }); | 98 }); |
654 | 99 |
655 group('ArgResults[]', () { | 100 group('ArgResults[]', () { |
656 test('throws if the name is not an option', () { | 101 test('throws if the name is not an option', () { |
657 var parser = new ArgParser(); | 102 var parser = new ArgParser(); |
658 var results = parser.parse([]); | 103 var results = parser.parse([]); |
659 throwsIllegalArg(() => results['unknown']); | 104 throwsIllegalArg(() => results['unknown']); |
660 }); | 105 }); |
661 }); | 106 }); |
662 } | 107 } |
663 | 108 |
664 throwsIllegalArg(function) { | 109 throwsIllegalArg(function) { |
665 expect(function, throwsArgumentError); | 110 expect(function, throwsArgumentError); |
666 } | 111 } |
667 | 112 |
668 throwsFormat(ArgParser parser, List<String> args) { | 113 throwsFormat(ArgParser parser, List<String> args) { |
669 expect(() => parser.parse(args), throwsFormatException); | 114 expect(() => parser.parse(args), throwsFormatException); |
670 } | 115 } |
671 | |
672 validateUsage(ArgParser parser, String expected) { | |
673 expected = unindentString(expected); | |
674 expect(parser.getUsage(), equals(expected)); | |
675 } | |
676 | |
677 // TODO(rnystrom): Replace one in test_utils. | |
678 String unindentString(String text) { | |
679 var lines = text.split('\n'); | |
680 | |
681 // Count the indentation of the last line. | |
682 var whitespace = new RegExp('^ *'); | |
683 var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; | |
684 | |
685 // Drop the last line. It only exists for specifying indentation. | |
686 lines.removeLast(); | |
687 | |
688 // Strip indentation from the remaining lines. | |
689 for (var i = 0; i < lines.length; i++) { | |
690 var line = lines[i]; | |
691 if (line.length <= indent) { | |
692 // It's short, so it must be nothing but whitespace. | |
693 if (line.trim() != '') { | |
694 throw new ArgumentError( | |
695 'Line "$line" does not have enough indentation.'); | |
696 } | |
697 | |
698 lines[i] = ''; | |
699 } else { | |
700 if (line.substring(0, indent).trim() != '') { | |
701 throw new ArgumentError( | |
702 'Line "$line" does not have enough indentation.'); | |
703 } | |
704 | |
705 lines[i] = line.substring(indent); | |
706 } | |
707 } | |
708 | |
709 return Strings.join(lines, '\n'); | |
710 } | |
OLD | NEW |