File Coverage

blib/lib/Test/Stream/DeferredTests.pm
Criterion Covered Total %
statement 38 38 100.0
branch 9 10 90.0
condition 4 4 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 63 64 98.4


line stmt bran cond sub pod time code
1             package Test::Stream::DeferredTests;
2 12     12   2264 use strict;
  12         14  
  12         261  
3 12     12   33 use warnings;
  12         14  
  12         253  
4              
5 12     12   35 use Carp qw/croak/;
  12         14  
  12         455  
6 12     12   547 use Test::Stream::Util qw/get_tid/;
  12         12  
  12         51  
7              
8 12     12   49 use Test::Stream::Exporter qw/import default_exports/;
  12         22  
  12         52  
9             default_exports qw/def do_def/;
10 12     12   40 no Test::Stream::Exporter;
  12         19  
  12         113  
11              
12             my %TODO;
13              
14             sub def {
15 376     376 1 895 my ($func, @args) = @_;
16              
17 376         1747 my @caller = caller(0);
18              
19 376   100     798 $TODO{$caller[0]} ||= [];
20 376         277 push @{$TODO{$caller[0]}} => [$func, \@args, \@caller];
  376         8204  
21             }
22              
23             sub do_def {
24 14     14 1 118 my $for = caller;
25 14 100       179 my $tests = delete $TODO{$for} or croak "No tests to run!";
26              
27 13         39 for my $test (@$tests) {
28 259         2509 my ($func, $args, $caller) = @$test;
29              
30 259         405 my ($pkg, $file, $line) = @$caller;
31              
32             # Note: The '&' below is to bypass the prototype, which is important here.
33 259 100       11182 eval <<" EOT" or die $@;
34             package $pkg;
35             # line $line "(eval in DeferredTests) $file"
36             \&$func(\@\$args);
37             1;
38             EOT
39             }
40             }
41              
42             sub _verify {
43 14 100   14   167 return if Test::Stream::Sync->pid != $$;
44 10 50       34 return if Test::Stream::Sync->tid != get_tid();
45 10         14 my $not_ok = 0;
46 10         55 for my $pkg (keys %TODO) {
47 3         9 delete $TODO{$pkg};
48 3 100       9 print STDOUT "not ok - deferred tests were not run!\n" unless $not_ok++;
49 3         6 print STDERR "# '$pkg' has deferred tests that were never run!\n";
50 3   100     12 $? ||= 255;
51             }
52             }
53              
54 12     12   673 END { _verify() }
55              
56             1;
57              
58             __END__