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('../../../lib/unittest/unittest.dart'); | 7 #import('../../../lib/unittest/unittest.dart'); |
8 #import('../../../lib/args/args.dart'); | 8 #import('../../../lib/args/args.dart'); |
9 | 9 |
10 main() { | 10 main() { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 parser.addFlag('verbose'); | 85 parser.addFlag('verbose'); |
86 | 86 |
87 var args = parser.parse([]); | 87 var args = parser.parse([]); |
88 expect(args['verbose'], isFalse); | 88 expect(args['verbose'], isFalse); |
89 }); | 89 }); |
90 | 90 |
91 test('throws if given a value', () { | 91 test('throws if given a value', () { |
92 var parser = new ArgParser(); | 92 var parser = new ArgParser(); |
93 parser.addFlag('verbose'); | 93 parser.addFlag('verbose'); |
94 | 94 |
95 throwsBadFormat(parser, ['--verbose=true']); | 95 throwsFormat(parser, ['--verbose=true']); |
96 }); | 96 }); |
97 }); | 97 }); |
98 | 98 |
99 group('flags negated with "no-"', () { | 99 group('flags negated with "no-"', () { |
100 test('set the flag to false', () { | 100 test('set the flag to false', () { |
101 var parser = new ArgParser(); | 101 var parser = new ArgParser(); |
102 parser.addFlag('verbose'); | 102 parser.addFlag('verbose'); |
103 | 103 |
104 var args = parser.parse(['--no-verbose']); | 104 var args = parser.parse(['--no-verbose']); |
105 expect(args['verbose'], isFalse); | 105 expect(args['verbose'], isFalse); |
(...skipping 14 matching lines...) Expand all Loading... |
120 | 120 |
121 var args = parser.parse(['--no-strum']); | 121 var args = parser.parse(['--no-strum']); |
122 expect(args['no-strum'], isTrue); | 122 expect(args['no-strum'], isTrue); |
123 expect(args['strum'], isFalse); | 123 expect(args['strum'], isFalse); |
124 }); | 124 }); |
125 | 125 |
126 test('fail for non-negatable flags', () { | 126 test('fail for non-negatable flags', () { |
127 var parser = new ArgParser(); | 127 var parser = new ArgParser(); |
128 parser.addFlag('strum', negatable: false); | 128 parser.addFlag('strum', negatable: false); |
129 | 129 |
130 throwsBadFormat(parser, ['--no-strum']); | 130 throwsFormat(parser, ['--no-strum']); |
131 }); | 131 }); |
132 }); | 132 }); |
133 | 133 |
134 group('callbacks', () { | 134 group('callbacks', () { |
135 test('for present flags are invoked with the value', () { | 135 test('for present flags are invoked with the value', () { |
136 var a; | 136 var a; |
137 var parser = new ArgParser(); | 137 var parser = new ArgParser(); |
138 parser.addFlag('a', callback: (value) => a = value); | 138 parser.addFlag('a', callback: (value) => a = value); |
139 | 139 |
140 var args = parser.parse(['--a']); | 140 var args = parser.parse(['--a']); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 test('allow non-option characters in the value', () { | 241 test('allow non-option characters in the value', () { |
242 var parser = new ArgParser(); | 242 var parser = new ArgParser(); |
243 parser.addOption('apple', abbr: 'a'); | 243 parser.addOption('apple', abbr: 'a'); |
244 | 244 |
245 var args = parser.parse(['-ab?!c']); | 245 var args = parser.parse(['-ab?!c']); |
246 expect(args['apple'], equals('b?!c')); | 246 expect(args['apple'], equals('b?!c')); |
247 }); | 247 }); |
248 | 248 |
249 test('throw if unknown', () { | 249 test('throw if unknown', () { |
250 var parser = new ArgParser(); | 250 var parser = new ArgParser(); |
251 throwsBadFormat(parser, ['-f']); | 251 throwsFormat(parser, ['-f']); |
252 }); | 252 }); |
253 | 253 |
254 test('throw if the value is missing', () { | 254 test('throw if the value is missing', () { |
255 var parser = new ArgParser(); | 255 var parser = new ArgParser(); |
256 parser.addOption('file', abbr: 'f'); | 256 parser.addOption('file', abbr: 'f'); |
257 | 257 |
258 throwsBadFormat(parser, ['-f']); | 258 throwsFormat(parser, ['-f']); |
259 }); | 259 }); |
260 | 260 |
261 test('throw if the value looks like an option', () { | 261 test('throw if the value looks like an option', () { |
262 var parser = new ArgParser(); | 262 var parser = new ArgParser(); |
263 parser.addOption('file', abbr: 'f'); | 263 parser.addOption('file', abbr: 'f'); |
264 parser.addOption('other'); | 264 parser.addOption('other'); |
265 | 265 |
266 throwsBadFormat(parser, ['-f', '--other']); | 266 throwsFormat(parser, ['-f', '--other']); |
267 throwsBadFormat(parser, ['-f', '--unknown']); | 267 throwsFormat(parser, ['-f', '--unknown']); |
268 throwsBadFormat(parser, ['-f', '-abbr']); | 268 throwsFormat(parser, ['-f', '-abbr']); |
269 }); | 269 }); |
270 | 270 |
271 test('throw if the value is not allowed', () { | 271 test('throw if the value is not allowed', () { |
272 var parser = new ArgParser(); | 272 var parser = new ArgParser(); |
273 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); | 273 parser.addOption('mode', abbr: 'm', allowed: ['debug', 'release']); |
274 | 274 |
275 throwsBadFormat(parser, ['-mprofile']); | 275 throwsFormat(parser, ['-mprofile']); |
276 }); | 276 }); |
277 | 277 |
278 test('throw if any but the first is not a flag', () { | 278 test('throw if any but the first is not a flag', () { |
279 var parser = new ArgParser(); | 279 var parser = new ArgParser(); |
280 parser.addFlag('apple', abbr: 'a'); | 280 parser.addFlag('apple', abbr: 'a'); |
281 parser.addOption('banana', abbr: 'b'); // Takes an argument. | 281 parser.addOption('banana', abbr: 'b'); // Takes an argument. |
282 parser.addFlag('cherry', abbr: 'c'); | 282 parser.addFlag('cherry', abbr: 'c'); |
283 | 283 |
284 throwsBadFormat(parser, ['-abc']); | 284 throwsFormat(parser, ['-abc']); |
285 }); | 285 }); |
286 | 286 |
287 test('throw if it has a value but the option is a flag', () { | 287 test('throw if it has a value but the option is a flag', () { |
288 var parser = new ArgParser(); | 288 var parser = new ArgParser(); |
289 parser.addFlag('apple', abbr: 'a'); | 289 parser.addFlag('apple', abbr: 'a'); |
290 parser.addFlag('banana', abbr: 'b'); | 290 parser.addFlag('banana', abbr: 'b'); |
291 | 291 |
292 // The '?!' means this can only be understood as '--apple b?!c'. | 292 // The '?!' means this can only be understood as '--apple b?!c'. |
293 throwsBadFormat(parser, ['-ab?!c']); | 293 throwsFormat(parser, ['-ab?!c']); |
294 }); | 294 }); |
295 }); | 295 }); |
296 | 296 |
297 group('options', () { | 297 group('options', () { |
298 test('are parsed if present', () { | 298 test('are parsed if present', () { |
299 var parser = new ArgParser(); | 299 var parser = new ArgParser(); |
300 parser.addOption('mode'); | 300 parser.addOption('mode'); |
301 var args = parser.parse(['--mode=release']); | 301 var args = parser.parse(['--mode=release']); |
302 expect(args['mode'], equals('release')); | 302 expect(args['mode'], equals('release')); |
303 }); | 303 }); |
(...skipping 14 matching lines...) Expand all Loading... |
318 | 318 |
319 test('allow the value to be separated by whitespace', () { | 319 test('allow the value to be separated by whitespace', () { |
320 var parser = new ArgParser(); | 320 var parser = new ArgParser(); |
321 parser.addOption('mode'); | 321 parser.addOption('mode'); |
322 var args = parser.parse(['--mode', 'release']); | 322 var args = parser.parse(['--mode', 'release']); |
323 expect(args['mode'], equals('release')); | 323 expect(args['mode'], equals('release')); |
324 }); | 324 }); |
325 | 325 |
326 test('throw if unknown', () { | 326 test('throw if unknown', () { |
327 var parser = new ArgParser(); | 327 var parser = new ArgParser(); |
328 throwsBadFormat(parser, ['--unknown']); | 328 throwsFormat(parser, ['--unknown']); |
329 throwsBadFormat(parser, ['--nobody']); // Starts with "no". | 329 throwsFormat(parser, ['--nobody']); // Starts with "no". |
330 }); | 330 }); |
331 | 331 |
332 test('throw if the arg does not include a value', () { | 332 test('throw if the arg does not include a value', () { |
333 var parser = new ArgParser(); | 333 var parser = new ArgParser(); |
334 parser.addOption('mode'); | 334 parser.addOption('mode'); |
335 throwsBadFormat(parser, ['--mode']); | 335 throwsFormat(parser, ['--mode']); |
336 }); | 336 }); |
337 | 337 |
338 test('throw if the value looks like an option', () { | 338 test('throw if the value looks like an option', () { |
339 var parser = new ArgParser(); | 339 var parser = new ArgParser(); |
340 parser.addOption('mode'); | 340 parser.addOption('mode'); |
341 parser.addOption('other'); | 341 parser.addOption('other'); |
342 | 342 |
343 throwsBadFormat(parser, ['--mode', '--other']); | 343 throwsFormat(parser, ['--mode', '--other']); |
344 throwsBadFormat(parser, ['--mode', '--unknown']); | 344 throwsFormat(parser, ['--mode', '--unknown']); |
345 throwsBadFormat(parser, ['--mode', '-abbr']); | 345 throwsFormat(parser, ['--mode', '-abbr']); |
346 }); | 346 }); |
347 | 347 |
348 test('do not throw if the value is in the allowed set', () { | 348 test('do not throw if the value is in the allowed set', () { |
349 var parser = new ArgParser(); | 349 var parser = new ArgParser(); |
350 parser.addOption('mode', allowed: ['debug', 'release']); | 350 parser.addOption('mode', allowed: ['debug', 'release']); |
351 var args = parser.parse(['--mode=debug']); | 351 var args = parser.parse(['--mode=debug']); |
352 expect(args['mode'], equals('debug')); | 352 expect(args['mode'], equals('debug')); |
353 }); | 353 }); |
354 | 354 |
355 test('throw if the value is not in the allowed set', () { | 355 test('throw if the value is not in the allowed set', () { |
356 var parser = new ArgParser(); | 356 var parser = new ArgParser(); |
357 parser.addOption('mode', allowed: ['debug', 'release']); | 357 parser.addOption('mode', allowed: ['debug', 'release']); |
358 throwsBadFormat(parser, ['--mode=profile']); | 358 throwsFormat(parser, ['--mode=profile']); |
359 }); | 359 }); |
360 }); | 360 }); |
361 | 361 |
362 group('remaining args', () { | 362 group('remaining args', () { |
363 test('stops parsing args when a non-option-like arg is encountered', () { | 363 test('stops parsing args when a non-option-like arg is encountered', () { |
364 var parser = new ArgParser(); | 364 var parser = new ArgParser(); |
365 parser.addFlag('woof'); | 365 parser.addFlag('woof'); |
366 parser.addOption('meow'); | 366 parser.addOption('meow'); |
367 parser.addOption('tweet', defaultsTo: 'bird'); | 367 parser.addOption('tweet', defaultsTo: 'bird'); |
368 | 368 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 var results = parser.parse([]); | 553 var results = parser.parse([]); |
554 throwsIllegalArg(() => results['unknown']); | 554 throwsIllegalArg(() => results['unknown']); |
555 }); | 555 }); |
556 }); | 556 }); |
557 } | 557 } |
558 | 558 |
559 throwsIllegalArg(function) { | 559 throwsIllegalArg(function) { |
560 expect(function, throwsA(new isInstanceOf<IllegalArgumentException>())); | 560 expect(function, throwsA(new isInstanceOf<IllegalArgumentException>())); |
561 } | 561 } |
562 | 562 |
563 throwsBadFormat(ArgParser parser, List<String> args) { | 563 throwsFormat(ArgParser parser, List<String> args) { |
564 expect(() => parser.parse(args), | 564 expect(() => parser.parse(args), throwsFormatException); |
565 throwsA(new isInstanceOf<ArgFormatException>())); | |
566 } | 565 } |
567 | 566 |
568 validateUsage(ArgParser parser, String expected) { | 567 validateUsage(ArgParser parser, String expected) { |
569 expected = unindentString(expected); | 568 expected = unindentString(expected); |
570 expect(parser.getUsage(), equals(expected)); | 569 expect(parser.getUsage(), equals(expected)); |
571 } | 570 } |
572 | 571 |
573 // TODO(rnystrom): Replace one in test_utils. | 572 // TODO(rnystrom): Replace one in test_utils. |
574 String unindentString(String text) { | 573 String unindentString(String text) { |
575 var lines = text.split('\n'); | 574 var lines = text.split('\n'); |
(...skipping 21 matching lines...) Expand all Loading... |
597 throw new IllegalArgumentException( | 596 throw new IllegalArgumentException( |
598 'Line "$line" does not have enough indentation.'); | 597 'Line "$line" does not have enough indentation.'); |
599 } | 598 } |
600 | 599 |
601 lines[i] = line.substring(indent); | 600 lines[i] = line.substring(indent); |
602 } | 601 } |
603 } | 602 } |
604 | 603 |
605 return Strings.join(lines, '\n'); | 604 return Strings.join(lines, '\n'); |
606 } | 605 } |
OLD | NEW |