ConfigSystem.t 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. ################################################################################
  5. # A mock Fcm::ConfigSystem object
  6. {
  7. package MockConfigSystem;
  8. use base qw{Fcm::ConfigSystem};
  9. our $LATEST_INVOKED_INSTANCE;
  10. ############################################################################
  11. # Returns the arguments to the last invoke() call
  12. sub get_invoke_args {
  13. my ($self) = @_;
  14. return $self->{invoke_args};
  15. }
  16. ############################################################################
  17. # Does nothing but captures the arguments
  18. sub invoke {
  19. my ($self, %args) = @_;
  20. $LATEST_INVOKED_INSTANCE = $self;
  21. $self->{invoke_args} = \%args;
  22. return 1;
  23. }
  24. }
  25. use Cwd;
  26. use Test::More qw{no_plan};
  27. main();
  28. sub main {
  29. my $class = 'Fcm::CLI::Invoker::ConfigSystem';
  30. use_ok($class);
  31. test_invoke($class);
  32. }
  33. ################################################################################
  34. # Tests normal usage of invoke()
  35. sub test_invoke {
  36. my ($class) = @_;
  37. my $prefix = "invoke";
  38. my %TEST = (
  39. test1 => {
  40. command => 'pig',
  41. options => {'egg' => 1},
  42. arguments => ['bacon'],
  43. expected_options => {FOO => undef, BAR_BAZ => undef, EGGS => 1},
  44. expected_arguments => 'bacon',
  45. },
  46. test2 => {
  47. command => 'pig',
  48. options => {'foo' => 1, 'bar-baz' => 1},
  49. arguments => [],
  50. expected_options => {FOO => 1, BAR_BAZ => 1, EGGS => undef},
  51. expected_arguments => cwd(),
  52. }
  53. );
  54. for my $key (keys(%TEST)) {
  55. my $invoker = $class->new({
  56. command => $TEST{$key}{command},
  57. options => $TEST{$key}{options},
  58. arguments => $TEST{$key}{arguments},
  59. impl_class => 'MockConfigSystem',
  60. cli2invoke_key_map => {
  61. 'foo' => 'FOO', 'bar-baz' => 'BAR_BAZ', 'egg' => 'EGGS',
  62. },
  63. });
  64. isa_ok($invoker, 'Fcm::CLI::Invoker::ConfigSystem', "$prefix: $key");
  65. $invoker->invoke();
  66. my $config_system_instance = $MockConfigSystem::LATEST_INVOKED_INSTANCE;
  67. isa_ok(
  68. $config_system_instance,
  69. 'Fcm::ConfigSystem',
  70. "$prefix: $key: Fcm::ConfigSystem",
  71. );
  72. is(
  73. $config_system_instance->cfg()->src(),
  74. $TEST{$key}{expected_arguments},
  75. "$prefix: $key: cfg()->src()",
  76. );
  77. is_deeply(
  78. $config_system_instance->get_invoke_args(),
  79. $TEST{$key}{expected_options},
  80. "$prefix: $key: invoke args",
  81. );
  82. }
  83. }
  84. __END__