Option.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More qw{no_plan};
  5. main();
  6. sub main {
  7. my $class = 'Fcm::CLI::Option';
  8. use_ok($class);
  9. test_simplest($class);
  10. test_simplest_scalar_arg($class);
  11. test_simplest_array_arg($class);
  12. test_simplest_hash_arg($class);
  13. test_simple($class);
  14. test_simple_scalar_arg($class);
  15. test_simple_array_arg($class);
  16. test_simple_hash_arg($class);
  17. test_long_letter($class);
  18. }
  19. ################################################################################
  20. # Tests simplest usage
  21. sub test_simplest {
  22. my ($class) = @_;
  23. my $prefix = 'simplest';
  24. my $option = $class->new({
  25. delimiter => 'delimiter-value',
  26. description => 'description value',
  27. name => 'name-value',
  28. });
  29. isa_ok($option, $class);
  30. is($option->get_delimiter(), 'delimiter-value', "$prefix: delimiter");
  31. is($option->get_description(), 'description value', "$prefix: description");
  32. is($option->get_name(), 'name-value', "$prefix: name");
  33. is($option->get_letter(), undef, "$prefix: letter");
  34. ok(!$option->has_arg(), "$prefix: has arg");
  35. is($option->get_arg_for_getopt_long(), 'name-value', "$prefix: has arg");
  36. }
  37. ################################################################################
  38. # Tests simplest usage with a scalar argument
  39. sub test_simplest_scalar_arg {
  40. my ($class) = @_;
  41. my $prefix = 'simplest scalar arg';
  42. my $option = $class->new({
  43. description => 'description value',
  44. name => 'name-value',
  45. has_arg => $class->SCALAR_ARG,
  46. });
  47. isa_ok($option, $class);
  48. is($option->has_arg(), $class->SCALAR_ARG, "$prefix: has arg");
  49. is($option->get_arg_for_getopt_long(), 'name-value=s', "$prefix: has arg");
  50. }
  51. ################################################################################
  52. # Tests simplest usage with array argument
  53. sub test_simplest_array_arg {
  54. my ($class) = @_;
  55. my $prefix = 'simplest array arg';
  56. my $option = $class->new({
  57. description => 'description value',
  58. name => 'name-value',
  59. has_arg => $class->ARRAY_ARG,
  60. });
  61. isa_ok($option, $class);
  62. is($option->has_arg(), $class->ARRAY_ARG, "$prefix: has arg");
  63. is($option->get_arg_for_getopt_long(), 'name-value=s@', "$prefix: has arg");
  64. }
  65. ################################################################################
  66. # Tests simplest usage with hash argument
  67. sub test_simplest_hash_arg {
  68. my ($class) = @_;
  69. my $prefix = 'simplest hash arg';
  70. my $option = $class->new({
  71. description => 'description value',
  72. name => 'name-value',
  73. has_arg => $class->HASH_ARG,
  74. });
  75. isa_ok($option, $class);
  76. is($option->has_arg(), $class->HASH_ARG, "$prefix: has arg");
  77. is($option->get_arg_for_getopt_long(), 'name-value=s%', "$prefix: has arg");
  78. }
  79. ################################################################################
  80. # Tests simple usage
  81. sub test_simple {
  82. my ($class) = @_;
  83. my $prefix = 'simple';
  84. my $option = $class->new({
  85. description => 'description value',
  86. name => 'name-value',
  87. letter => 'n',
  88. });
  89. isa_ok($option, $class);
  90. is($option->get_description(), 'description value', "$prefix: description");
  91. is($option->get_name(), 'name-value', "$prefix: name");
  92. is($option->get_letter(), 'n', "$prefix: letter");
  93. is($option->has_arg(), $class->NO_ARG, "$prefix: has arg");
  94. is($option->get_arg_for_getopt_long(), 'name-value|n', "$prefix: has arg");
  95. }
  96. ################################################################################
  97. # Tests simple usage with a scalar argument
  98. sub test_simple_scalar_arg {
  99. my ($class) = @_;
  100. my $prefix = 'simple scalar arg';
  101. my $option = $class->new({
  102. description => 'description value',
  103. name => 'name-value',
  104. letter => 'n',
  105. has_arg => $class->SCALAR_ARG,
  106. });
  107. isa_ok($option, $class);
  108. is($option->has_arg(), $class->SCALAR_ARG, "$prefix: has arg");
  109. is($option->get_arg_for_getopt_long(), 'name-value|n=s', "$prefix: has arg");
  110. }
  111. ################################################################################
  112. # Tests simplest usage with array argument
  113. sub test_simple_array_arg {
  114. my ($class) = @_;
  115. my $prefix = 'simple array arg';
  116. my $option = $class->new({
  117. description => 'description value',
  118. name => 'name-value',
  119. letter => 'n',
  120. has_arg => $class->ARRAY_ARG,
  121. });
  122. isa_ok($option, $class);
  123. is($option->has_arg(), $class->ARRAY_ARG, "$prefix: has arg");
  124. is($option->get_arg_for_getopt_long(), 'name-value|n=s@', "$prefix: has arg");
  125. }
  126. ################################################################################
  127. # Tests simplest usage with hash argument
  128. sub test_simple_hash_arg {
  129. my ($class) = @_;
  130. my $prefix = 'simple hash arg';
  131. my $option = $class->new({
  132. description => 'description value',
  133. name => 'name-value',
  134. letter => 'n',
  135. has_arg => $class->HASH_ARG,
  136. });
  137. isa_ok($option, $class);
  138. is($option->has_arg(), $class->HASH_ARG, "$prefix: has arg");
  139. is($option->get_arg_for_getopt_long(), 'name-value|n=s%', "$prefix: has arg");
  140. }
  141. ################################################################################
  142. # Tests longer than 1 letter
  143. sub test_long_letter {
  144. my ($class) = @_;
  145. my $prefix = 'long letter';
  146. my $option = $class->new({
  147. name => 'name-value',
  148. letter => 'name',
  149. });
  150. isa_ok($option, $class);
  151. is($option->get_letter(), 'n', "$prefix: letter");
  152. is($option->get_arg_for_getopt_long(), 'name-value|n', "$prefix: has arg");
  153. }
  154. __END__