Interactive.t 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. ################################################################################
  5. # A sub-class of Fcm::Interactive::InputGetter for testing
  6. {
  7. package TestInputGetter;
  8. use base qw{Fcm::Interactive::InputGetter};
  9. ############################################################################
  10. # A callback for testing
  11. sub get_callback {
  12. my ($self) = @_;
  13. return $self->{callback};
  14. }
  15. ############################################################################
  16. # Returns some pre-defined strings
  17. sub invoke {
  18. my ($self) = @_;
  19. $self->get_callback()->(
  20. $self->get_title(),
  21. $self->get_message(),
  22. $self->get_type(),
  23. $self->get_default(),
  24. );
  25. return 'answer';
  26. }
  27. }
  28. use Test::More qw{no_plan};
  29. main();
  30. sub main {
  31. use_ok('Fcm::Interactive');
  32. test_default_impl();
  33. test_set_impl();
  34. test_get_input();
  35. }
  36. ################################################################################
  37. # Tests default setting of input getter implementation
  38. sub test_default_impl {
  39. my $prefix = 'default impl';
  40. my ($class_name, $class_options_ref) = Fcm::Interactive::get_default_impl();
  41. is($class_name, 'Fcm::Interactive::InputGetter::CLI', "$prefix: class name");
  42. is_deeply($class_options_ref, {}, "$prefix: class options");
  43. }
  44. ################################################################################
  45. # Tests setting the input getter implementation
  46. sub test_set_impl {
  47. my $prefix = 'set impl';
  48. my %options = (extra => 'extra-value');
  49. my $name = 'TestInputGetter';
  50. Fcm::Interactive::set_impl($name, \%options);
  51. my ($class_name, $class_options_ref) = Fcm::Interactive::get_impl();
  52. is($class_name, $name, "$prefix: class name");
  53. is_deeply($class_options_ref, \%options, "$prefix: class options");
  54. }
  55. ################################################################################
  56. # Tests getting input with test input getter
  57. sub test_get_input {
  58. my $prefix = 'get input';
  59. my %EXPECTED = (
  60. TITLE => 'title-value',
  61. MESSAGE => 'message-value',
  62. TYPE => 'type-value',
  63. DEFAULT => 'default-value',
  64. ANSWER => 'answer',
  65. );
  66. Fcm::Interactive::set_impl('TestInputGetter', {
  67. callback => sub {
  68. my ($title, $message, $type, $default) = @_;
  69. is($title, $EXPECTED{TITLE}, "$prefix: title");
  70. is($message, $EXPECTED{MESSAGE}, "$prefix: message");
  71. is($type, $EXPECTED{TYPE}, "$prefix: type");
  72. is($default, $EXPECTED{DEFAULT}, "$prefix: default");
  73. },
  74. });
  75. my $ans = Fcm::Interactive::get_input(
  76. title => $EXPECTED{TITLE},
  77. message => $EXPECTED{MESSAGE},
  78. type => $EXPECTED{TYPE},
  79. default => $EXPECTED{DEFAULT},
  80. );
  81. is($ans, $EXPECTED{ANSWER}, "$prefix: answer");
  82. }
  83. __END__