File Coverage

inc/Test/More.pm
Criterion Covered Total %
statement 58 263 22.0
branch 6 136 4.4
condition 1 33 3.0
subroutine 13 36 36.1
pod 20 21 95.2
total 98 489 20.0


line stmt bran cond sub pod time code
1             #line 1
2             package Test::More;
3 5     5   2844  
  5         14  
  5         169  
4             use 5.004;
5 5     5   25  
  5         9  
  5         169  
6 5     5   3118 use strict;
  5         15  
  5         519  
7             use Test::Builder;
8              
9              
10             # Can't use Carp because it might cause use_ok() to accidentally succeed
11             # even though the module being used forgot to use Carp. Yes, this
12             # actually happened.
13 0     0   0 sub _carp {
14 0         0 my($file, $line) = (caller(1))[1,2];
15             warn @_, " at $file line $line\n";
16             }
17              
18              
19              
20 5     5   33 require Exporter;
  5         8  
  5         9451  
21             use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO);
22             $VERSION = '0.54';
23             $VERSION = eval $VERSION; # make the alpha version come out as a number
24              
25             @ISA = qw(Exporter);
26             @EXPORT = qw(ok use_ok require_ok
27             is isnt like unlike is_deeply
28             cmp_ok
29             skip todo todo_skip
30             pass fail
31             eq_array eq_hash eq_set
32             $TODO
33             plan
34             can_ok isa_ok
35             diag
36             );
37              
38             my $Test = Test::Builder->new;
39             my $Show_Diag = 1;
40              
41              
42             # 5.004's Exporter doesn't have export_to_level.
43             sub _export_to_level
44 5     5   15 {
45 5         8 my $pkg = shift;
46 5         7 my $level = shift;
47 5         8 (undef) = shift; # redundant arg
48 5         4456 my $callpkg = caller($level);
49             $pkg->export($callpkg, @_);
50             }
51              
52              
53             #line 177
54              
55             sub plan {
56             my(@plan) = @_;
57              
58             my $idx = 0;
59             my @cleaned_plan;
60             while( $idx <= $#plan ) {
61             my $item = $plan[$idx];
62              
63             if( $item eq 'no_diag' ) {
64             $Show_Diag = 0;
65             }
66             else {
67             push @cleaned_plan, $item;
68             }
69              
70             $idx++;
71             }
72              
73             $Test->plan(@cleaned_plan);
74             }
75              
76             sub import {
77             my($class) = shift;
78              
79             my $caller = caller;
80              
81             $Test->exported_to($caller);
82              
83             my $idx = 0;
84             my @plan;
85             my @imports;
86             while( $idx <= $#_ ) {
87             my $item = $_[$idx];
88              
89             if( $item eq 'import' ) {
90             push @imports, @{$_[$idx+1]};
91             $idx++;
92             }
93             else {
94             push @plan, $item;
95             }
96              
97             $idx++;
98             }
99              
100             plan(@plan);
101              
102             __PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);
103             }
104              
105              
106             #line 295
107              
108             sub ok ($;$) {
109             my($test, $name) = @_;
110             $Test->ok($test, $name);
111             }
112              
113             #line 359
114              
115             sub is ($$;$) {
116             $Test->is_eq(@_);
117             }
118              
119             sub isnt ($$;$) {
120             $Test->isnt_eq(@_);
121             }
122              
123             *isn't = \&isnt;
124              
125              
126             #line 400
127              
128             sub like ($$;$) {
129             $Test->like(@_);
130             }
131              
132              
133             #line 414
134              
135             sub unlike ($$;$) {
136             $Test->unlike(@_);
137             }
138              
139              
140             #line 452
141              
142             sub cmp_ok($$$;$) {
143             $Test->cmp_ok(@_);
144             }
145              
146              
147             #line 486
148              
149             sub can_ok ($@) {
150             my($proto, @methods) = @_;
151             my $class = ref $proto || $proto;
152              
153             unless( @methods ) {
154             my $ok = $Test->ok( 0, "$class->can(...)" );
155             $Test->diag(' can_ok() called with no methods');
156             return $ok;
157             }
158              
159             my @nok = ();
160             foreach my $method (@methods) {
161             local($!, $@); # don't interfere with caller's $@
162             # eval sometimes resets $!
163             eval { $proto->can($method) } || push @nok, $method;
164             }
165              
166             my $name;
167             $name = @methods == 1 ? "$class->can('$methods[0]')"
168             : "$class->can(...)";
169            
170             my $ok = $Test->ok( !@nok, $name );
171              
172             $Test->diag(map " $class->can('$_') failed\n", @nok);
173              
174             return $ok;
175             }
176              
177             #line 543
178              
179 7     7 1 27 sub isa_ok ($$;$) {
180             my($object, $class, $obj_name) = @_;
181 7         13  
182 7         10 my $diag;
183 7         25 $obj_name = 'The object' unless defined $obj_name;
184 10         20 my $name = "$obj_name isa $class";
185             if( !defined $object ) {
186 10 50       23 $diag = "$obj_name isn't defined";
187 0         0 }
188             elsif( !ref $object ) {
189             $diag = "$obj_name isn't a reference";
190 10         14 }
191             else {
192             # We can't use UNIVERSAL::isa because we want to honor isa() overrides
193 10         25 local($@, $!); # eval sometimes resets $!
194             my $rslt = eval { $object->isa($class) };
195             if( $@ ) {
196 7         30 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
197             if( !UNIVERSAL::isa($object, $class) ) {
198             my $ref = ref $object;
199             $diag = "$obj_name isn't a '$class' it's a '$ref'";
200 5     5   46 }
201             } else {
202 5         24 die <
203             WHOA! I tried to call ->isa on your object and got some weird error.
204 5         24 This should never happen. Please contact the author immediately.
205             Here's the error.
206 5         6 $@
207 5         8 WHOA
208             }
209 5         23 }
210 6         11 elsif( !$rslt ) {
211             my $ref = ref $object;
212 6 50       17 $diag = "$obj_name isn't a '$class' it's a '$ref'";
213 0         0 }
  0         0  
214 0         0 }
215            
216            
217 6         9  
218             my $ok;
219             if( $diag ) {
220 6         17 $ok = $Test->ok( 0, $name );
221             $Test->diag(" $diag\n");
222             }
223 5         15 else {
224             $ok = $Test->ok( 1, $name );
225 5         16 }
226              
227             return $ok;
228             }
229              
230              
231             #line 612
232              
233             sub pass (;$) {
234             $Test->ok(1, @_);
235             }
236              
237             sub fail (;$) {
238             $Test->ok(0, @_);
239             }
240              
241             #line 665
242              
243             sub diag {
244             return unless $Show_Diag;
245             $Test->diag(@_);
246             }
247              
248              
249             #line 721
250              
251             sub use_ok ($;@) {
252             my($module, @imports) = @_;
253             @imports = () unless @imports;
254              
255             my($pack,$filename,$line) = caller;
256              
257             local($@,$!); # eval sometimes interferes with $!
258              
259             if( @imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/ ) {
260             # probably a version check. Perl needs to see the bare number
261             # for it to work with non-Exporter based modules.
262             eval <
263             package $pack;
264             use $module $imports[0];
265             USE
266             }
267             else {
268             eval <
269             package $pack;
270             use $module \@imports;
271             USE
272             }
273              
274             my $ok = $Test->ok( !$@, "use $module;" );
275              
276             unless( $ok ) {
277             chomp $@;
278             $@ =~ s{^BEGIN failed--compilation aborted at .*$}
279             {BEGIN failed--compilation aborted at $filename line $line.}m;
280             $Test->diag(<
281             Tried to use '$module'.
282             Error: $@
283             DIAGNOSTIC
284              
285             }
286              
287             return $ok;
288             }
289              
290             #line 769
291              
292             sub require_ok ($) {
293             my($module) = shift;
294              
295             my $pack = caller;
296              
297 6     6 1 5515 # Try to deterine if we've been given a module name or file.
298 6         30 # Module names must be barewords, files not.
299             $module = qq['$module'] unless _is_module_name($module);
300              
301             local($!, $@); # eval sometimes interferes with $!
302             eval <
303             package $pack;
304             require $module;
305             REQUIRE
306              
307             my $ok = $Test->ok( !$@, "require $module;" );
308              
309             unless( $ok ) {
310             chomp $@;
311             $Test->diag(<
312             Tried to require '$module'.
313             Error: $@
314             DIAGNOSTIC
315              
316             }
317              
318             return $ok;
319             }
320              
321              
322             sub _is_module_name {
323             my $module = shift;
324              
325             # Module names start with a letter.
326             # End with an alphanumeric.
327             # The rest is an alphanumeric or ::
328             $module =~ s/\b::\b//g;
329             $module =~ /^[a-zA-Z]\w+$/;
330             }
331              
332             #line 870
333              
334             #'#
335             sub skip {
336             my($why, $how_many) = @_;
337              
338             unless( defined $how_many ) {
339             # $how_many can only be avoided when no_plan is in use.
340             _carp "skip() needs to know \$how_many tests are in the block"
341             unless $Test->has_plan eq 'no_plan';
342             $how_many = 1;
343             }
344              
345             for( 1..$how_many ) {
346             $Test->skip($why);
347             }
348              
349             local $^W = 0;
350             last SKIP;
351             }
352              
353              
354             #line 951
355              
356             sub todo_skip {
357             my($why, $how_many) = @_;
358              
359             unless( defined $how_many ) {
360             # $how_many can only be avoided when no_plan is in use.
361 10     10 1 437 _carp "todo_skip() needs to know \$how_many tests are in the block"
362             unless $Test->has_plan eq 'no_plan';
363             $how_many = 1;
364             }
365 0     0 1 0  
366             for( 1..$how_many ) {
367             $Test->todo_skip($why);
368             }
369              
370             local $^W = 0;
371             last TODO;
372             }
373              
374             #line 1007
375              
376             use vars qw(@Data_Stack %Refs_Seen);
377             my $DNE = bless [], 'Does::Not::Exist';
378             sub is_deeply {
379             unless( @_ == 2 or @_ == 3 ) {
380             my $msg = <
381             is_deeply() takes two or three args, you gave %d.
382             This usually means you passed an array or hash instead
383             of a reference to it
384             WARNING
385             chop $msg; # clip off newline so carp() will put in line/file
386              
387             _carp sprintf $msg, scalar @_;
388             }
389              
390             my($this, $that, $name) = @_;
391              
392             my $ok;
393             if( !ref $this xor !ref $that ) { # one's a reference, one isn't
394             $ok = 0;
395             }
396             if( !ref $this and !ref $that ) {
397             $ok = $Test->is_eq($this, $that, $name);
398             }
399             else {
400             local @Data_Stack = ();
401             local %Refs_Seen = ();
402 0     0 1 0 if( _deep_check($this, $that) ) {
403             $ok = $Test->ok(1, $name);
404             }
405             else {
406             $ok = $Test->ok(0, $name);
407             $ok = $Test->diag(_format_stack(@Data_Stack));
408             }
409             }
410              
411             return $ok;
412             }
413              
414             sub _format_stack {
415             my(@Stack) = @_;
416 0     0 1 0  
417             my $var = '$FOO';
418             my $did_arrow = 0;
419             foreach my $entry (@Stack) {
420             my $type = $entry->{type} || '';
421             my $idx = $entry->{'idx'};
422             if( $type eq 'HASH' ) {
423             $var .= "->" unless $did_arrow++;
424             $var .= "{$idx}";
425             }
426             elsif( $type eq 'ARRAY' ) {
427             $var .= "->" unless $did_arrow++;
428             $var .= "[$idx]";
429             }
430             elsif( $type eq 'REF' ) {
431             $var = "\${$var}";
432             }
433             }
434              
435             my @vals = @{$Stack[-1]{vals}}[0,1];
436             my @vars = ();
437             ($vars[0] = $var) =~ s/\$FOO/ \$got/;
438             ($vars[1] = $var) =~ s/\$FOO/\$expected/;
439              
440             my $out = "Structures begin differing at:\n";
441             foreach my $idx (0..$#vals) {
442             my $val = $vals[$idx];
443             $vals[$idx] = !defined $val ? 'undef' :
444             $val eq $DNE ? "Does not exist"
445             : "'$val'";
446             }
447              
448             $out .= "$vars[0] = $vals[0]\n";
449             $out .= "$vars[1] = $vals[1]\n";
450              
451             $out =~ s/^/ /msg;
452             return $out;
453             }
454 0     0 1 0  
455              
456             sub _type {
457             my $thing = shift;
458              
459             return '' if !ref $thing;
460              
461             for my $type (qw(ARRAY HASH REF SCALAR GLOB Regexp)) {
462             return $type if UNIVERSAL::isa($thing, $type);
463             }
464              
465             return '';
466             }
467              
468              
469             #line 1109
470              
471             #'#
472             sub eq_array {
473             local @Data_Stack;
474             local %Refs_Seen;
475             _eq_array(@_);
476             }
477              
478             sub _eq_array {
479             my($a1, $a2) = @_;
480              
481             if( grep !_type($_) eq 'ARRAY', $a1, $a2 ) {
482             warn "eq_array passed a non-array ref";
483             return 0;
484             }
485              
486             return 1 if $a1 eq $a2;
487              
488 0     0 1 0 if($Refs_Seen{$a1}) {
489 0   0     0 return $Refs_Seen{$a1} eq $a2;
490             }
491 0 0       0 else {
492 0         0 $Refs_Seen{$a1} = "$a2";
493 0         0 }
494 0         0  
495             my $ok = 1;
496             my $max = $#$a1 > $#$a2 ? $#$a1 : $#$a2;
497 0         0 for (0..$max) {
498 0         0 my $e1 = $_ > $#$a1 ? $DNE : $a1->[$_];
499 0         0 my $e2 = $_ > $#$a2 ? $DNE : $a2->[$_];
500              
501 0 0       0 push @Data_Stack, { type => 'ARRAY', idx => $_, vals => [$e1, $e2] };
  0         0  
502             $ok = _deep_check($e1,$e2);
503             pop @Data_Stack if $ok;
504 0         0  
505 0 0       0 last unless $ok;
506             }
507              
508 0         0 return $ok;
509             }
510 0         0  
511             sub _deep_check {
512 0         0 my($e1, $e2) = @_;
513             my $ok = 0;
514              
515             {
516             # Quiet uninitialized value warnings when comparing undefs.
517             local $^W = 0;
518              
519             $Test->_unoverload(\$e1, \$e2);
520              
521             # Either they're both references or both not.
522             my $same_ref = !(!ref $e1 xor !ref $e2);
523              
524             if( defined $e1 xor defined $e2 ) {
525             $ok = 0;
526             }
527             elsif ( $e1 == $DNE xor $e2 == $DNE ) {
528             $ok = 0;
529             }
530             elsif ( $same_ref and ($e1 eq $e2) ) {
531             $ok = 1;
532             }
533             else {
534             my $type = _type($e1);
535             $type = '' unless _type($e2) eq $type;
536              
537             if( !$type ) {
538             push @Data_Stack, { vals => [$e1, $e2] };
539             $ok = 0;
540             }
541             elsif( $type eq 'ARRAY' ) {
542             $ok = _eq_array($e1, $e2);
543             }
544             elsif( $type eq 'HASH' ) {
545 0     0 1 0 $ok = _eq_hash($e1, $e2);
546             }
547 0         0 elsif( $type eq 'REF' ) {
548 0 0       0 push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
549 0         0 $ok = _deep_check($$e1, $$e2);
550 0 0       0 pop @Data_Stack if $ok;
    0          
551 0         0 }
552             elsif( $type eq 'SCALAR' ) {
553             push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
554 0         0 $ok = _deep_check($$e1, $$e2);
555             pop @Data_Stack if $ok;
556             }
557             }
558 0         0 }
559 0         0  
  0         0  
560 0 0       0 return $ok;
    0          
561 0 0       0 }
562 0 0       0  
563 0         0  
564 0         0 #line 1211
565              
566             sub eq_hash {
567 0         0 local @Data_Stack;
568             local %Refs_Seen;
569             return _eq_hash(@_);
570             }
571              
572             sub _eq_hash {
573             my($a1, $a2) = @_;
574              
575             if( grep !_type($_) eq 'HASH', $a1, $a2 ) {
576 0         0 warn "eq_hash passed a non-hash ref";
577 0         0 return 0;
578             }
579              
580             return 1 if $a1 eq $a2;
581              
582             if( $Refs_Seen{$a1} ) {
583 0         0 return $Refs_Seen{$a1} eq $a2;
584 0 0       0 }
585 0         0 else {
586 0         0 $Refs_Seen{$a1} = "$a2";
587             }
588              
589 0         0 my $ok = 1;
590             my $bigger = keys %$a1 > keys %$a2 ? $a1 : $a2;
591             foreach my $k (keys %$bigger) {
592 0         0 my $e1 = exists $a1->{$k} ? $a1->{$k} : $DNE;
593             my $e2 = exists $a2->{$k} ? $a2->{$k} : $DNE;
594              
595             push @Data_Stack, { type => 'HASH', idx => $k, vals => [$e1, $e2] };
596             $ok = _deep_check($e1, $e2);
597             pop @Data_Stack if $ok;
598              
599             last unless $ok;
600             }
601              
602             return $ok;
603             }
604              
605             #line 1263
606              
607             sub eq_set {
608             my($a1, $a2) = @_;
609             return 0 unless @$a1 == @$a2;
610              
611             # There's faster ways to do this, but this is easiest.
612             local $^W = 0;
613              
614 0     0 1 0 # We must make sure that references are treated neutrally. It really
615             # doesn't matter how we sort them, as long as both arrays are sorted
616             # with the same algorithm.
617             # Have to inline the sort routine due to a threading/sort bug.
618 0     0 1 0 # See [rt.cpan.org 6782]
619             return eq_array(
620             [sort { ref $a ? -1 : ref $b ? 1 : $a cmp $b } @$a1],
621             [sort { ref $a ? -1 : ref $b ? 1 : $a cmp $b } @$a2]
622             );
623             }
624              
625             #line 1306
626              
627             sub builder {
628             return Test::Builder->new;
629             }
630              
631             #line 1446
632              
633             1;