Subcommand.t 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Fcm::CLI::Option;
  5. use Test::More qw{no_plan};
  6. main();
  7. sub main {
  8. my $class = 'Fcm::CLI::Subcommand';
  9. use_ok($class);
  10. test_constructor($class);
  11. test_has_a_name($class);
  12. test_as_string($class);
  13. }
  14. ################################################################################
  15. # Tests the constructor
  16. sub test_constructor {
  17. my ($class) = @_;
  18. my $prefix = 'constructor';
  19. my %OPTION_OF = (
  20. description => 'description value',
  21. invoker_class => 'invoker_class value',
  22. invoker_config => 'invoker_config value',
  23. is_vc => 'is_vc value',
  24. names => 'names value',
  25. options => 'options value',
  26. synopsis => 'synopsis value',
  27. usage => 'usage value',
  28. );
  29. my $subcommand = Fcm::CLI::Subcommand->new(\%OPTION_OF);
  30. isa_ok($subcommand, $class, $prefix);
  31. for my $key (keys(%OPTION_OF)) {
  32. my $getter = index($key, 'is') == 0 ? $key : "get_$key";
  33. is($subcommand->$getter(), $OPTION_OF{$key}, "$prefix: $getter");
  34. }
  35. }
  36. ################################################################################
  37. # Tests match a string name to a subcommand
  38. sub test_has_a_name {
  39. my ($class) = @_;
  40. my $prefix = 'has a name';
  41. my @NAMES = ('foo', 'bar', 'baz');
  42. my $subcommand = $class->new({names => \@NAMES});
  43. for my $name (@NAMES) {
  44. ok($subcommand->has_a_name($name), "$prefix: $name");
  45. }
  46. for my $name (qw{egg ham mayo}) {
  47. ok(!$subcommand->has_a_name($name), "$prefix: $name");
  48. }
  49. }
  50. ################################################################################
  51. # Tests string representation of a subcommand
  52. sub test_as_string {
  53. my ($class) = @_;
  54. my $prefix = 'as string';
  55. my %OPTION_OF = (
  56. 'foo (bar, baz)' => ['foo', 'bar', 'baz'],
  57. 'foo (bar)' => ['foo', 'bar'],
  58. 'foo' => ['foo'],
  59. q{} => [],
  60. );
  61. for my $key (keys(%OPTION_OF)) {
  62. my $subcommand = $class->new({names => $OPTION_OF{$key}});
  63. is($subcommand->as_string(), $key, "$prefix: $key");
  64. }
  65. }
  66. __END__