| 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() { |
| 11 group('ArgParser()', () { |
| 12 test('adds a help option if includeHelp is true', () { |
| 13 var parser = new ArgParser(includeHelp: true); |
| 14 var results = parser.parse(['-h']); |
| 15 expect(results['help']).isTrue(); |
| 16 }); |
| 17 |
| 18 test('does not add a help option if includeHelp is false', () { |
| 19 var parser = new ArgParser(includeHelp: false); |
| 20 throwsBadFormat(parser, ['-h']); |
| 21 }); |
| 22 |
| 23 test('includeHelp defaults to true', () { |
| 24 var parser = new ArgParser(); |
| 25 var results = parser.parse(['-h']); |
| 26 expect(results['help']).isTrue(); |
| 27 }); |
| 28 }); |
| 29 |
| 11 group('ArgParser.addFlag()', () { | 30 group('ArgParser.addFlag()', () { |
| 12 test('throws IllegalArgumentException if the flag already exists', () { | 31 test('throws IllegalArgumentException if the flag already exists', () { |
| 13 var parser = new ArgParser(); | 32 var parser = new ArgParser(); |
| 14 parser.addFlag('foo'); | 33 parser.addFlag('foo'); |
| 15 throwsIllegalArg(() => parser.addFlag('foo')); | 34 throwsIllegalArg(() => parser.addFlag('foo')); |
| 16 }); | 35 }); |
| 17 | 36 |
| 18 test('throws IllegalArgumentException if the option already exists', () { | 37 test('throws IllegalArgumentException if the option already exists', () { |
| 19 var parser = new ArgParser(); | 38 var parser = new ArgParser(); |
| 20 parser.addOption('foo'); | 39 parser.addOption('foo'); |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 392 |
| 374 var results = parser.parse(['--woof', '--', '--meow']); | 393 var results = parser.parse(['--woof', '--', '--meow']); |
| 375 expect(results['woof']).isTrue(); | 394 expect(results['woof']).isTrue(); |
| 376 expect(results['meow']).equals('kitty'); | 395 expect(results['meow']).equals('kitty'); |
| 377 expect(results.rest).equalsCollection(['--meow']); | 396 expect(results.rest).equalsCollection(['--meow']); |
| 378 }); | 397 }); |
| 379 }); | 398 }); |
| 380 }); | 399 }); |
| 381 | 400 |
| 382 group('ArgParser.getUsage()', () { | 401 group('ArgParser.getUsage()', () { |
| 383 test('flags show "no-" in title', () { | 402 test('shows usage if provided', () { |
| 384 var parser = new ArgParser(); | 403 var parser = new ArgParser(usage: 'myapp [options]'); |
| 385 parser.addFlag('mode', help: 'The mode'); | 404 parser.addFlag('mode', help: 'The mode'); |
| 386 | 405 |
| 387 validateUsage(parser, | 406 validateUsage(parser, |
| 407 ''' |
| 408 myapp [options] |
| 409 |
| 410 -h, --[no-]help Display usage information |
| 411 --[no-]mode The mode |
| 412 '''); |
| 413 }); |
| 414 |
| 415 test('shows help if included', () { |
| 416 var parser = new ArgParser(includeHelp: true); |
| 417 parser.addFlag('mode', help: 'The mode'); |
| 418 |
| 419 validateUsage(parser, |
| 420 ''' |
| 421 -h, --[no-]help Display usage information |
| 422 --[no-]mode The mode |
| 423 '''); |
| 424 }); |
| 425 |
| 426 test('shows "no-" in flag titles', () { |
| 427 var parser = new ArgParser(includeHelp: false); |
| 428 parser.addFlag('mode', help: 'The mode'); |
| 429 |
| 430 validateUsage(parser, |
| 388 ''' | 431 ''' |
| 389 --[no-]mode The mode | 432 --[no-]mode The mode |
| 390 '''); | 433 '''); |
| 391 }); | 434 }); |
| 392 | 435 |
| 393 test('if there are no abbreviations, there is no column for them', () { | 436 test('omits the column if there are no abbreviations', () { |
| 394 var parser = new ArgParser(); | 437 var parser = new ArgParser(includeHelp: false); |
| 395 parser.addFlag('mode', help: 'The mode'); | 438 parser.addFlag('mode', help: 'The mode'); |
| 396 | 439 |
| 397 validateUsage(parser, | 440 validateUsage(parser, |
| 398 ''' | 441 ''' |
| 399 --[no-]mode The mode | 442 --[no-]mode The mode |
| 400 '''); | 443 '''); |
| 401 }); | 444 }); |
| 402 | 445 |
| 403 test('options are lined up past abbreviations', () { | 446 test('lines up options past abbreviations', () { |
| 404 var parser = new ArgParser(); | 447 var parser = new ArgParser(includeHelp: false); |
| 405 parser.addFlag('mode', abbr: 'm', help: 'The mode'); | 448 parser.addFlag('mode', abbr: 'm', help: 'The mode'); |
| 406 parser.addOption('long', help: 'Lacks an abbreviation'); | 449 parser.addOption('long', help: 'Lacks an abbreviation'); |
| 407 | 450 |
| 408 validateUsage(parser, | 451 validateUsage(parser, |
| 409 ''' | 452 ''' |
| 410 -m, --[no-]mode The mode | 453 -m, --[no-]mode The mode |
| 411 --long Lacks an abbreviation | 454 --long Lacks an abbreviation |
| 412 '''); | 455 '''); |
| 413 }); | 456 }); |
| 414 | 457 |
| 415 test('help text is lined up past the longest option', () { | 458 test('lines up help text past the longest option', () { |
| 416 var parser = new ArgParser(); | 459 var parser = new ArgParser(includeHelp: false); |
| 417 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); | 460 parser.addFlag('mode', abbr: 'm', help: 'Lined up with below'); |
| 418 parser.addOption('a-really-long-name', help: 'Its help text'); | 461 parser.addOption('a-really-long-name', help: 'Its help text'); |
| 419 | 462 |
| 420 validateUsage(parser, | 463 validateUsage(parser, |
| 421 ''' | 464 ''' |
| 422 -m, --[no-]mode Lined up with below | 465 -m, --[no-]mode Lined up with below |
| 423 --a-really-long-name Its help text | 466 --a-really-long-name Its help text |
| 424 '''); | 467 '''); |
| 425 }); | 468 }); |
| 426 | 469 |
| 427 test('leading empty lines are ignored in help text', () { | 470 test('ignores leading empty lines in help text', () { |
| 428 var parser = new ArgParser(); | 471 var parser = new ArgParser(includeHelp: false); |
| 429 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); | 472 parser.addFlag('mode', help: '\n\n\n\nAfter newlines'); |
| 430 | 473 |
| 431 validateUsage(parser, | 474 validateUsage(parser, |
| 432 ''' | 475 ''' |
| 433 --[no-]mode After newlines | 476 --[no-]mode After newlines |
| 434 '''); | 477 '''); |
| 435 }); | 478 }); |
| 436 | 479 |
| 437 test('trailing empty lines are ignored in help text', () { | 480 test('ignores trailing empty lines in help text', () { |
| 438 var parser = new ArgParser(); | 481 var parser = new ArgParser(includeHelp: false); |
| 439 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); | 482 parser.addFlag('mode', help: 'Before newlines\n\n\n\n'); |
| 440 | 483 |
| 441 validateUsage(parser, | 484 validateUsage(parser, |
| 442 ''' | 485 ''' |
| 443 --[no-]mode Before newlines | 486 --[no-]mode Before newlines |
| 444 '''); | 487 '''); |
| 445 }); | 488 }); |
| 446 | 489 |
| 447 test('options are documented in the order they were added', () { | 490 test('documents options in the order they were added', () { |
| 448 var parser = new ArgParser(); | 491 var parser = new ArgParser(includeHelp: false); |
| 449 parser.addFlag('zebra', help: 'First'); | 492 parser.addFlag('zebra', help: 'First'); |
| 450 parser.addFlag('monkey', help: 'Second'); | 493 parser.addFlag('monkey', help: 'Second'); |
| 451 parser.addFlag('wombat', help: 'Third'); | 494 parser.addFlag('wombat', help: 'Third'); |
| 452 | 495 |
| 453 validateUsage(parser, | 496 validateUsage(parser, |
| 454 ''' | 497 ''' |
| 455 --[no-]zebra First | 498 --[no-]zebra First |
| 456 --[no-]monkey Second | 499 --[no-]monkey Second |
| 457 --[no-]wombat Third | 500 --[no-]wombat Third |
| 458 '''); | 501 '''); |
| 459 }); | 502 }); |
| 460 | 503 |
| 461 test('the default value for a flag is shown if on', () { | 504 test('shows the default value for a flag if true', () { |
| 462 var parser = new ArgParser(); | 505 var parser = new ArgParser(includeHelp: false); |
| 463 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); | 506 parser.addFlag('affirm', help: 'Should be on', defaultsTo: true); |
| 464 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); | 507 parser.addFlag('negate', help: 'Should be off', defaultsTo: false); |
| 465 | 508 |
| 466 validateUsage(parser, | 509 validateUsage(parser, |
| 467 ''' | 510 ''' |
| 468 --[no-]affirm Should be on | 511 --[no-]affirm Should be on |
| 469 (defaults to on) | 512 (defaults to on) |
| 470 | 513 |
| 471 --[no-]negate Should be off | 514 --[no-]negate Should be off |
| 472 '''); | 515 '''); |
| 473 }); | 516 }); |
| 474 | 517 |
| 475 test('the default value for an option with no allowed list is shown', () { | 518 test('shows the default value for an option with no allowed list', () { |
| 476 var parser = new ArgParser(); | 519 var parser = new ArgParser(includeHelp: false); |
| 477 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); | 520 parser.addOption('any', help: 'Can be anything', defaultsTo: 'whatevs'); |
| 478 | 521 |
| 479 validateUsage(parser, | 522 validateUsage(parser, |
| 480 ''' | 523 ''' |
| 481 --any Can be anything | 524 --any Can be anything |
| 482 (defaults to "whatevs") | 525 (defaults to "whatevs") |
| 483 '''); | 526 '''); |
| 484 }); | 527 }); |
| 485 | 528 |
| 486 test('the allowed list is shown', () { | 529 test('shows the allowed list', () { |
| 487 var parser = new ArgParser(); | 530 var parser = new ArgParser(includeHelp: false); |
| 488 parser.addOption('suit', help: 'Like in cards', | 531 parser.addOption('suit', help: 'Like in cards', |
| 489 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | 532 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 490 | 533 |
| 491 validateUsage(parser, | 534 validateUsage(parser, |
| 492 ''' | 535 ''' |
| 493 --suit Like in cards | 536 --suit Like in cards |
| 494 [spades, clubs, hearts, diamonds] | 537 [spades, clubs, hearts, diamonds] |
| 495 '''); | 538 '''); |
| 496 }); | 539 }); |
| 497 | 540 |
| 498 test('the default is highlighted in the allowed list', () { | 541 test('highlights the default in the allowed list', () { |
| 499 var parser = new ArgParser(); | 542 var parser = new ArgParser(includeHelp: false); |
| 500 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | 543 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', |
| 501 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); | 544 allowed: ['spades', 'clubs', 'hearts', 'diamonds']); |
| 502 | 545 |
| 503 validateUsage(parser, | 546 validateUsage(parser, |
| 504 ''' | 547 ''' |
| 505 --suit Like in cards | 548 --suit Like in cards |
| 506 [spades, clubs (default), hearts, diamonds] | 549 [spades, clubs (default), hearts, diamonds] |
| 507 '''); | 550 '''); |
| 508 }); | 551 }); |
| 509 | 552 |
| 510 test('the allowed help is shown', () { | 553 test('shows the allowed help', () { |
| 511 var parser = new ArgParser(); | 554 var parser = new ArgParser(includeHelp: false); |
| 512 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', | 555 parser.addOption('suit', help: 'Like in cards', defaultsTo: 'clubs', |
| 513 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], | 556 allowed: ['spades', 'clubs', 'diamonds', 'hearts'], |
| 514 allowedHelp: { | 557 allowedHelp: { |
| 515 'spades': 'Swords of a soldier', | 558 'spades': 'Swords of a soldier', |
| 516 'clubs': 'Weapons of war', | 559 'clubs': 'Weapons of war', |
| 517 'diamonds': 'Money for this art', | 560 'diamonds': 'Money for this art', |
| 518 'hearts': 'The shape of my heart' | 561 'hearts': 'The shape of my heart' |
| 519 }); | 562 }); |
| 520 | 563 |
| 521 validateUsage(parser, | 564 validateUsage(parser, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 throw new IllegalArgumentException( | 622 throw new IllegalArgumentException( |
| 580 'Line "$line" does not have enough indentation.'); | 623 'Line "$line" does not have enough indentation.'); |
| 581 } | 624 } |
| 582 | 625 |
| 583 lines[i] = line.substring(indent); | 626 lines[i] = line.substring(indent); |
| 584 } | 627 } |
| 585 } | 628 } |
| 586 | 629 |
| 587 return Strings.join(lines, '\n'); | 630 return Strings.join(lines, '\n'); |
| 588 } | 631 } |
| OLD | NEW |