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 import 'utils.dart'; | 9 import 'utils.dart'; |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 throwsIllegalArg(() => parser.addFlag('foo')); | 22 throwsIllegalArg(() => parser.addFlag('foo')); |
23 }); | 23 }); |
24 | 24 |
25 test('throws ArgumentError if the abbreviation exists', () { | 25 test('throws ArgumentError if the abbreviation exists', () { |
26 var parser = new ArgParser(); | 26 var parser = new ArgParser(); |
27 parser.addFlag('foo', abbr: 'f'); | 27 parser.addFlag('foo', abbr: 'f'); |
28 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); | 28 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'f')); |
29 }); | 29 }); |
30 | 30 |
31 test('throws ArgumentError if the abbreviation is longer ' | 31 test('throws ArgumentError if the abbreviation is longer ' |
32 'than one character', () { | 32 'than one character', () { |
33 var parser = new ArgParser(); | 33 var parser = new ArgParser(); |
34 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); | 34 throwsIllegalArg(() => parser.addFlag('flummox', abbr: 'flu')); |
35 }); | 35 }); |
36 | 36 |
37 test('throws ArgumentError if a flag name is invalid', () { | 37 test('throws ArgumentError if a flag name is invalid', () { |
38 var parser = new ArgParser(); | 38 var parser = new ArgParser(); |
39 | 39 |
40 for(var name in _INVALID_OPTIONS) { | 40 for (var name in _INVALID_OPTIONS) { |
41 var reason = '${Error.safeToString(name)} is not valid'; | 41 var reason = '${Error.safeToString(name)} is not valid'; |
42 throwsIllegalArg(() => parser.addFlag(name), reason: reason); | 42 throwsIllegalArg(() => parser.addFlag(name), reason: reason); |
43 } | 43 } |
44 }); | 44 }); |
45 | 45 |
46 test('accepts valid flag names', () { | 46 test('accepts valid flag names', () { |
47 var parser = new ArgParser(); | 47 var parser = new ArgParser(); |
48 | 48 |
49 for(var name in _VALID_OPTIONS) { | 49 for (var name in _VALID_OPTIONS) { |
50 var reason = '${Error.safeToString(name)} is valid'; | 50 var reason = '${Error.safeToString(name)} is valid'; |
51 expect(() => parser.addFlag(name), returnsNormally, reason: reason); | 51 expect(() => parser.addFlag(name), returnsNormally, reason: reason); |
52 } | 52 } |
53 }); | 53 }); |
54 }); | 54 }); |
55 | 55 |
56 group('ArgParser.addOption()', () { | 56 group('ArgParser.addOption()', () { |
57 test('throws ArgumentError if the flag already exists', () { | 57 test('throws ArgumentError if the flag already exists', () { |
58 var parser = new ArgParser(); | 58 var parser = new ArgParser(); |
59 parser.addFlag('foo'); | 59 parser.addFlag('foo'); |
60 throwsIllegalArg(() => parser.addOption('foo')); | 60 throwsIllegalArg(() => parser.addOption('foo')); |
61 }); | 61 }); |
62 | 62 |
63 test('throws ArgumentError if the option already exists', () { | 63 test('throws ArgumentError if the option already exists', () { |
64 var parser = new ArgParser(); | 64 var parser = new ArgParser(); |
65 parser.addOption('foo'); | 65 parser.addOption('foo'); |
66 throwsIllegalArg(() => parser.addOption('foo')); | 66 throwsIllegalArg(() => parser.addOption('foo')); |
67 }); | 67 }); |
68 | 68 |
69 test('throws ArgumentError if the abbreviation exists', () { | 69 test('throws ArgumentError if the abbreviation exists', () { |
70 var parser = new ArgParser(); | 70 var parser = new ArgParser(); |
71 parser.addFlag('foo', abbr: 'f'); | 71 parser.addFlag('foo', abbr: 'f'); |
72 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); | 72 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'f')); |
73 }); | 73 }); |
74 | 74 |
75 test('throws ArgumentError if the abbreviation is longer ' | 75 test('throws ArgumentError if the abbreviation is longer ' |
76 'than one character', () { | 76 'than one character', () { |
77 var parser = new ArgParser(); | 77 var parser = new ArgParser(); |
78 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); | 78 throwsIllegalArg(() => parser.addOption('flummox', abbr: 'flu')); |
79 }); | 79 }); |
80 | 80 |
81 test('throws ArgumentError if the abbreviation is empty', () { | 81 test('throws ArgumentError if the abbreviation is empty', () { |
82 var parser = new ArgParser(); | 82 var parser = new ArgParser(); |
83 throwsIllegalArg(() => parser.addOption('flummox', abbr: '')); | 83 throwsIllegalArg(() => parser.addOption('flummox', abbr: '')); |
84 }); | 84 }); |
85 | 85 |
86 test('throws ArgumentError if the abbreviation is an invalid value', () { | 86 test('throws ArgumentError if the abbreviation is an invalid value', () { |
87 var parser = new ArgParser(); | 87 var parser = new ArgParser(); |
88 for(var name in _INVALID_OPTIONS.where((v) => v != null)) { | 88 for (var name in _INVALID_OPTIONS.where((v) => v != null)) { |
89 throwsIllegalArg(() => parser.addOption('flummox', abbr: name)); | 89 throwsIllegalArg(() => parser.addOption('flummox', abbr: name)); |
90 } | 90 } |
91 }); | 91 }); |
92 | 92 |
93 test('throws ArgumentError if the abbreviation is a dash', () { | 93 test('throws ArgumentError if the abbreviation is a dash', () { |
94 var parser = new ArgParser(); | 94 var parser = new ArgParser(); |
95 throwsIllegalArg(() => parser.addOption('flummox', abbr: '-')); | 95 throwsIllegalArg(() => parser.addOption('flummox', abbr: '-')); |
96 }); | 96 }); |
97 | 97 |
98 test('allows explict null value for "abbr"', () { | 98 test('allows explict null value for "abbr"', () { |
99 var parser = new ArgParser(); | 99 var parser = new ArgParser(); |
100 expect(() => parser.addOption('flummox', abbr: null), returnsNormally); | 100 expect(() => parser.addOption('flummox', abbr: null), returnsNormally); |
101 }); | 101 }); |
102 | 102 |
103 test('throws ArgumentError if an option name is invalid', () { | 103 test('throws ArgumentError if an option name is invalid', () { |
104 var parser = new ArgParser(); | 104 var parser = new ArgParser(); |
105 | 105 |
106 for(var name in _INVALID_OPTIONS) { | 106 for (var name in _INVALID_OPTIONS) { |
107 var reason = '${Error.safeToString(name)} is not valid'; | 107 var reason = '${Error.safeToString(name)} is not valid'; |
108 throwsIllegalArg(() => parser.addOption(name), reason: reason); | 108 throwsIllegalArg(() => parser.addOption(name), reason: reason); |
109 } | 109 } |
110 }); | 110 }); |
111 | 111 |
112 test('accepts valid option names', () { | 112 test('accepts valid option names', () { |
113 var parser = new ArgParser(); | 113 var parser = new ArgParser(); |
114 | 114 |
115 for(var name in _VALID_OPTIONS) { | 115 for (var name in _VALID_OPTIONS) { |
116 var reason = '${Error.safeToString(name)} is valid'; | 116 var reason = '${Error.safeToString(name)} is valid'; |
117 expect(() => parser.addOption(name), returnsNormally, reason: reason); | 117 expect(() => parser.addOption(name), returnsNormally, reason: reason); |
118 } | 118 } |
119 }); | 119 }); |
120 }); | 120 }); |
121 | 121 |
122 group('ArgParser.getDefault()', () { | 122 group('ArgParser.getDefault()', () { |
123 test('returns the default value for an option', () { | 123 test('returns the default value for an option', () { |
124 var parser = new ArgParser(); | 124 var parser = new ArgParser(); |
125 parser.addOption('mode', defaultsTo: 'debug'); | 125 parser.addOption('mode', defaultsTo: 'debug'); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 expect(parser.options['single-def'].getOrDefault('v'), equals('v')); | 279 expect(parser.options['single-def'].getOrDefault('v'), equals('v')); |
280 expect(parser.options['multi-no'].getOrDefault(null), equals([])); | 280 expect(parser.options['multi-no'].getOrDefault(null), equals([])); |
281 expect(parser.options['multi-no'].getOrDefault(['v']), equals(['v'])); | 281 expect(parser.options['multi-no'].getOrDefault(['v']), equals(['v'])); |
282 expect(parser.options['multi-def'].getOrDefault(null), equals(['def'])); | 282 expect(parser.options['multi-def'].getOrDefault(null), equals(['def'])); |
283 expect(parser.options['multi-def'].getOrDefault(['v']), equals(['v'])); | 283 expect(parser.options['multi-def'].getOrDefault(['v']), equals(['v'])); |
284 }); | 284 }); |
285 }); | 285 }); |
286 } | 286 } |
287 | 287 |
288 const _INVALID_OPTIONS = const [ | 288 const _INVALID_OPTIONS = const [ |
289 ' ', '', '-', '--', '--foo', | 289 ' ', |
290 ' with space', | 290 '', |
291 'with\ttab', | 291 '-', |
292 'with\rcarriage\rreturn', | 292 '--', |
293 'with\nline\nfeed', | 293 '--foo', |
294 "'singlequotes'", | 294 ' with space', |
295 '"doublequotes"', | 295 'with\ttab', |
296 'back\\slash', | 296 'with\rcarriage\rreturn', |
297 'forward/slash' | 297 'with\nline\nfeed', |
| 298 "'singlequotes'", |
| 299 '"doublequotes"', |
| 300 'back\\slash', |
| 301 'forward/slash' |
298 ]; | 302 ]; |
299 | 303 |
300 const _VALID_OPTIONS = const [ | 304 const _VALID_OPTIONS = const [ |
301 'a' // One character. | 305 'a' // One character. |
302 'contains-dash', | 306 'contains-dash', |
303 'contains_underscore', | 307 'contains_underscore', |
304 'ends-with-dash-', | 308 'ends-with-dash-', |
305 'contains--doubledash--', | 309 'contains--doubledash--', |
306 '1starts-with-number', | 310 '1starts-with-number', |
307 'contains-a-1number', | 311 'contains-a-1number', |
308 'ends-with-a-number8' | 312 'ends-with-a-number8' |
309 ]; | 313 ]; |
OLD | NEW |