File Coverage

blib/lib/Test/SetupTeardown.pm
Criterion Covered Total %
statement 35 39 89.7
branch 12 12 100.0
condition 3 3 100.0
subroutine 11 13 84.6
pod 2 3 66.6
total 63 70 90.0


line stmt bran cond sub pod time code
1             package Test::SetupTeardown;
2              
3 3     3   30652 use strict;
  3         8  
  3         101  
4 3     3   16 use warnings;
  3         6  
  3         110  
5              
6 3     3   29 use Test::Builder;
  3         5  
  3         88  
7 3     3   1913 use Try::Tiny;
  3         7552  
  3         592  
8              
9             our $VERSION = 0.004;
10              
11             sub new {
12 7     7 1 1475 my ($class, %routines) = @_;
13 7         13 my $self = \%routines;
14 7         14 bless $self, $class;
15 7 100       37 $self->{begin}->() if $self->{begin};
16 7         18 return $self;
17             }
18              
19             # interrupting e.g. prove with C-c bypasses the DESTROY method, so
20             # trap SIGINT. just die()-ing does not seem to work -- I just get
21             # bin/perl t/whatever.t. exit(1) seems to work better. in any case I
22             # don't see any output anywhere so *shrug*
23              
24             sub handler {
25 0     0 0 0 my $sig = shift;
26 0         0 print "Test::SetupTeardown caught signal $sig, aborting\n";
27 0         0 exit 1;
28             }
29              
30 3     3   2134 use sigtrap handler => \&handler, qw/normal-signals untrapped/;
  3         4016  
  3         25  
31              
32             sub run_test {
33 12     12 1 3645 my ($self, $description, $coderef) = @_;
34 12         16 my $exception_while_running_block;
35              
36 12 100 100     53 if ($ENV{TEST_ST_ONLY}
37             and $ENV{TEST_ST_ONLY} ne $description) {
38             # TEST_ST_ONLY is set for another test case
39 1         4 return;
40             }
41              
42 11         40 Test::Builder->new->note($description);
43              
44             # Run setup() routine before each test
45 11 100       1380 $self->{setup}->() if $self->{setup};
46              
47             try {
48             # Catch all exceptions thrown by the block, to be rethrown
49             # later after the teardown has had a chance to run
50 11     11   827 $coderef->();
51             } catch {
52             # Stash this for now
53 3     3   65 $exception_while_running_block = $_;
54 11         88 };
55              
56             # Run teardown routine after each test
57 11 100       165 $self->{teardown}->() if $self->{teardown};
58              
59 11 100       31 if ($exception_while_running_block) {
60             # The teardown has run now, rethrow the exception
61 3         22 die $exception_while_running_block;
62             }
63             }
64              
65             sub DESTROY {
66 7     7   3392 my $self = shift;
67             try {
68 7 100   7   364 $self->{end}->() if $self->{end};
69             } catch {
70             # an exception from end() won't bubble up from DESTROY,
71             # so at least we should warn
72 0     0     warn $_;
73 7         46 };
74             }
75              
76             1;
77             __END__