lib/Badger/Test/Manager.pm

filter: Statementstmt 60.8% 107 / 176 Branchbran 39.3% 26 / 66 Conditioncond 31.3% 16 / 51 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 72.0% 18 / 25 Podpod 100% 21 / 21 totaltotal 50.8% 188 / 370 CC 59 SCAR 24.5
CC
59
Cov
51%
colour
42.0
different
30.0
finish
17.2
CRAP 11.5
« lib/Badger/Filesystem/Virtual.pm ↑ Summary lib/Badger/Timestamp.pm »
1 package Badger::Test::Manager;
2  
3 use Badger::Class
4 70 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
5 debug => 1,
6 base => 'Badger::Prototype',
7 70 import => 'class';
8  
9 use Badger::Rainbow
10 70 ▶ 'ANSI_colours ANSI_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 70 ▶eval "use Algorithm::Diff qw( diff )";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 70 ▶our $CAN_DIFF = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 70 70
14 70 our $ESCAPES = qr/\e\[(.*?)m/; # remove ANSI escapes
15 70 our $REASON = 'No reason given';
16 70 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 70 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 70 *color = \&colour;
49  
50 # for nice cleanup in END block
51 70 our $INSTANCES = { };
52  
53  
54 #-----------------------------------------------------------------------
55 # constructor method
56 #-----------------------------------------------------------------------
57  
58 69 ▶sub init {
Subroutine init: called CC=8 SCAR=24.0
Pod documented
59 69 my ($self, $config) = @_;
60 69 ▶ $self->{ plan } = $config->{ plan } || 0;
Condition50%1/2
$config->{'plan'} || 0
l!l
069
MC/DC0%0/1
$config->{'plan'} || 0
$config->{'plan'}
Truth table50%1/2
A
A= $config->{'plan'}
Aresult
11
00
61 69 ▶ $self->{ count } = $config->{ count } || 1;
Condition50%1/2
$config->{'count'} || 1
l!l
069
MC/DC0%0/1
$config->{'count'} || 1
$config->{'count'}
Truth table50%1/2
A
A= $config->{'count'}
Aresult
11
00
62 69 ▶ $self->{ results } = $config->{ results } || [ ];
Condition50%1/2
$config->{'results'} || []
l!l
069
MC/DC0%0/1
$config->{'results'} || []
$config->{'results'}
Truth table50%1/2
A
A= $config->{'results'}
Aresult
11
00
63 69 ▶ $self->{ summary } = $config->{ summary } || 0;
Condition50%1/2
$config->{'summary'} || 0
l!l
069
MC/DC0%0/1
$config->{'summary'} || 0
$config->{'summary'}
Truth table50%1/2
A
A= $config->{'summary'}
Aresult
11
00
64 69 ▶ $self->{ reason } = $config->{ reason } || $REASON;
Condition33%1/3
$config->{'reason'} || $Badger::Test::Manager::REASON
l!l&&r!l&&!r
0690
MC/DC0%0/2
$config->{'reason'} || $Badger::Test::Manager::REASON
$config->{'reason'} $Badger::Test::Manager::REASON
Truth table33%1/3
A || B
A= $config->{'reason'}B= $Badger::Test::Manager::REASON
ABresult
1X1
011
000
65 69 ▶ $self->{ colour } = $config->{ colour } || $config->{ color } || 0;
Condition40%2/5
$config->{'colour'} || $config->{'color'} || 0
l!l
069
$config->{"colour"} or $config->{"color"}
l!l&&r!l&&!r
0069
MC/DC0%0/2
$config->{'colour'} || $config->{'color'} || 0
$config->{"colour"} $config->{"color"}
Truth table33%1/3
A or B
A= $config->{"colour"}B= $config->{"color"}
ABresult
1X1
011
000
66 69 $INSTANCES->{ $self } = $self;
67 69 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 65 ▶sub plan ($$;$) {
Subroutine plan: called CC=4 SCAR=14.4
Pod documented
82 65 my $self = shift->prototype;
83 65 my ($tests, $reason) = @_;
84  
85 # calling plan() twice would be ambiguous
86 return $self->error_msg('dup_plan')
87 65 ▶ if $self->{ plan };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"plan"} 0 65 65
88  
89 # if $tests == 0 then skip all
90 65 ▶ return $self->skip_all($reason)
Branch: 1 branch
BranchTrueFalseTotal
unless $tests 0 65 65
91 unless $tests;
92
93 # update the plan to account for any tests that have already been run
94 65 my $results = $self->{ results };
95 65 $tests += @$results;
96 65 $self->test_msg( plan => $tests );
97 65 $self->{ plan } = $tests;
98 65 $self->{ tested } = 0;
99 65 $self->{ passed } = 0;
100 65 $self->{ failed } = 0;
101 65 $self->{ skipped } = 0;
102  
103 # now flush any cached test results
104 65 ▶ while (@$results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$results) 0 65 65
105 0 my $test = shift @$results;
106 0 $self->result(@$test);
107 }
108 }
109  
110 1836 ▶sub ok ($$;$$) {
Subroutine ok: called CC=4 SCAR=14.7
Pod documented
111 1836 my $self = shift->prototype;
112 1836 my ($ok, $name, $detail) = @_;
113 1836 ▶ $detail ||= '';
Condition50%1/2
$detail ||= ""
l!l
01836
MC/DC0%0/1
$detail ||= ""
$detail
Truth table50%1/2
A
A= $detail
Aresult
11
00
114  
115 1836 ▶ $name ||= $self->test_name;
Condition66%2/3
$name ||= $self->test_name
l!l&&r!l&&!r
183510
MC/DC0%0/2
$name ||= $self->test_name
$name $self->test_name
Truth table66%2/3
A ||= B
A= $nameB= $self->test_name
ABresult
1X1
011
000
116  
117 1836 ▶ if ($self->{ plan }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'plan'}) { } 1836 0 1836
118 1836 $self->result($ok, $self->{ count }, $name, $detail);
119 }
120 else {
121 # cache results if plan() not yet called
122 0 push(@{ $self->{ results } }, [ $ok, $self->{ count }, $name, $detail ]);
123 }
124  
125 1836 $self->{ count }++;
126 1836 $self->{ tested }++;
127  
128 1836 return $ok;
129 }
130  
131 1293 ▶sub pass ($;$) {
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
132 1293 shift->ok(1, @_);
133 }
134  
135 0 ▶sub fail ($;$) {
Subroutine fail: not called CC=1 SCAR=6.9
Pod documented
136 0 shift->ok(0, @_);
137 }
138  
139 1222 ▶sub is ($$$;$) {
Subroutine is: called CC=6 SCAR=19.5
Pod documented
140 1222 my $self = shift->prototype;
141 1222 my ($result, $expect, $msg) = @_;
142 1222 ▶ $msg ||= $self->test_name();
Condition33%1/3
$msg ||= $self->test_name
l!l&&r!l&&!r
122200
MC/DC0%0/2
$msg ||= $self->test_name
$msg $self->test_name
Truth table33%1/3
A ||= B
A= $msgB= $self->test_name
ABresult
1X1
011
000
143  
144 1222 ▶ if (! defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 1222 1222
145 0 return $self->fail($msg, $self->message('no_result'));
146 }
147
148 # force stringification of $result to avoid 'no eq method' overload errors
149 1222 ▶ $result = "$result" if ref $result;
Branch: 1 branch
BranchTrueFalseTotal
if ref $result 116 1106 1222
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 1222 ▶ s/$ESCAPES//g if $self->{ colour };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"colour"} 0 2444 2444
155 2444 $_
156 } ($result, $expect);
157
158 1222 ▶ if ($r eq $e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq $e) { } 1222 0 1222
159 1222 return $self->pass($msg);
160 }
161 else {
162 0 return $self->fail($msg, $self->different($expect, $result));
163 }
164 }
165  
166 5 ▶sub isnt ($$$;$) {
Subroutine isnt: called CC=6 SCAR=19.9
Pod documented
167 5 my $self = shift->prototype;
168 5 my ($result, $expect, $msg) = @_;
169 5 ▶ $msg ||= $self->test_name();
Condition33%1/3
$msg ||= $self->test_name
l!l&&r!l&&!r
500
MC/DC0%0/2
$msg ||= $self->test_name
$msg $self->test_name
Truth table33%1/3
A ||= B
A= $msgB= $self->test_name
ABresult
1X1
011
000
170  
171 # force stringification of $result to avoid 'no eq method' overload errors
172 5 ▶ $result = "$result" if ref $result;
Branch: 1 branch
BranchTrueFalseTotal
if ref $result 2 3 5
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 5 ▶ s/$ESCAPES//g if $self->{ colour };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"colour"} 0 10 10
178 10 $_
179 } ($result, $expect);
180
181 5 ▶ if ($r ne $e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r ne $e) { } 5 0 5
182 5 return $self->pass($msg);
183 }
184 else {
185 0 for ($expect, $result) {
186 0 s/\n/\n |/g;
187 }
188 0 return $self->fail($msg, $self->message( not_eq => $expect, $result ));
189 }
190 }
191  
192 42 ▶sub like ($$$;$) {
Subroutine like: called CC=4 SCAR=15.2
Pod documented
193 42 my $self = shift->prototype;
194 42 my ($result, $expect, $name) = @_;
195 42 ▶ $name ||= $self->test_name();
Condition33%1/3
$name ||= $self->test_name
l!l&&r!l&&!r
4200
MC/DC0%0/2
$name ||= $self->test_name
$name $self->test_name
Truth table33%1/3
A ||= B
A= $nameB= $self->test_name
ABresult
1X1
011
000
196  
197 # strip ANSI escapes if necessary
198 42 my $r = $result;
199 42 ▶ $r =~ s/$ESCAPES//g if $self->{ colour };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"colour"} 0 42 42
200  
201 42 ▶ if ($r =~ $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /$expect/) { } 42 0 42
202 42 $self->pass($name);
203 }
204 else {
205 0 return $self->fail($name, $self->message( not_like => $expect, $result ));
206 }
207 }
208  
209 2 ▶sub unlike ($$$;$) {
Subroutine unlike: called CC=4 SCAR=15.2
Pod documented
210 2 my $self = shift->prototype;
211 2 my ($result, $expect, $name) = @_;
212 2 ▶ $name ||= $self->test_name();
Condition33%1/3
$name ||= $self->test_name
l!l&&r!l&&!r
200
MC/DC0%0/2
$name ||= $self->test_name
$name $self->test_name
Truth table33%1/3
A ||= B
A= $nameB= $self->test_name
ABresult
1X1
011
000
213  
214 # strip ANSI escapes if necessary
215 2 my $r = $result;
216 2 ▶ $r =~ s/$ESCAPES//g if $self->{ colour };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"colour"} 0 2 2
217  
218 2 ▶ if ($r !~ $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $r =~ /$expect/) { } 2 0 2
219 2 $self->pass($name);
220 }
221 else {
222 0 return $self->fail($name, $self->message( not_unlike => $expect, $result ));
223 }
224 }
225  
226 0 ▶sub skip ($;$) {
Subroutine skip: not called CC=3 SCAR=24.8
Pod documented
227 0 my $self = shift->prototype;
228 0 ▶ my $msg = shift || $self->test_name;
Condition0%0/3
shift() || $self->test_name
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->test_name
shift() $self->test_name
Truth table0%0/3
A || B
A= shift()B= $self->test_name
ABresult
1X1
011
000
229  
230 return $self->error_msg('no_plan')
231 0 ▶ unless $self->{ plan };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"plan"} 0 0 0
232  
233 0 $self->{ tested }++;
234 0 $self->{ skipped }++;
235 0 return $self->test_msg( skip_one => $self->{ count }++, $msg );
236 }
237  
238 0 ▶sub skip_some {
Subroutine skip_some: not called CC=3 SCAR=24.8
Pod documented
239 0 my ($self, $n, $msg) = @_;
240 0 $n = int $n;
241 0 ▶ return unless $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n > 0 0 0 0
242 0 ▶ while ($n--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n--) 0 0 0
243 0 $self->skip($msg);
244 }
245 }
246  
247 0 ▶sub skip_rest {
Subroutine skip_rest: not called CC=2 SCAR=17.9
Pod documented
248 0 my $self = shift->prototype;
249 0 my $msg = shift;
250 0 my $plan = $self->{ plan };
251 0 ▶ while ($self->{ tested } < $plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tested"} < $plan) 0 0 0
252 0 $self->skip($msg);
253 }
254 0 exit;
255 }
256  
257 4 ▶sub skip_all ($;$) {
Subroutine skip_all: called CC=2 SCAR=7.6
Pod documented
258 4 my $self = shift->prototype;
259 4 ▶ $self->test_msg( skip_all => shift || $self->{ reason } );
Condition33%1/3
shift() || $self->{'reason'}
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() || $self->{'reason'}
shift() $self->{'reason'}
Truth table33%1/3
A || B
A= shift()B= $self->{'reason'}
ABresult
1X1
011
000
260 4 exit;
261 }
262  
263 1836 ▶sub result {
Subroutine result: called CC=3 SCAR=12.0
Pod documented
264 1836 my $self = shift->prototype;
265 1836 my $ok = shift;
266  
267 return $self->error_msg('no_plan')
268 1836 ▶ unless $self->{ plan };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"plan"} 0 1836 1836
269
270 1836 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 1836 0 1836
271 1836 $self->{ passed }++;
272 1836 return $self->test_msg( ok => @_ );
273 }
274 else {
275 0 $self->{ failed }++;
276 0 return $self->test_msg( not_ok => @_ );
277 }
278 }
279  
280 1905 ▶sub test_msg {
Subroutine test_msg: called CC=1 SCAR=0.0
Pod documented
281 1905 my $self = shift;
282 1905 print $self->message(@_);
283 }
284  
285 1 ▶sub test_name ($) {
Subroutine test_name: called CC=1 SCAR=0.0
Pod documented
286 1 my $self = shift->prototype;
287 1 my ($pkg, $file, $line) = caller(2);
288 1 $self->message( name => $self->{ count }, $file, $line );
289 }
290  
291 0 ▶sub different {
Subroutine different: not called CC=5 SCAR=34.0
Pod documented
292 0 my ($self, $expect, $result) = @_;
293 0 my ($pad_exp, $pad_res) = ($expect, $result);
294 0 for ($pad_exp, $pad_res) {
295 0 s/\n/\n# |/g;
296 }
297 0 my $msg = $self->message( not_eq => $pad_exp, $pad_res );
298  
299 0 ▶ return $msg
Branch: 1 branch
BranchTrueFalseTotal
unless $Badger::Test::Manager::CAN_DIFF 0 0 0
300 unless $CAN_DIFF;
301  
302 0 my $diffs = diff( map { [ split(/\n/) ] } $expect, $result );
303 0 my $n = 0;
304 0 my $m = scalar @$diffs;
305
306 0 foreach my $hunk (@$diffs) {
307 0 $msg .= $self->message( hunk => ++$n, $m );
308 0 foreach my $delta (@$hunk) {
309 0 $msg .= $self->message( delta => @$delta );
310 }
311 # $msg .= "\n";
312 }
313 0 return $msg;
314 }
315  
316 0 ▶sub colour {
Subroutine colour: not called CC=6 SCAR=37.4
Pod documented
317 0 my $self = shift->prototype;
318 0 my $ansi = ANSI_colours;
319  
320 # enable colour mode by inserting ANSI escapes into $MESSAGES
321 0 ▶ if (@_ && ($self->{ colour } = shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $self->{"colour"} = shift()) 0 0 0
Condition0%0/3
@_ and $self->{"colour"} = shift()
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $self->{"colour"} = shift()
@_ $self->{"colour"} = shift()
Truth table0%0/3
A and B
A= @_B= $self->{"colour"} = shift()
ABresult
0X0
100
111
322 0 foreach my $col (keys %$SCHEME) {
323 0 ▶ my $code = $ansi->{ $col }
Condition0%0/3
$ansi->{$col} || $self->error("Invalid colour name in \$SCHEME: $col\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
$ansi->{$col} || $self->error("Invalid colour name in \$SCHEME: $col\n")
$ansi->{$col} $self->error("Invalid colour name in \$SCHEME: $col\n")
Truth table0%0/3
A || B
A= $ansi->{$col}B= $self->error("Invalid colour name in \$SCHEME: $col\n")
ABresult
1X1
011
000
324 || $self->error("Invalid colour name in \$SCHEME: $col\n");
325 $MESSAGES->{ $_ } = ANSI_escape($code, $MESSAGES->{ $_ })
326 0 for split(/\s+/, $SCHEME->{ $col });
327 }
328 0 Badger::Debug->enable_colour;
329 }
330  
331 0 return $self->{ colour };
332 }
333  
334 69 ▶sub flush {
Subroutine flush: called CC=5 SCAR=26.2
Pod documented
335 69 my $self = shift->prototype;
336 69 ▶ my $results = shift || $self->{ results };
Condition33%1/3
shift() || $self->{'results'}
l!l&&r!l&&!r
0690
MC/DC0%0/2
shift() || $self->{'results'}
shift() $self->{'results'}
Truth table33%1/3
A || B
A= shift()B= $self->{'results'}
ABresult
1X1
011
000
337 69 ▶ return unless @$results;
Branch: 1 branch
BranchTrueFalseTotal
unless @$results 69 0 69
338 0 ▶ $self->{ plan } ||= @$results;
Condition0%0/3
$self->{"plan"} ||= @$results
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"plan"} ||= @$results
$self->{"plan"} @$results
Truth table0%0/3
A ||= B
A= $self->{"plan"}B= @$results
ABresult
1X1
011
000
339 0 ▶ while (@$results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$results) 0 0 0
340 0 my $test = shift @$results;
341 0 $self->result(@$test);
342 }
343 }
344  
345 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
346 0 my $self = shift->prototype;
347 return @_
348 ? ($self->{ summary } = shift)
349 0 ▶ : $self->{ summary };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
350 }
351  
352 69 ▶sub finish {
Subroutine finish: called CC=7 SCAR=28.4
Pod documented
353 69 my $self = shift->prototype;
354  
355 69 $self->flush; # output any cached results
356  
357 my ($plan, $ran, $pass, $fail, $skip)
358 69 = @$self{ qw( plan tested passed failed skipped ) };
359
360 69 ▶ return unless $plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $plan 4 65 69
361  
362 # mandatory warnings about too many/too few
363 65 ▶ if ($ran < $plan) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ran < $plan) { } 0 65 65
elsif ($ran > $plan) { } 0 65 65
364 0 $self->test_msg( too_few => $plan, $ran );
365 }
366 elsif ($ran > $plan) {
367 0 $self->test_msg( too_many => $plan, $ran );
368 }
369  
370 # optional summary follows for those who want it
371 65 ▶ return unless $self->{ summary };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"summary"} 65 0 65
372
373 0 my $good = $pass + $skip;
374
375 0 ▶ if ($fail) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fail) { } 0 0 0
elsif ($good == $plan) { } 0 0 0
376 0 $self->test_msg( fail => $fail );
377 }
378 elsif ($good == $plan) {
379 0 $self->test_msg( pass => $plan );
380 }
381 else {
382 0 $self->test_msg('mess');
383 }
384 0 $self->test_msg( summary => $ran, $plan, $pass, $fail, $skip );
385  
386 # remove ourselves from the index
387 0 delete $INSTANCES->{ $self };
388
389 }
390  
391 70 ▶END {
Subroutine END: called CC=2 SCAR=6.9
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 70 $_->finish for values %$INSTANCES;
397 }
398  
399 70 1;
400  
401 __END__
« lib/Badger/Filesystem/Virtual.pm ↑ Summary lib/Badger/Timestamp.pm »