Config.t 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Fcm::CLI::Config::Default;
  5. use Fcm::CLI::Subcommand;
  6. use Test::More qw{no_plan};
  7. main();
  8. sub main {
  9. my $class = 'Fcm::CLI::Config';
  10. use_ok($class);
  11. test_get_instance($class);
  12. test_get_subcommand_of_string($class);
  13. }
  14. ################################################################################
  15. # Tests normal usage of getting an instance
  16. sub test_get_instance {
  17. my ($class) = @_;
  18. my $prefix = 'constructor';
  19. my $cli_config = $class->instance();
  20. isa_ok($cli_config, $class, "$prefix");
  21. is_deeply(
  22. [$cli_config->get_core_subcommands()],
  23. \@Fcm::CLI::Config::Default::CORE_SUBCOMMANDS,
  24. "$prefix: default core",
  25. );
  26. is_deeply(
  27. [$cli_config->get_vc_subcommands()],
  28. \@Fcm::CLI::Config::Default::VC_SUBCOMMANDS,
  29. "$prefix: default vc",
  30. );
  31. is_deeply(
  32. [$cli_config->get_subcommands()],
  33. [$cli_config->get_core_subcommands(), $cli_config->get_vc_subcommands()],
  34. "$prefix: default",
  35. );
  36. is($class->instance(), $cli_config, "$prefix: same instance");
  37. isnt($class->instance({}), $cli_config, "$prefix: not the same instance");
  38. my $empty_cli_config = $class->instance({
  39. core_subcommands => [],
  40. vc_subcommands => [],
  41. });
  42. is_deeply(
  43. [$empty_cli_config->get_core_subcommands()],
  44. [],
  45. "$prefix: empty core",
  46. );
  47. is_deeply(
  48. [$empty_cli_config->get_vc_subcommands()],
  49. [],
  50. "$prefix: empty vc",
  51. );
  52. is_deeply(
  53. [$empty_cli_config->get_subcommands()],
  54. [],
  55. "$prefix: empty",
  56. );
  57. }
  58. ################################################################################
  59. # Tests getting a subcommand of a matching string
  60. sub test_get_subcommand_of_string {
  61. my ($class) = @_;
  62. my $prefix = 'get_subcommand_of';
  63. my $foo_subcommand = Fcm::CLI::Subcommand->new({names => ['food', 'foo']});
  64. my $bar_subcommand = Fcm::CLI::Subcommand->new({names => ['barley', 'bar']});
  65. my $cli_config = $class->instance({
  66. core_subcommands => [$foo_subcommand, $bar_subcommand],
  67. vc_subcommands => [],
  68. });
  69. for my $key ('food', 'foo') {
  70. is($cli_config->get_subcommand_of($key), $foo_subcommand,
  71. "$prefix: $key");
  72. }
  73. for my $key ('barley', 'bar') {
  74. is($cli_config->get_subcommand_of($key), $bar_subcommand,
  75. "$prefix: $key");
  76. }
  77. is($cli_config->get_subcommand_of('baz'), undef, "$prefix: baz");
  78. }
  79. __END__