File Coverage

blib/lib/Wetware/CLI/TestSuite.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             #-------------------------------------------------------------------------------
2             # $URL$
3             # $Date$
4             # $Author$
5             # $Revision$
6             #-------------------------------------------------------------------------------
7             package Wetware::CLI::TestSuite;
8              
9 1     1   1054 use strict;
  1         3  
  1         41  
10 1     1   7 use warnings;
  1         2  
  1         32  
11 1     1   987 use Wetware::Test::Suite;
  1         12427  
  1         33  
12 1     1   8 use base q{Wetware::Test::Suite};
  1         2  
  1         65  
13              
14 1     1   458 use Test::Differences qw();
  0            
  0            
15             use Test::More;
16             use Wetware::CLI;
17              
18             #-----------------------------------------------------------------------------
19              
20             sub class_under_test { return 'Wetware::CLI'; }
21              
22             #-----------------------------------------------------------------------------
23              
24             sub test_get_options: Test(3) {
25             my $self = shift;
26             my $cli = $self->object_under_test();
27             #Test::More::can_ok( $cli, 'get_options' );
28             my @args = qw( --verbose );
29             my $expected= { verbose => 1 };
30            
31             my $got = $cli->get_options(@args);
32            
33             Test::Differences::eq_or_diff_text( $got, $expected,
34             'get_options() returns expected hash ref with verbose set' );
35            
36             my (%params, $bad_got);
37             {
38             # Now this is a bit 'over the top' - proving that
39             # we visit the right things.
40             ## no critic (ProhibitProlongedStrictureOverride)
41             no strict qw(refs); ## no critic (ProhibitNoStrict)
42             no warnings qw(redefine); ## no critic (ProhibitNoWarnings)
43              
44             my $class = $self->class_under_test();
45              
46             # remember pod2usage has been imported in to the class_under_test
47             my $method_name = $class . '::pod2usage';
48             local *{$method_name} = sub { %params = @_; return; };
49            
50             my @badArgs = qw(--ReallyBadArg);
51             {
52             # because GetOptions will warn
53             local $SIG{__WARN__} = sub {};
54             $bad_got = $cli->get_options(@badArgs);
55             }
56             }
57             # this is a testability thing - since we have it doing a return on a
58             # call to pod2usage() that will exit...
59             Test::More::ok( ! $bad_got, 'get_options returns undef if GetOptions fail');
60             my %expected_params = (
61             -message => 'Error Parsing GetOptions',
62             -exitval => 2);
63            
64             Test::Differences::eq_or_diff_text( \%params, \%expected_params,
65             'get_options() returns expected params from pod2usaage' );
66             return $self;
67             }
68              
69             sub test_help_or_pod : Test(3) {
70             my $self = shift;
71             my $cli = $self->object_under_test();
72             my @args_passed;
73             {
74             # This is done with intent.
75             # a lovely way to show that setting up sensor methods
76             # can help make clear that the code does what is expected
77             ## no critic (ProhibitProlongedStrictureOverride)
78             no strict qw(refs); ## no critic (ProhibitNoStrict)
79             no warnings qw(redefine); ## no critic (ProhibitNoWarnings)
80              
81             my $class = $self->class_under_test();
82              
83             # remember pod2usage has been imported in to the class_under_test
84             my $method_name = $class . '::pod2usage';
85             local *{$method_name} = sub { @args_passed = @_; return; };
86             my $options = {};
87             $cli->help_or_pod($options);
88             Test::More::ok(! @args_passed, 'help_or_pod does not visit pod2usage');
89            
90             $options = { 'help' => 1 };
91             my $expected_help_count = 1; # only the exitval passed as a number
92             $cli->help_or_pod($options);
93             my $got_help_count = scalar @args_passed;
94             Test::More::is($got_help_count, $expected_help_count, 'help_or_pod does for help');
95            
96            
97             $options = { 'pod' => 1 };
98             my $expected_pod_count = 2; # passes a single fat comma
99             $cli->help_or_pod($options);
100             my $got_pod_count = scalar @args_passed;
101             Test::More::is($got_pod_count, $expected_pod_count, 'help_or_pod does for help');
102             }
103             return $self;
104             }
105             #-----------------------------------------------------------------------------
106              
107             sub test_new : Test(1) {
108             my $self = shift;
109             my $object = $self->object_under_test();
110             my $expected_class = $self->class_under_test();
111              
112             Test::More::isa_ok( $object, $expected_class );
113             return $self;
114             }
115              
116             #-----------------------------------------------------------------------------
117              
118             sub test_option_defaults : Test(1) {
119             my $self = shift;
120             my $cli = $self->object_under_test();
121             #Test::More::can_ok( $cli, 'option_defaults' );
122             my $expected ={};
123             my $got = $cli->option_defaults();
124            
125             Test::Differences::eq_or_diff_text( $got, $expected,
126             'option_defaults() returns expected empty hash' );
127             return $self;
128             }
129              
130             sub test_option_specifications : Test(1) {
131             my $self = shift;
132             my $cli = $self->object_under_test();
133             ##Test::More::can_ok( $cli, 'option_specifications' );
134             my @expected = qw(
135             verbose
136             help
137             pod
138             );
139             my @got = $cli->option_specifications();
140            
141             Test::Differences::eq_or_diff_text( \@got, \@expected,
142             'option_specifications() returns expected list' );
143             return $self;
144             }
145              
146             sub test_required_settings : Test(1) {
147             my $self = shift;
148             my $cli = $self->object_under_test();
149             Test::More::can_ok( $cli, 'required_settings' );
150             return $self;
151             }
152              
153             sub test_verify_required_options: Test(3) {
154             my $self = shift;
155             my $cli = $self->object_under_test();
156             #Test::More::can_ok( $cli, 'verify_required_options' );
157             my $visited_pod_to_usage;
158             {
159             # This is done with intent.
160             # a lovely way to show that setting up sensor methods
161             # can help make clear that the code does what is expected
162             ## no critic (ProhibitProlongedStrictureOverride)
163             no strict qw(refs); ## no critic (ProhibitNoStrict)
164             no warnings qw(redefine); ## no critic (ProhibitNoWarnings)
165              
166             my $class = $self->class_under_test();
167              
168             # remember pod2usage has been imported in to the class_under_test
169             my $method_name = $class . '::pod2usage';
170             local *{$method_name} = sub { $visited_pod_to_usage++; return; };
171            
172             # there are no required ones by default.
173             my $options = {};
174             $cli->verify_required_options($options);
175             Test::More::ok(! $visited_pod_to_usage , 'verify_required_options default');
176            
177             # now to show that we will visit the pod2usage IF the required_attribute is not found.
178             my $required_attribute = 'SuperSpecialTestRequiredAttributeStringLeastLikelyToBeFoundInTheWilds';
179             my $required_settings = $class . '::required_settings';
180             local *{$required_settings} = sub { return $required_attribute };
181             $cli->verify_required_options($options);
182             Test::More::ok($visited_pod_to_usage , 'verify_required_options visits pod2usage');
183            
184             # now reset and recheck with the required attribute in the options.
185             $visited_pod_to_usage = 0;
186             $options->{$required_attribute} = 1;
187             $cli->verify_required_options($options);
188             Test::More::ok(! $visited_pod_to_usage , 'verify_required_options if option set');
189            
190             }
191             return $self;
192             }
193              
194             sub test_remaining_argv : Test(2) {
195             my $self = shift;
196             my $cli = $self->object_under_test();
197             #Test::More::can_ok( $cli, 'remaining_argv' );
198            
199             my $no_new_opt = { 'stuff' => 1 };
200             my %expected_no_new_options = %{$no_new_opt};
201             my @empty_argv;
202             $cli->remaining_argv($no_new_opt, @empty_argv);
203            
204             Test::Differences::eq_or_diff_text( $no_new_opt, \%expected_no_new_options,
205             'remaining_argv() no elements in @argv' );
206              
207            
208             my $opt = { 'stuff' => 1 };
209             my %expected_options = %{$opt};
210             my $remaining_value = 'one_arg';
211             $expected_options{'remaining_argv'} = [ $remaining_value ];
212             my @argv = ( $remaining_value );
213             $cli->remaining_argv($opt, @argv);
214            
215             Test::Differences::eq_or_diff_text( $opt, \%expected_options,
216             'remaining_argv() at least one elements in @argv' );
217              
218             return $self;
219             }
220              
221             #-----------------------------------------------------------------------------
222              
223             1;
224              
225             __END__
226              
227             =pod
228              
229             =head1 NAME
230              
231             Wetware::CLI::TestSuite - The CLI Test::Class
232              
233             =head1 SYNOPSIS
234              
235             This requires Wetware::Test distribution. It provides the basic
236             testing of the Modules.
237              
238             =head1 OVERRIDDEN ETHODS
239              
240             =head2 class_under_test()
241              
242             =head1 TEST METHODS
243              
244             We use the naming convention I<test_METHODNAME> for test methods.
245              
246             =head2 test_get_options()
247              
248             =head2 test_help_or_pod()
249              
250             =head2 test_new()
251              
252             =head2 test_option_defaults()
253              
254             =head2 test_option_specifications()
255              
256             =head2 test_required_settings()
257              
258             =head2 test_verify_required_options()
259              
260             =head2 test_verify_required_options()
261              
262             =head2 test_remaining_argv()
263              
264             =head1 COPYRIGHT & LICENSE
265              
266             Copyright 2009 "drieux", all rights reserved.
267              
268             This program is free software; you can redistribute it and/or modify it
269             under the same terms as Perl itself.
270              
271             =cut
272              
273             # End of Wetware::CLI