File Coverage

lib/Test/Classy/Base.pm
Criterion Covered Total %
statement 196 200 98.0
branch 65 72 90.2
condition 6 7 85.7
subroutine 35 36 97.2
pod 6 6 100.0
total 308 321 95.9


line stmt bran cond sub pod time code
1             package Test::Classy::Base;
2              
3 13     13   137971 use strict;
  13         86  
  13         494  
4 13     13   91 use warnings;
  13         53  
  13         693  
5 13     13   113 use base qw( Class::Data::Inheritable );
  13         22  
  13         6698  
6 13     13   4574 use Test::More ();
  13         26  
  13         234  
7 13     13   6784 use Data::Dump;
  13         105904  
  13         1174  
8 13     13   730 use Class::Inspector;
  13         4428  
  13         434  
9 13     13   2376 use Encode;
  13         88136  
  13         1512  
10 13     13   6821 use Term::Encoding;
  13         9914  
  13         799  
11 13     13   727 use Test::Classy::Util;
  13         29  
  13         1634  
12              
13             my $ENCODE = eval { find_encoding(Term::Encoding::get_encoding()) };
14              
15             sub import {
16 54     54   14840 my ($class, @flags) = @_;
17 54         157 my $caller = caller;
18              
19 54 100       202 if ( $class ne __PACKAGE__ ) {
20 26 100       301 return unless grep { $_ eq 'base' } @flags;
  2         8  
21             }
22              
23 13     13   83 no strict 'refs';
  13         35  
  13         10984  
24 30         50 push @{"$caller\::ISA"}, $class;
  30         404  
25              
26 30 50       183 Test::Stream::Toolset::init_tester($caller) if $INC{'Test/Stream/Toolset.pm'};
27              
28             # XXX: not sure why but $TODO refused to be exported well
29 30         55 *{"$caller\::TODO"} = \$Test::More::TODO;
  30         187  
30              
31 30         96 foreach my $export ( @Test::More::EXPORT ) {
32 840 100       1998 next if $export =~ /^\W/;
33 810         1025 *{"$caller\::$export"} = \&{"Test::More\::$export"};
  810         3306  
  810         1968  
34             }
35              
36 30 100       101 if ( grep { $_ eq 'ignore' or $_ eq 'ignore_me' } @flags ) {
  4 100       29  
37 2         3 ${"$caller\::_ignore_me"} = 1;
  2         9  
38             }
39              
40 30 100       633 if ( $class eq __PACKAGE__ ) {
41 28         337 $caller->mk_classdata( _tests => {} );
42 28         904 $caller->mk_classdata( _plan => 0 );
43 28         573 $caller->mk_classdata( test_name => '' );
44             }
45             }
46              
47             sub MODIFY_CODE_ATTRIBUTES {
48 66     66   39328 my ($class, $code, @attrs) = @_;
49              
50 66         120 my %stash;
51 66         181 foreach my $attr ( @attrs ) {
52 86 100       628 if ( $attr eq 'Test' ) {
    100          
    100          
53 31         92 $stash{plan} = 1;
54             }
55             elsif ( my ($dummy, $plan) = $attr =~ /^Tests?\((['"]?)(\d+|no_plan)\1\)$/ ) {
56 31         217 $stash{plan} = $plan;
57             }
58             elsif ( my ($type, $dummy2, $reason) = $attr =~ /^(Skip|TODO)(?:\((['"]?)(.+)\)\2)?$/ ) {
59 19         66 $stash{$type} = $reason;
60             }
61             else {
62 5         14 $stash{$attr} = 1;
63             }
64             }
65 66 100       203 return unless $stash{plan};
66              
67 62 100       169 if ( $stash{plan} eq 'no_plan' ) {
68 3 100       11 Test::More::plan 'no_plan' unless Test::Classy::Util::_planned();
69 3         1178 $stash{plan} = 0;
70             }
71              
72 62         219 $class->_plan( $class->_plan + $stash{plan} );
73              
74 62         1095 $stash{code} = $code;
75              
76             # At this point, the name looks like CODE(...)
77             # we'll make it human-readable later, with class inspection
78 62         193 $class->_tests->{$code} = \%stash;
79              
80 62         672 return;
81             }
82              
83             sub _limit {
84 2     2   6 my ($class, @monikers) = @_;
85              
86 2         7 my $tests = $class->_tests;
87 2         18 my $reason = 'limited by attributes';
88              
89             LOOP:
90 2         3 foreach my $name ( keys %{ $tests } ) {
  2         7  
91 3         4 foreach my $moniker ( @monikers ) {
92 3 100       11 next LOOP if exists $tests->{$name}->{$moniker};
93             }
94 2         9 $tests->{$name}->{Skip} = $reason;
95             }
96             }
97              
98             sub _should_be_ignored {
99 25     25   56 my $class = shift;
100              
101 13     13   100 { no strict 'refs';
  13         30  
  13         8251  
  25         44  
102 25 100       38 if ( ${"$class\::_ignore_me"} ) {
  25         293  
103             SKIP: {
104 2         25 Test::More::skip 'a base class, not to test', $class->_plan;
  2         7  
105             }
106 2         1327 return 1;
107             }
108             }
109             }
110              
111             sub _find_symbols {
112 23     23   64 my $class = shift;
113              
114             # to allow multibyte method names
115 23         135 local $Class::Inspector::RE_IDENTIFIER = qr/.+/s;
116              
117 23         154 my $methods = Class::Inspector->methods($class, 'expanded');
118              
119 23         31057 my %symbols;
120 23         42 foreach my $entry ( @{ $methods } ) {
  23         70  
121 1582         3761 $symbols{$entry->[3]} = $entry->[2]; # coderef to sub name
122             }
123 23         1239 return %symbols;
124             }
125              
126             sub _run_tests {
127 25     25   69 my ($class, @args) = @_;
128              
129 25 100       191 return if $class->_should_be_ignored;
130              
131 23         208 my %sym = $class->_find_symbols;
132              
133 23         244 $class->test_name( undef );
134              
135 23         529 $class->initialize(@args);
136              
137 23         105 my $tests = $class->_tests;
138              
139 23         194 foreach my $name ( sort { $sym{$a} cmp $sym{$b} } grep { $sym{$_} } keys %{ $tests } ) {
  33         112  
  50         214  
  23         74  
140 44 50       1461 next if $sym{$name} =~ /^(?:initialize|finalize)$/;
141              
142 44 100       218 if ( my $reason = $class->_should_skip_this_class ) {
143 6         12 SKIP: { Test::More::skip $class->message($reason), $tests->{$name}->{plan}; }
  6         25  
144 6         5743 next;
145             }
146              
147 38         201 $class->_run_test( $tests->{$name}, $sym{$name}, @args );
148             }
149              
150 23         195 $class->finalize(@args);
151             }
152              
153             sub _run_test {
154 38     38   138 my ($class, $test, $name, @args) = @_;
155              
156 38         138 $class->test_name( $name );
157 38         377 $class->_clear_skip_flag;
158              
159 38 100       159 if ( exists $test->{TODO} ) {
    100          
160             my $reason = defined $test->{TODO}
161             ? $test->{TODO}
162 7 100       26 : "$name is not implemented";
163              
164 7 100       20 if ( exists $test->{Skip} ) { # todo skip
165             TODO: {
166 1         3 Test::More::todo_skip $class->message($reason), $test->{plan};
  1         5  
167             }
168             }
169             else {
170             TODO: {
171 13     13   110 no strict 'refs';
  13         29  
  13         7581  
  6         11  
172 6         34 local ${"$class\::TODO"} = $class->message($reason); # perl 5.6.2 hates this
  6         58  
173              
174 6         26 $class->__run_test($test, @args);
175             }
176             }
177 7         2220 return;
178             }
179             elsif ( exists $test->{Skip} ) {
180             my $reason = defined $test->{Skip}
181             ? $test->{Skip}
182 5 100       15 : "skipped $name";
183 5         10 SKIP: { Test::More::skip $class->message($reason), $test->{plan}; }
  5         16  
184 5         5743 return;
185             }
186              
187 26         141 $class->__run_test($test, @args);
188             }
189              
190             sub __run_test {
191 32     32   82 my ($class, $test, @args) = @_;
192              
193 32         160 my $current = Test::Classy::Util::_current_test();
194              
195 32         7491 local $@;
196 32         75 eval { $test->{code}($class, @args); };
  32         141  
197 32 100       17433 if ( $@ ) {
198 3         12 my $done = Test::Classy::Util::_current_test() - $current;
199 3         556 my $rest = $test->{plan} - $done;
200 3 50       11 if ( $rest ) {
201 3 50       8 if ( exists $test->{TODO} ) {
202             my $reason = defined $test->{TODO}
203             ? $test->{TODO}
204 3 50       10 : 'not implemented';
205             TODO: {
206 3         4 Test::More::todo_skip( $class->message("$reason: $@"), $rest );
  3         13  
207             }
208             }
209             else {
210 0         0 for ( 1 .. $rest ) {
211 0         0 Test::More::ok( 0, $class->message($@) );
212             }
213             }
214             }
215             }
216              
217 32 100       2959 if ( my $reason = $class->_is_skipped ) {
218 3         10 my $done = Test::Classy::Util::_current_test() - $current;
219 3         522 my $rest = $test->{plan} - $done;
220 3 100       11 if ( $rest ) {
221 2         7 for ( 1 .. $rest ) {
222 2         8 Test::More->builder->skip( $class->message($reason) );
223             }
224             }
225             }
226             }
227              
228             sub skip_this_class {
229 4     4 1 43 my ($class, $reason) = @_;
230              
231 13     13   116 no strict 'refs';
  13         45  
  13         1537  
232 4   100     18 ${"$class\::_skip_this_class"} = $reason || 'for some reason';
  4         29  
233             }
234              
235             *skip_the_rest = \&skip_this_class;
236              
237             sub _should_skip_this_class {
238 44     44   99 my $class = shift;
239              
240 13     13   91 no strict 'refs';
  13         62  
  13         1211  
241 44         109 return ${"$class\::_skip_this_class"};
  44         341  
242             }
243              
244             sub skip_this_test {
245 3     3 1 12 my ($class, $reason) = @_;
246              
247 13     13   112 no strict 'refs';
  13         35  
  13         1183  
248 3   100     14 ${"$class\::_skip_this_test"} = $reason || 'for some reason';
  3         20  
249             }
250              
251             *abort_this_test = \&skip_this_test;
252              
253             sub _clear_skip_flag {
254 38     38   63 my $class = shift;
255              
256 13     13   77 no strict 'refs';
  13         23  
  13         997  
257 38         67 ${"$class\::_skip_this_test"} = '';
  38         154  
258             }
259              
260             sub _is_skipped {
261 32     32   95 my $class = shift;
262              
263 13     13   103 no strict 'refs';
  13         66  
  13         6288  
264 32         56 return ${"$class\::_skip_this_test"};
  32         317  
265             }
266              
267             sub dump {
268 0     0 1 0 my $class = shift;
269 0         0 Test::More::diag( Data::Dump::dump( @_ ) );
270             }
271              
272             sub message {
273 63     63 1 2215 my ($class, $message) = @_;
274              
275 63         213 $message = $class->_prepend_class_name( $class->_prepend_test_name( $message ) );
276              
277 63 100 66     504 $message = $ENCODE->encode($message) if $ENCODE && Encode::is_utf8($message);
278              
279 63         325 return $message;
280             }
281              
282             sub _prepend_test_name {
283 63     63   130 my ($class, $message) = @_;
284              
285 63 100       224 $message = '' unless defined $message;
286              
287 63 100       177 if ( my $name = $class->test_name ) {
288 57 100       1631 $message = "$name: $message" unless $message =~ /\b$name\b/;
289             }
290              
291 63         352 return $message;
292             }
293              
294             sub _prepend_class_name {
295 63     63   129 my ($class, $message) = @_;
296              
297 63 50       147 $message = '' unless defined $message;
298              
299 63 50       677 if ( my ($name) = $class =~ /(\w+)$/ ) {
300 63 100       734 $message = "$name: $message" unless $message =~ /\b$name\b/;
301             }
302              
303 63         154 return $message;
304             }
305              
306       18 1   sub initialize {}
307       23 1   sub finalize {}
308              
309             1;
310              
311             __END__