Test.pm

filter: Statementstmt 14.2% 33 / 231 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 35.0% 7 / 20 Podpod 11.1% 1 / 9 totaltotal 11.0% 41 / 371 CC 59 SCAR 56.5
CC
59
Cov
10%
test
756.0
get_all_tests
110.0
extract_tests
56.0
CRAP 285.3
« Object/Persistent.pm ↑ Summary DB/Table.pm »
1 package Basset::Test;
2  
3 #Basset::Test, copyright and (c) 2004, 2005, 2006 James A Thomason III
4  
5 =pod
6  
7 Inline testing for Basset modules. Or anyone else that wants it, for that matter. Built off of Test::More.
8  
9 =cut
10  
11 4 $VERSION = '1.01';
12  
13 4 ▶use Basset::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 our @ISA = Basset::Object->pkg_for_type('object');
15  
16 4 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 __PACKAGE__->add_class_attr('singleton');
22  
23 =pod
24  
25 =head1 METHODS
26  
27 =over
28  
29 =item test
30  
31 ->test is the bread and butter of Basset testing. Takes one, or optionally two arguments.
32 The first argument is always the class to test. The second argument is an optional boolean flag.
33 If true, then the test will also test all super classes B<in the current package>.
34  
35 For more information on tests, read up on Test::More and Test::Builder.
36  
37 To embed a test:
38  
39 =pod
40
41 =begin btest(NAMEOFTEST)
42
43 $test->ok('true value', "Testing for true value");
44 $test->is(1, 1, "1 = 1");
45 #etc.
46
47 =end btest(NAMEOFTEST)
48
49 =cut
50  
51 Note that you must specify the same NAMEOFTEST on both the begin and end lines (otherwise it's a pod formatting bug), and
52 it cannot contain any spaces.
53  
54 If you append "__only" to the name of the test, then it will only be run within the test that contains it, never in a superclass.
55 For example, this is useful if you have a dummy method in a super class that always dies with the intention of having the subclass
56 populate it. You never want to run the superclass's tests from a subclass.
57  
58 You do have access to Test::More style SKIP: blocks and the $TODO variable (declare it as our $TODO up at the
59 top if you use todos).
60  
61 =end btest
62  
63 =cut
64  
65 4 __PACKAGE__->add_class_attr('_plan', 0);
66  
67 0 ▶sub plan {
Subroutine plan: not called CC=2 SCAR=17.9
Pod undocumented
68 0 my $self = shift;
69 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
70 0 $self->_plan(shift);
71 };
72 0 return $self->_plan;
73 }
74  
75 4 __PACKAGE__->add_class_attr('_output', []);
76  
77 4 __PACKAGE__->add_class_attr('silently');
78  
79 0 ▶sub test {
Subroutine test: not called CC=27 SCAR=66.3
Pod documented
80  
81 0 my $self = shift;
82 0 ▶ my $class = shift or return $self->error("Cannot test w/o class", "XXX");
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or return $self->error("Cannot test w/o class", "XXX") 0 0 0
83 0 ▶ my $superclasses = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
84
85 0 $self->singleton(Test::Builder->new);
86
87 0 $self->_output([]);
88  
89 0 $self->singleton->no_header(1);
90 0 $self->singleton->no_ending(1);
91
92 0 ▶ my @t = $self->get_all_tests($class, $superclasses) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@t) = $self->get_all_tests($class, $superclasses) or return 0 0 0
93  
94 0 my $tested = {};
95  
96 0 $self->announce('1..' . $self->plan . "\n");
97  
98 0 ▶ while (@t) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 0 0 0
99 0 my $n = shift @t;
100 0 ▶ my $t = '{' . (shift(@t) || '') . '};';
Condition0%0/2
shift @t || ''
l!l
00
MC/DC0%0/1
shift @t || ''
shift @t
Truth table0%0/2
A
A= shift @t
Aresult
11
00
101 0 $t = "package $class;\n$t";
102 0 $t =~ s/\$test\b/$self/g;
103  
104 0 my @num = ($t =~ /$self->/g);
105 0 $t =~ s/__PACKAGE__/$class/g;
106  
107 0 ▶ next if $tested->{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $tested->{$n} 0 0 0
108  
109 0 ▶ if ($n =~ s/__only//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/__only//) 0 0 0
110 0 ▶ next if $tested->{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
if $tested->{$n}++ 0 0 0
111 };
112  
113  
114 0 my $num = @num;
115  
116 0 ▶ if ($num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num) { } 0 0 0
117 0 $self->announce("# 1..$num testing $n\n");
118 0 local $@ = undef;
119 0 eval $t;
120 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
121 0 $self->singleton->diag("failure ($@) during test suite :\n----\n$t\n----\n");
122 }
123  
124 } else {
125 0 $self->singleton->diag("no tests for $n\n");
126 };
127 };
128
129 0 my @results = $self->singleton->summary;
130 0 my ($successes, $failures) = (0,0);
131 0 foreach my $r (@results) {
132 0 ▶ $r ? $successes++ : $failures++;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
133 }
134
135 0 my $total = $successes + $failures;
136
137 0 $self->announce("# \n");
138 0 $self->announce("# " . $successes . " tests passed\n");
139 0 $self->announce("# " . $failures . " tests failed\n");
140
141 0 ▶ $self->singleton->diag("Looks like you failed $failures tests of $total")
Branch: 1 branch
BranchTrueFalseTotal
if $failures 0 0 0
142 if $failures;
143  
144 0 ▶ if ($total > $self->plan) {
Branch: 2 branches
BranchTrueFalseTotal
if ($total > $self->plan) { } 0 0 0
elsif ($total < $self->plan) { } 0 0 0
145 0 $self->singleton->diag("Looks like you planned " . $self->plan
146 . " tests but ran " . ($total - $self->plan) . " extra.");
147 }
148 elsif ($total < $self->plan) {
149 0 $self->singleton->diag("Looks like you planned " . $self->plan
150 . " tests but only ran $total.");
151 }
152  
153 0 my $laststream = undef;
154
155 0 ▶ return 1 if $self->silently;
Branch: 1 branch
BranchTrueFalseTotal
if $self->silently 0 0 0
156
157 0 ▶ if ($self->proving) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proving) 0 0 0
158 0 print <<" eTESTOUTPUT";
159 select((select(\\*STDOUT), \$| = 1)[0]);
160 select((select(\\*STDERR), \$| = 1)[0]);
161 use Test::Builder;
162 my \$builder = Test::Builder->new();
163 \$builder->plan('tests' => $total);
164
165 eTESTOUTPUT
166 }
167
168 0 my $test_idx = 0;
169
170 0 foreach my $item (@{$self->_output}) {
171 0 my ($stream, $msg) = @$item;
172 0 ▶ $test_idx++ if $msg =~ /^(not )?ok/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^(not )?ok/ 0 0 0
173 0 ▶ if (defined $laststream && $laststream eq $stream) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $laststream and $laststream eq $stream) { } 0 0 0
Condition0%0/3
defined $laststream and $laststream eq $stream
!ll&&!rl&&r
000
MC/DC0%0/2
defined $laststream and $laststream eq $stream
defined $laststream $laststream eq $stream
Truth table0%0/3
A and B
A= defined $laststreamB= $laststream eq $stream
ABresult
0X0
100
111
174 0 print $msg;
175 } else {
176 0 ▶ if ($self->proving) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proving) 0 0 0
177 0 ▶ if ($laststream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($laststream) 0 0 0
178 0 print "eTESTOUTPUT\n";
179 }
180 0 ▶ if ($stream eq 'error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream eq 'error') { } 0 0 0
181 0 print "\$builder->current_test($test_idx);\n";
182 0 print "print STDERR <<'eTESTOUTPUT';\n";
183 } else {
184 0 print "print STDOUT <<'eTESTOUTPUT';\n";
185 }
186 }
187 0 print $msg;
188 0 $laststream = $stream;
189 }
190 }
191
192 0 ▶ if ($laststream && $self->proving) {
Branch: 1 branch
BranchTrueFalseTotal
if ($laststream and $self->proving) 0 0 0
Condition0%0/3
$laststream and $self->proving
!ll&&!rl&&r
000
MC/DC0%0/2
$laststream and $self->proving
$laststream $self->proving
Truth table0%0/3
A and B
A= $laststreamB= $self->proving
ABresult
0X0
100
111
193 0 print "eTESTOUTPUT\n";
194 }
195  
196 0 return 1;
197 }
198  
199 0 ▶sub get_all_tests {
Subroutine get_all_tests: not called CC=10 SCAR=47.0
Pod undocumented
200  
201 0 my $self = shift;
202 0 my $class = shift;
203 0 ▶ my $superclasses = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
204 0 local $@ = undef;
205  
206 0 eval "use $class";
207 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
208 0 return $self->error("Catastrophe - could not use $class : $@", "BT-01");
209 };
210
211 0 ▶ $class->exceptions(0) if $class->can('exceptions');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("exceptions") 0 0 0
212
213 0 ▶ my $classes = $superclasses ? Basset::Object::isa_path($class) : [$class];
Branch: 1 branch
BranchTrueFalseTotal
$superclasses ? : 0 0 0
214  
215 0 my @modules = ();
216 0 foreach my $class (@$classes) {
217 0 local $@ = undef;
218 0 eval "use $class";
219 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
220 0 return $self->error("Catastrophe - could not use $class : $@", "BT-02");
221 };
222 0 my $module = $self->module_for_class($class);
223 0 push @modules, $module;
224 };
225  
226 0 my @t = ();
227
228 0 foreach my $module (reverse @modules) {
229 0 push @t, $self->extract_tests($INC{$module});
230 };
231
232 0 my $handleclass = 'Basset::Test::_Handle';
233
234 0 tie *OUT, $handleclass, $self, 'output';
235 0 tie *ERR, $handleclass, $self, 'error';
236 0 tie *TODO, $handleclass, $self, 'todo';
237
238 0 my $numtests = $self->count_tests(@t);
239
240 0 $self->singleton->output(\*OUT);
241 0 $self->singleton->failure_output(\*ERR);
242 0 $self->singleton->todo_output(\*TODO);
243  
244 0 $self->singleton->exported_to($self->pkg);
245  
246 0 ▶ unless ($self->plan) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->plan) 0 0 0
247 0 $self->singleton->plan('tests' => $numtests);
248 0 $self->plan($numtests);
249 }
250
251 0 ▶ return @t ? @t : ($class);
Branch: 1 branch
BranchTrueFalseTotal
@t ? : 0 0 0
252 }
253  
254  
255 0 ▶sub count_tests {
Subroutine count_tests: not called CC=5 SCAR=34.0
Pod undocumented
256 0 my $class = shift;
257 0 my @tests = @_;
258 0 my @num = ();
259
260 0 my $skips = {};
261
262 0 my $test_name = '';
263
264 0 foreach my $test (@tests) {
265  
266 0 ▶ if ($test =~ /^\s*\$test->(?!announce)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test =~ /^\s*\$test->(?!announce)/m) { } 0 0 0
267 0 ▶ next if $skips->{$test_name};
Branch: 1 branch
BranchTrueFalseTotal
if $skips->{$test_name} 0 0 0
268 0 push @num, $test =~ /^\s*\$test->(?!announce)/gm;
269
270 0 ▶ if ($test_name =~ s/__only$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_name =~ s/__only$//) 0 0 0
271 0 $skips->{$test_name}++;
272 }
273 } else {
274 0 $test_name = $test;
275  
276 }
277
278 }
279  
280 0 return scalar @num;
281 }
282  
283 0 ▶sub extract_tests {
Subroutine extract_tests: not called CC=7 SCAR=40.3
Pod undocumented
284 0 my $self = shift;
285 0 my $class = $self->pkg;
286 0 my $file = shift;
287
288 0 my $data = undef;
289
290 0 open (my $fh, $file);
291 {
292 0 local $/ = undef;
293 0 $data = <$fh>;
294 };
295 0 close $file;
296
297 0 $self->line_counter(1);
298  
299 0 ▶ if ($data =~ /^\s*\$test->plan\(([^)]+)\);\s*\n/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\s*\$test->plan\(([^)]+)\);\s*\n/m) { } 0 0 0
300 0 my @plan = ();
301 0 my $plan = $1;
302 0 ▶ if ($1 =~ /,/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 =~ /,/) { } 0 0 0
elsif ($1 =~ /=>/) { } 0 0 0
303 0 @plan = split(/,/, $plan);
304 } elsif ($1 =~ /=>/) {
305 0 @plan = split(/\s*=>\s*/, $plan);
306 } else {
307 0 @plan = ($plan);
308 }
309 0 ▶ $plan[0] =~ s/['"\s]//g if defined $plan[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $plan[0] 0 0 0
310 0 ▶ $plan[1] =~ s/['"\s]//g if defined $plan[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $plan[1] 0 0 0
311  
312 0 $plan[1] += 2;
313  
314 0 $self->singleton->plan(@plan);
315 0 $self->plan($plan[1]);
316  
317 } else {
318 0 $self->plan(0);
319 }
320  
321 0 my @tests = ();
322 0 ▶ if ($data =~ /^\s*=begin btest\(/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\s*=begin btest\(/m) { } 0 0 0
323 0 $data =~ s/(\n|^\s*=begin btest\(([^)]+)\)\s*\n)/$self->numberer($1, $2)/gem;
324 0 @tests = $data =~ /^\s*=begin btest\(([^)]+)\)\s*\n(.+?)^\s*=end btest\(\1\)\s*\n/sgm;
325 } else {
326 0 $data =~ s/(\n|^\s*=begin btest( +([^\n]+))?\n)/$self->numberer($1, $2)/gem;
327 0 @tests = $data =~ /^\s*=begin btest( +[^\n]+)?\n(.+?)^\s*=end btest\s*\n/sgm;
328 }
329  
330 0 $self->test_for_strict($class, $file, \$data, \@tests);
331  
332 0 return @tests;
333 };
334  
335 4 __PACKAGE__->add_class_attr('line_counter');
336 4 __PACKAGE__->add_class_attr('proving');
337  
338 0 ▶sub numberer {
Subroutine numberer: not called CC=3 SCAR=24.8
Pod undocumented
339 0 my $self = shift;
340 0 my $val = shift;
341  
342 0 $self->line_counter($self->line_counter + 1);
343 0 ▶ if ($val ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne "\n") 0 0 0
344 0 ▶ my $name = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
345 0 $val .= "\n#line " . $self->line_counter . " $name\n";
346 }
347 0 return $val;
348 };
349  
350 0 ▶sub test_for_strict {
Subroutine test_for_strict: not called CC=3 SCAR=24.8
Pod undocumented
351 0 my $self = shift;
352 0 my $class = shift;
353 0 my $file = shift;
354 0 my $data = shift;
355 0 my $tests = shift;
356
357 0 ▶ my $uses_strict = $$data =~ /^\s*use\s*strict\s*;/m ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$data =~ /^\s*use\s*strict\s*;/m ? : 0 0 0
358 0 ▶ my $uses_warnings = $$data =~ /^\s*use\s*warnings\s*;/m ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$data =~ /^\s*use\s*warnings\s*;/m ? : 0 0 0
359
360 0 unshift @$tests, ("strict and warnings checks", <<" eoT");
361 \$test->ok($uses_strict, "uses strict");
362 \$test->ok($uses_warnings, "uses warnings");
363 eoT
364
365 0 return 1;
366 }
367  
368 0 ▶sub skip {
Subroutine skip: not called CC=4 SCAR=24.2
Pod undocumented
369 0 my $self = shift;
370 0 ▶ my $reason = shift or return $self->error("Cannot skip w/o reason", "XXX");
Branch: 1 branch
BranchTrueFalseTotal
my $reason = shift() or return $self->error("Cannot skip w/o reason", "XXX") 0 0 0
371 0 ▶ my $num = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
372
373 0 foreach (1..$num) {
374 0 $self->singleton->skip($reason);
375 }
376
377 #cheat and bail out of the loop.
378 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
379
380 0 last SKIP;
381 }
382  
383 4 __PACKAGE__->add_attr('todo');
384  
385 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=22.0
386 0 my $self = shift;
387 0 (my $method = $Basset::Test::AUTOLOAD) =~ s/^(.+):://;
388
389 0 my $method_map = {
390 $method => $method,
391 'is' => 'is_eq',
392 'isnt' => 'isnt_eq',
393 };
394
395 0 my $imethod = $method_map->{$method};
396
397 0 ▶ if ($method ne 'DESTROY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "DESTROY") 0 0 0
398  
399 0 ▶ if (defined $self->singleton) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->singleton) { } 0 0 0
400 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
401 0 my $pkg = $self->pkg;
402
403 0 ▶ *{$pkg . "::$method"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
404 0 my $self = shift;
405 0 ▶ if (my $singleton = $self->singleton) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $singleton = $self->singleton) { } 0 0 0
406 0 $self->singleton->$imethod(@_);
407 } else {
408 0 return $self->error("Cannot call method ($method) : no singleton", "XXX");
409 }
410 0 };
411  
412 0 return $self->$method(@_);
413 } else {
414 0 return $self->error("Cannot do anything without singleton", "XXX");
415 };
416 }
417 };
418  
419 0 ▶sub announce {
Subroutine announce: not called CC=2 SCAR=17.9
Pod undocumented
420 0 my $self = shift;
421 0 my @msgs = @_;
422
423 0 foreach my $msg (@msgs) {
424 0 push @{$self->_output}, ['output', $msg];
425 };
426 }
427  
428 4 1;
429  
430 package Basset::Test::_Handle;
431  
432 4 ▶use Basset::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
433 4 our @ISA = qw(Basset::Test);
434  
435 0 ▶sub PRINT {
Subroutine PRINT: not called CC=2 SCAR=17.9
436 0 my $self = shift;
437 0 my ($test, $stream) = @$self;
438 0 my @args = @_;
439  
440 0 foreach my $arg (@args) {
441 0 push @{$test->_output}, [$stream, $arg];
442 };
443 }
444  
445 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
446 0 my $class = shift;
447 0 my $test = shift;
448 0 my $stream = shift;
449  
450 0 return bless [$test, $stream], $class;
451 }
452  
453 4 1;
« Object/Persistent.pm ↑ Summary DB/Table.pm »