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('../../unittest/unittest.dart'); | 7 #import('../../unittest/unittest.dart'); |
8 | 8 |
9 // TODO(rnystrom): Use "package:" URL here when test.dart can handle pub. | 9 // TODO(rnystrom): Use "package:" URL here when test.dart can handle pub. |
10 #import('../lib/args.dart'); | 10 #import('../lib/args.dart'); |
11 | 11 |
12 main() { | 12 main() { |
13 group('ArgParser.addFlag()', () { | 13 group('ArgParser.addFlag()', () { |
14 test('throws IllegalArgumentException if the flag already exists', () { | 14 test('throws ArgumentError if the flag already exists', () { |
15 var parser = new ArgParser(); | 15 var parser = new ArgParser(); |
16 parser.addFlag('foo'); | 16 parser.addFlag('foo'); |
17 throwsIllegalArg(() => parser.addFlag('foo')); | 17 throwsIllegalArg(() => parser.addFlag('foo')); |
18 }); | 18 }); |
19 | 19 |
20 test('throws IllegalArgumentException if the option already exists', () { | 20 test('throws ArgumentError if the option already exists', () { |
21 var parser = new ArgParser(); | 21 var parser = new ArgParser(); |
22 parser.addOption('foo'); | 22 parser.addOption('foo'); |
23 throwsIllegalArg(() => parser.addFlag('foo')); | 23 throwsIllegalArg(() => parser.addFlag('foo')); |
24 }); | 24 }); |
25 | 25 |
26 test('throws IllegalArgumentException if the abbreviation exists', () { | 26 test('throws ArgumentError if the abbreviation exists', () { |
27 var parser = new ArgParser(); | 27 var parser = new ArgParser(); |
28 parser.addFlag('foo', abbr: 'f'); | 28 parser.addFlag('foo', abbr: 'f'); |
29 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); | 29 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); |
30 }); | 30 }); |
31 | 31 |
32 test('throws IllegalArgumentException if the abbreviation is longer ' | 32 test('throws ArgumentError if the abbreviation is longer ' |
33 'than one character', () { | 33 'than one character', () { |
34 var parser = new ArgParser(); | 34 var parser = new ArgParser(); |
35 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); | 35 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); |
36 }); | 36 }); |
37 }); | 37 }); |
38 | 38 |
39 group('ArgParser.addOption()', () { | 39 group('ArgParser.addOption()', () { |
40 test('throws IllegalArgumentException if the flag already exists', () { | 40 test('throws ArgumentError if the flag already exists', () { |
41 var parser = new ArgParser(); | 41 var parser = new ArgParser(); |
42 parser.addFlag('foo'); | 42 parser.addFlag('foo'); |
43 throwsIllegalArg(() => parser.addOption('foo')); | 43 throwsIllegalArg(() => parser.addOption('foo')); |
44 }); | 44 }); |
45 | 45 |
46 test('throws IllegalArgumentException if the option already exists', () { | 46 test('throws ArgumentError if the option already exists', () { |
47 var parser = new ArgParser(); | 47 var parser = new ArgParser(); |
48 parser.addOption('foo'); | 48 parser.addOption('foo'); |
49 throwsIllegalArg(() => parser.addOption('foo')); | 49 throwsIllegalArg(() => parser.addOption('foo')); |
50 }); | 50 }); |
51 | 51 |
52 test('throws IllegalArgumentException if the abbreviation exists', () { | 52 test('throws ArgumentError if the abbreviation exists', () { |
53 var parser = new ArgParser(); | 53 var parser = new ArgParser(); |
54 parser.addFlag('foo', abbr: 'f'); | 54 parser.addFlag('foo', abbr: 'f'); |
55 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); | 55 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); |
56 }); | 56 }); |
57 | 57 |
58 test('throws IllegalArgumentException if the abbreviation is longer ' | 58 test('throws ArgumentError if the abbreviation is longer ' |
59 'than one character', () { | 59 'than one character', () { |
60 var parser = new ArgParser(); | 60 var parser = new ArgParser(); |
61 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); | 61 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); |
62 }); | 62 }); |
63 }); | 63 }); |
64 | 64 |
65 group('ArgParser.parse()', () { | 65 group('ArgParser.parse()', () { |
66 group('flags', () { | 66 group('flags', () { |
67 test('are true if present', () { | 67 test('are true if present', () { |
68 var parser = new ArgParser(); | 68 var parser = new ArgParser(); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 var args = parser.parse(['']); | 427 var args = parser.parse(['']); |
428 expect(args['define'], equals(['0'])); | 428 expect(args['define'], equals(['0'])); |
429 }); | 429 }); |
430 }); | 430 }); |
431 | 431 |
432 group('query default values', () { | 432 group('query default values', () { |
433 test('queries the default value', () { | 433 test('queries the default value', () { |
434 var parser = new ArgParser(); | 434 var parser = new ArgParser(); |
435 parser.addOption('define', defaultsTo: '0'); | 435 parser.addOption('define', defaultsTo: '0'); |
436 expect(()=>parser.getDefault('undefine'), | 436 expect(()=>parser.getDefault('undefine'), |
437 throwsIllegalArgumentException); | 437 throwsArgumentError); |
438 }); | 438 }); |
439 | 439 |
440 test('queries the default value for an unknown option', () { | 440 test('queries the default value for an unknown option', () { |
441 var parser = new ArgParser(); | 441 var parser = new ArgParser(); |
442 parser.addOption('define', defaultsTo: '0'); | 442 parser.addOption('define', defaultsTo: '0'); |
443 expect(()=>parser.getDefault('undefine'), | 443 expect(()=>parser.getDefault('undefine'), |
444 throwsIllegalArgumentException); | 444 throwsArgumentError); |
445 }); | 445 }); |
446 }); | 446 }); |
447 | 447 |
448 group('gets the option names from an ArgsResult', () { | 448 group('gets the option names from an ArgsResult', () { |
449 test('queries the set options', () { | 449 test('queries the set options', () { |
450 var parser = new ArgParser(); | 450 var parser = new ArgParser(); |
451 parser.addFlag('woof', defaultsTo: false); | 451 parser.addFlag('woof', defaultsTo: false); |
452 parser.addOption('meow', defaultsTo: 'kitty'); | 452 parser.addOption('meow', defaultsTo: 'kitty'); |
453 var args = parser.parse([]); | 453 var args = parser.parse([]); |
454 expect(args.options, hasLength(2)); | 454 expect(args.options, hasLength(2)); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 group('ArgResults[]', () { | 657 group('ArgResults[]', () { |
658 test('throws if the name is not an option', () { | 658 test('throws if the name is not an option', () { |
659 var parser = new ArgParser(); | 659 var parser = new ArgParser(); |
660 var results = parser.parse([]); | 660 var results = parser.parse([]); |
661 throwsIllegalArg(() => results['unknown']); | 661 throwsIllegalArg(() => results['unknown']); |
662 }); | 662 }); |
663 }); | 663 }); |
664 } | 664 } |
665 | 665 |
666 throwsIllegalArg(function) { | 666 throwsIllegalArg(function) { |
667 expect(function, throwsIllegalArgumentException); | 667 expect(function, throwsArgumentError); |
668 } | 668 } |
669 | 669 |
670 throwsFormat(ArgParser parser, List<String> args) { | 670 throwsFormat(ArgParser parser, List<String> args) { |
671 expect(() => parser.parse(args), throwsFormatException); | 671 expect(() => parser.parse(args), throwsFormatException); |
672 } | 672 } |
673 | 673 |
674 validateUsage(ArgParser parser, String expected) { | 674 validateUsage(ArgParser parser, String expected) { |
675 expected = unindentString(expected); | 675 expected = unindentString(expected); |
676 expect(parser.getUsage(), equals(expected)); | 676 expect(parser.getUsage(), equals(expected)); |
677 } | 677 } |
678 | 678 |
679 // TODO(rnystrom): Replace one in test_utils. | 679 // TODO(rnystrom): Replace one in test_utils. |
680 String unindentString(String text) { | 680 String unindentString(String text) { |
681 var lines = text.split('\n'); | 681 var lines = text.split('\n'); |
682 | 682 |
683 // Count the indentation of the last line. | 683 // Count the indentation of the last line. |
684 var whitespace = const RegExp('^ *'); | 684 var whitespace = const RegExp('^ *'); |
685 var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; | 685 var indent = whitespace.firstMatch(lines[lines.length - 1])[0].length; |
686 | 686 |
687 // Drop the last line. It only exists for specifying indentation. | 687 // Drop the last line. It only exists for specifying indentation. |
688 lines.removeLast(); | 688 lines.removeLast(); |
689 | 689 |
690 // Strip indentation from the remaining lines. | 690 // Strip indentation from the remaining lines. |
691 for (var i = 0; i < lines.length; i++) { | 691 for (var i = 0; i < lines.length; i++) { |
692 var line = lines[i]; | 692 var line = lines[i]; |
693 if (line.length <= indent) { | 693 if (line.length <= indent) { |
694 // It's short, so it must be nothing but whitespace. | 694 // It's short, so it must be nothing but whitespace. |
695 if (line.trim() != '') { | 695 if (line.trim() != '') { |
696 throw new IllegalArgumentException( | 696 throw new ArgumentError( |
697 'Line "$line" does not have enough indentation.'); | 697 'Line "$line" does not have enough indentation.'); |
698 } | 698 } |
699 | 699 |
700 lines[i] = ''; | 700 lines[i] = ''; |
701 } else { | 701 } else { |
702 if (line.substring(0, indent).trim() != '') { | 702 if (line.substring(0, indent).trim() != '') { |
703 throw new IllegalArgumentException( | 703 throw new ArgumentError( |
704 'Line "$line" does not have enough indentation.'); | 704 'Line "$line" does not have enough indentation.'); |
705 } | 705 } |
706 | 706 |
707 lines[i] = line.substring(indent); | 707 lines[i] = line.substring(indent); |
708 } | 708 } |
709 } | 709 } |
710 | 710 |
711 return Strings.join(lines, '\n'); | 711 return Strings.join(lines, '\n'); |
712 } | 712 } |
OLD | NEW |