blib/lib/Badger/Test/Manager.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 51 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 330 CC 29 SCAR 28.2
CC
29
Cov
0%
finish
56.0
is
30.0
isnt
20.0
CRAP 16.7
1 package Badger::Test::Manager;
2  
3 use Badger::Class
4 version => 0.01,
5 debug => 1,
6 base => 'Badger::Prototype',
7 import => 'class';
8  
9 use Badger::Rainbow
10 'ANSI_colours ANSI_escape';
11  
12 eval "use Algorithm::Diff qw( diff )";
13 our $CAN_DIFF = $@ ? 0 : 1;
14 our $ESCAPES = qr/\e\[(.*?)m/; # remove ANSI escapes
15 our $REASON = 'No reason given';
16 our $MESSAGES = {
17 no_plan => "You haven't called plan() yet!\n",
18 dup_plan => "You called plan() twice!\n",
19 plan => "1..%s\n",
20 skip_all => "1..0 # skip %s\n",
21 skip_one => "ok %s # skip %s\n",
22 name => "test %s at %s line %s",
23 ok => "ok %s - %s\n",
24 not_ok => "not ok %s - %s\n%s",
25 not_eq => "# expect: [%s]\n# result: [%s]\n",
26 not_ne => "# unexpected match: [%s]\n",
27 not_like => "# expect: /%s/\n# result: [%s]\n",
28 not_unlike => "# expect: ! /%s/\n# result: [%s]\n",
29 too_few => "# Looks like you planned %s tests but only ran %s.\n",
30 too_many => "# Looks like you planned only %s tests but ran %s.\n",
31 no_result => "# result is undefined\n",
32 pass => "# PASS: All %d tests passed\n",
33 fail => "# FAIL: %d tests failed\n",
34 mess => "# FAIL: Inconsistent test results\n",
35 summary => "# %d/%d tests run, %d passed, %d failed, %d skipped\n",
36 hunk => "# -- diffs %s of %s --\n",
37 delta => "# %s %3d %s\n",
38
39 };
40 our $SCHEME = {
41 green => 'ok pass',
42 red => 'not_ok too_few too_many fail mess',
43 cyan => 'skip_one skip_all hunk delta',
44 yellow => 'plan not_eq not_ne not_like not_unlike summary',
45 };
46  
47 # Sorry, English and American/Spanish only, no couleur, colori, farbe, etc.
48 *color = \&colour;
49  
50 # for nice cleanup in END block
51 our $INSTANCES = { };
52  
53  
54 #-----------------------------------------------------------------------
55 # constructor method
56 #-----------------------------------------------------------------------
57  
58 sub init {
59 my ($self, $config) = @_;
60 $self->{ plan } = $config->{ plan } || 0;
61 $self->{ count } = $config->{ count } || 1;
62 $self->{ results } = $config->{ results } || [ ];
63 $self->{ summary } = $config->{ summary } || 0;
64 $self->{ reason } = $config->{ reason } || $REASON;
65 $self->{ colour } = $config->{ colour } || $config->{ color } || 0;
66 $INSTANCES->{ $self } = $self;
67 return $self;
68 }
69  
70  
71 #------------------------------------------------------------------------
72 # plan($n)
73 #
74 # Declare how many (more) tests are expected to come. If ok() is called
75 # before plan() then the results are cached instead of being printed up
76 # front. When plan() is called, the total number of tests (including any
77 # cached) is known and the "1..$n" line can be printed along with any
78 # cached results. After that, calls to ok() generated output immediately.
79 #------------------------------------------------------------------------
80  
81 sub plan ($$;$) {
82 my $self = shift->prototype;
83 my ($tests, $reason) = @_;
84  
85 # calling plan() twice would be ambiguous
86 return $self->error_msg('dup_plan')
87 if $self->{ plan };
88  
89 # if $tests == 0 then skip all
90 return $self->skip_all($reason)
91 unless $tests;
92
93 # update the plan to account for any tests that have already been run
94 my $results = $self->{ results };
95 $tests += @$results;
96 $self->test_msg( plan => $tests );
97 $self->{ plan } = $tests;
98 $self->{ tested } = 0;
99 $self->{ passed } = 0;
100 $self->{ failed } = 0;
101 $self->{ skipped } = 0;
102  
103 # now flush any cached test results
104 while (@$results) {
105 my $test = shift @$results;
106 $self->result(@$test);
107 }
108 }
109  
110 sub ok ($$;$$) {
111 my $self = shift->prototype;
112 my ($ok, $name, $detail) = @_;
113 $detail ||= '';
114  
115 $name ||= $self->test_name;
116  
117 if ($self->{ plan }) {
118 $self->result($ok, $self->{ count }, $name, $detail);
119 }
120 else {
121 # cache results if plan() not yet called
122 push(@{ $self->{ results } }, [ $ok, $self->{ count }, $name, $detail ]);
123 }
124  
125 $self->{ count }++;
126 $self->{ tested }++;
127  
128 return $ok;
129 }
130  
131 sub pass ($;$) {
132 shift->ok(1, @_);
133 }
134  
135 sub fail ($;$) {
136 shift->ok(0, @_);
137 }
138  
139 sub is ($$$;$) {
140 my $self = shift->prototype;
141 my ($result, $expect, $msg) = @_;
142 $msg ||= $self->test_name();
143  
144 if (! defined $result) {
145 return $self->fail($msg, $self->message('no_result'));
146 }
147
148 # force stringification of $result to avoid 'no eq method' overload errors
149 $result = "$result" if ref $result;
150  
151 # if we have coloured output enabled then the result might not match
152 # the expected because of embedded ANSI escapes, so we strip them out
153 my ($r, $e) = map {
154 s/$ESCAPES//g if $self->{ colour };
155 $_
156 } ($result, $expect);
157
158 if ($r eq $e) {
159 return $self->pass($msg);
160 }
161 else {
162 return $self->fail($msg, $self->different($expect, $result));
163 }
164 }
165  
166 sub isnt ($$$;$) {
167 my $self = shift->prototype;
168 my ($result, $expect, $msg) = @_;
169 $msg ||= $self->test_name();
170  
171 # force stringification of $result to avoid 'no eq method' overload errors
172 $result = "$result" if ref $result;
173  
174 # if we have coloured output enabled then the result might not match
175 # the expected because of embedded ANSI escapes, so we strip them out
176 my ($r, $e) = map {
177 s/$ESCAPES//g if $self->{ colour };
178 $_
179 } ($result, $expect);
180
181 if ($r ne $e) {
182 return $self->pass($msg);
183 }
184 else {
185 for ($expect, $result) {
186 s/\n/\n |/g;
187 }
188 return $self->fail($msg, $self->message( not_eq => $expect, $result ));
189 }
190 }
191  
192 sub like ($$$;$) {
193 my $self = shift->prototype;
194 my ($result, $expect, $name) = @_;
195 $name ||= $self->test_name();
196  
197 # strip ANSI escapes if necessary
198 my $r = $result;
199 $r =~ s/$ESCAPES//g if $self->{ colour };
200  
201 if ($r =~ $expect) {
202 $self->pass($name);
203 }
204 else {
205 return $self->fail($name, $self->message( not_like => $expect, $result ));
206 }
207 }
208  
209 sub unlike ($$$;$) {
210 my $self = shift->prototype;
211 my ($result, $expect, $name) = @_;
212 $name ||= $self->test_name();
213  
214 # strip ANSI escapes if necessary
215 my $r = $result;
216 $r =~ s/$ESCAPES//g if $self->{ colour };
217  
218 if ($r !~ $expect) {
219 $self->pass($name);
220 }
221 else {
222 return $self->fail($name, $self->message( not_unlike => $expect, $result ));
223 }
224 }
225  
226 sub skip ($;$) {
227 my $self = shift->prototype;
228 my $msg = shift || $self->test_name;
229  
230 return $self->error_msg('no_plan')
231 unless $self->{ plan };
232  
233 $self->{ tested }++;
234 $self->{ skipped }++;
235 return $self->test_msg( skip_one => $self->{ count }++, $msg );
236 }
237  
238 sub skip_some {
239 my ($self, $n, $msg) = @_;
240 $n = int $n;
241 return unless $n > 0;
242 while ($n--) {
243 $self->skip($msg);
244 }
245 }
246  
247 sub skip_rest {
248 my $self = shift->prototype;
249 my $msg = shift;
250 my $plan = $self->{ plan };
251 while ($self->{ tested } < $plan) {
252 $self->skip($msg);
253 }
254 exit;
255 }
256  
257 sub skip_all ($;$) {
258 my $self = shift->prototype;
259 $self->test_msg( skip_all => shift || $self->{ reason } );
260 exit;
261 }
262  
263 sub result {
264 my $self = shift->prototype;
265 my $ok = shift;
266  
267 return $self->error_msg('no_plan')
268 unless $self->{ plan };
269
270 if ($ok) {
271 $self->{ passed }++;
272 return $self->test_msg( ok => @_ );
273 }
274 else {
275 $self->{ failed }++;
276 return $self->test_msg( not_ok => @_ );
277 }
278 }
279  
280 sub test_msg {
281 my $self = shift;
282 print $self->message(@_);
283 }
284  
285 sub test_name ($) {
286 my $self = shift->prototype;
287 my ($pkg, $file, $line) = caller(2);
288 $self->message( name => $self->{ count }, $file, $line );
289 }
290  
291 sub different {
292 my ($self, $expect, $result) = @_;
293 my ($pad_exp, $pad_res) = ($expect, $result);
294 for ($pad_exp, $pad_res) {
295 s/\n/\n# |/g;
296 }
297 my $msg = $self->message( not_eq => $pad_exp, $pad_res );
298  
299 return $msg
300 unless $CAN_DIFF;
301  
302 my $diffs = diff( map { [ split(/\n/) ] } $expect, $result );
303 my $n = 0;
304 my $m = scalar @$diffs;
305
306 foreach my $hunk (@$diffs) {
307 $msg .= $self->message( hunk => ++$n, $m );
308 foreach my $delta (@$hunk) {
309 $msg .= $self->message( delta => @$delta );
310 }
311 # $msg .= "\n";
312 }
313 return $msg;
314 }
315  
316 sub colour {
317 my $self = shift->prototype;
318 my $ansi = ANSI_colours;
319  
320 # enable colour mode by inserting ANSI escapes into $MESSAGES
321 if (@_ && ($self->{ colour } = shift)) {
322 foreach my $col (keys %$SCHEME) {
323 my $code = $ansi->{ $col }
324 || $self->error("Invalid colour name in \$SCHEME: $col\n");
325 $MESSAGES->{ $_ } = ANSI_escape($code, $MESSAGES->{ $_ })
326 for split(/\s+/, $SCHEME->{ $col });
327 }
328 Badger::Debug->enable_colour;
329 }
330  
331 return $self->{ colour };
332 }
333  
334 sub flush {
335 my $self = shift->prototype;
336 my $results = shift || $self->{ results };
337 return unless @$results;
338 $self->{ plan } ||= @$results;
339 while (@$results) {
340 my $test = shift @$results;
341 $self->result(@$test);
342 }
343 }
344  
345 sub summary {
346 my $self = shift->prototype;
347 return @_
348 ? ($self->{ summary } = shift)
349 : $self->{ summary };
350 }
351  
352 sub finish {
353 my $self = shift->prototype;
354  
355 $self->flush; # output any cached results
356  
357 my ($plan, $ran, $pass, $fail, $skip)
358 = @$self{ qw( plan tested passed failed skipped ) };
359
360 return unless $plan;
361  
362 # mandatory warnings about too many/too few
363 if ($ran < $plan) {
364 $self->test_msg( too_few => $plan, $ran );
365 }
366 elsif ($ran > $plan) {
367 $self->test_msg( too_many => $plan, $ran );
368 }
369  
370 # optional summary follows for those who want it
371 return unless $self->{ summary };
372
373 my $good = $pass + $skip;
374
375 if ($fail) {
376 $self->test_msg( fail => $fail );
377 }
378 elsif ($good == $plan) {
379 $self->test_msg( pass => $plan );
380 }
381 else {
382 $self->test_msg('mess');
383 }
384 $self->test_msg( summary => $ran, $plan, $pass, $fail, $skip );
385  
386 # remove ourselves from the index
387 delete $INSTANCES->{ $self };
388
389 }
390  
391 END {
392 # Cleanup test managers so they can report errors using test_msg(). If
393 # we leave it until global destruction (e.g. by using a DESTROY method to
394 # call finish() then there's a chance that the Badger::Class object that
395 # perform the $MESSAGE lookup will have already been cleaned up.
396 $_->finish for values %$INSTANCES;
397 }
398  
399 1;
400  
401 __END__