Chromium Code Reviews| 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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 throwsFormat(parser, ['--mode=profile']); | 358 throwsFormat(parser, ['--mode=profile']); |
| 359 }); | 359 }); |
| 360 | |
| 361 test('return last set value', () { | |
|
Bob Nystrom
2012/08/09 23:08:40
"return" -> "returns"
"set" -> "provided"
gram
2012/08/09 23:47:00
Done.
| |
| 362 var parser = new ArgParser(); | |
| 363 parser.addOption('define'); | |
| 364 var args = parser.parse(['--define=1', '--define=2']); | |
| 365 expect(args['define'], equals('2')); | |
| 366 }); | |
| 367 | |
| 368 test('return List if multi-valued', () { | |
|
Bob Nystrom
2012/08/09 23:08:40
"return List" -> "returns a List"
gram
2012/08/09 23:47:00
Done.
| |
| 369 var parser = new ArgParser(); | |
| 370 parser.addOption('define', multiValued:true); | |
|
Bob Nystrom
2012/08/09 23:08:40
Space after ":" here and elsewhere.
gram
2012/08/09 23:47:00
Done.
| |
| 371 var args = parser.parse(['--define=1']); | |
| 372 expect(args['define'], hasLength(1)); | |
|
Bob Nystrom
2012/08/09 23:08:40
Can you just do this:
expect(args['define'], equa
gram
2012/08/09 23:47:00
Done.
| |
| 373 args = parser.parse(['--define=1', '--define=2']); | |
| 374 expect(args['define'], hasLength(2)); | |
| 375 expect(args['define'][0], equals('1')); | |
| 376 expect(args['define'][1], equals('2')); | |
|
Bob Nystrom
2012/08/09 23:08:40
Ditto here. equals() handles lists, right?
gram
2012/08/09 23:47:00
Done.
| |
| 377 }); | |
| 378 | |
| 379 test('default values for multi-valued arguments', () { | |
|
Bob Nystrom
2012/08/09 23:08:40
This should be worded like a predicate. Maybe "ret
| |
| 380 var parser = new ArgParser(); | |
| 381 parser.addOption('define', defaultsTo:'0', multiValued:true); | |
| 382 var args = parser.parse(['']); | |
| 383 expect(args['define'], hasLength(1)); | |
| 384 expect(args['define'][0], equals('0')); | |
| 385 args = parser.parse(['--define=1', '--define=2']); | |
| 386 expect(args['define'], hasLength(2)); | |
| 387 expect(args['define'][0], equals('1')); | |
| 388 expect(args['define'][1], equals('2')); | |
| 389 }); | |
| 360 }); | 390 }); |
| 361 | 391 |
| 362 group('remaining args', () { | 392 group('remaining args', () { |
| 363 test('stops parsing args when a non-option-like arg is encountered', () { | 393 test('stops parsing args when a non-option-like arg is encountered', () { |
| 364 var parser = new ArgParser(); | 394 var parser = new ArgParser(); |
| 365 parser.addFlag('woof'); | 395 parser.addFlag('woof'); |
| 366 parser.addOption('meow'); | 396 parser.addOption('meow'); |
| 367 parser.addOption('tweet', defaultsTo: 'bird'); | 397 parser.addOption('tweet', defaultsTo: 'bird'); |
| 368 | 398 |
| 369 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); | 399 var results = parser.parse(['--woof', '--meow', 'v', 'not', 'option']); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 group('ArgResults[]', () { | 580 group('ArgResults[]', () { |
| 551 test('throws if the name is not an option', () { | 581 test('throws if the name is not an option', () { |
| 552 var parser = new ArgParser(); | 582 var parser = new ArgParser(); |
| 553 var results = parser.parse([]); | 583 var results = parser.parse([]); |
| 554 throwsIllegalArg(() => results['unknown']); | 584 throwsIllegalArg(() => results['unknown']); |
| 555 }); | 585 }); |
| 556 }); | 586 }); |
| 557 } | 587 } |
| 558 | 588 |
| 559 throwsIllegalArg(function) { | 589 throwsIllegalArg(function) { |
| 560 expect(function, throwsA(new isInstanceOf<IllegalArgumentException>())); | 590 expect(function, throwsIllegalArgumentException); |
|
Bob Nystrom
2012/08/09 23:08:40
+1!
| |
| 561 } | 591 } |
| 562 | 592 |
| 563 throwsFormat(ArgParser parser, List<String> args) { | 593 throwsFormat(ArgParser parser, List<String> args) { |
| 564 expect(() => parser.parse(args), throwsFormatException); | 594 expect(() => parser.parse(args), throwsFormatException); |
| 565 } | 595 } |
| 566 | 596 |
| 567 validateUsage(ArgParser parser, String expected) { | 597 validateUsage(ArgParser parser, String expected) { |
| 568 expected = unindentString(expected); | 598 expected = unindentString(expected); |
| 569 expect(parser.getUsage(), equals(expected)); | 599 expect(parser.getUsage(), equals(expected)); |
| 570 } | 600 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 596 throw new IllegalArgumentException( | 626 throw new IllegalArgumentException( |
| 597 'Line "$line" does not have enough indentation.'); | 627 'Line "$line" does not have enough indentation.'); |
| 598 } | 628 } |
| 599 | 629 |
| 600 lines[i] = line.substring(indent); | 630 lines[i] = line.substring(indent); |
| 601 } | 631 } |
| 602 } | 632 } |
| 603 | 633 |
| 604 return Strings.join(lines, '\n'); | 634 return Strings.join(lines, '\n'); |
| 605 } | 635 } |
| OLD | NEW |