File Coverage

blib/lib/Test/Exit.pm
Criterion Covered Total %
statement 28 28 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 12 12 100.0
pod 5 5 100.0
total 48 50 96.0


line stmt bran cond sub pod time code
1             package Test::Exit;
2             {
3             $Test::Exit::VERSION = '0.11';
4             }
5              
6             # ABSTRACT: Test that some code calls exit() without terminating testing
7              
8 2     2   50714 use strict;
  2         5  
  2         66  
9 2     2   9 use warnings;
  2         3  
  2         56  
10              
11 2     2   1490 use Return::MultiLevel qw(with_return);
  2         4205  
  2         125  
12 2     2   12 use base 'Test::Builder::Module';
  2         3  
  2         429  
13              
14             our @EXPORT = qw(exit_code exits_ok exits_zero exits_nonzero never_exits_ok);
15              
16             # We have to install this at compile-time and globally.
17             # We provide one that does effectively nothing, and then override it locally.
18             # Of course, if anyone else overrides CORE::GLOBAL::exit as well, bad stuff happens.
19             our $exit_handler = sub {
20             CORE::exit $_[0];
21             };
22             BEGIN {
23 2 100   2   560 *CORE::GLOBAL::exit = sub (;$) { $exit_handler->(@_ ? 0 + $_[0] : 0) };
  8     8   50  
24             }
25              
26              
27             sub exit_code(&) {
28 9     9 1 64 my ($code) = @_;
29              
30             return with_return {
31 9     9   774 local $exit_handler = $_[0];
32 9         20 $code->();
33             undef
34 9         42 };
  1         12  
35             }
36              
37              
38             sub exits_ok (&;$) {
39 2     2 1 733 my ($code, $description) = @_;
40              
41 2         23 __PACKAGE__->builder->ok(defined &exit_code($code), $description);
42             }
43              
44              
45             sub exits_nonzero (&;$) {
46 2     2 1 1184 my ($code, $description) = @_;
47              
48 2         9 __PACKAGE__->builder->ok(&exit_code($code), $description);
49             }
50              
51              
52             sub exits_zero (&;$) {
53 3     3 1 1092 my ($code, $description) = @_;
54            
55 3         7 my $exit = &exit_code($code);
56 3   33     67 __PACKAGE__->builder->ok(defined $exit && $exit == 0, $description);
57             }
58              
59              
60             sub never_exits_ok (&;$) {
61 1     1 1 359 my ($code, $description) = @_;
62              
63 1         5 __PACKAGE__->builder->ok(!defined &exit_code($code), $description);
64             }
65              
66             1;
67              
68             __END__