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