File Coverage

blib/lib/Test/UnixExit.pm
Criterion Covered Total %
statement 39 39 100.0
branch 18 18 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 70 70 100.0


line stmt bran cond sub pod time code
1             # -*- Perl -*-
2              
3             package Test::UnixExit;
4 2     2   207526 use 5.006;
  2         18  
5 2     2   12 use strict;
  2         3  
  2         48  
6 2     2   9 use warnings;
  2         4  
  2         70  
7 2     2   10 use Carp qw(croak);
  2         4  
  2         128  
8 2     2   15 use Test::Builder;
  2         5  
  2         103  
9              
10             our $VERSION = '0.02';
11              
12             require Exporter;
13 2     2   11 use vars qw(@ISA @EXPORT @EXPORT_OK);
  2         4  
  2         924  
14             @ISA = qw(Exporter);
15             @EXPORT = qw(exit_is);
16             @EXPORT_OK = qw(exit_is_nonzero);
17              
18             my $test = Test::Builder->new;
19              
20             sub exit_is {
21 13     13 1 14185 my ( $status, $expect, $name ) = @_;
22              
23 13 100 100     166 unless ( defined $status and defined $expect ) {
24 2         53 croak "Usage: status expected-value [test-name]";
25             }
26              
27 11         59 my $ref = ref $expect;
28 11 100       205 if ( $ref ne '' ) {
    100          
29 5 100       48 croak "expected-value must be integer or hash reference" unless $ref eq 'HASH';
30             } elsif ( $expect =~ m/^[0-9]+$/ ) {
31 5         91 $expect = { code => $expect };
32             } else {
33 1         52 croak "expected-value must be integer or hash reference";
34             }
35              
36 9         73 my @sigattr = qw(code signal iscore);
37              
38 9 100       129 my %got = (
39             code => $status >> 8,
40             signal => $status & 127,
41             iscore => $status & 128 ? 1 : 0
42             );
43              
44 9         38 my $passed = 1;
45 9         49 for my $attr (@sigattr) {
46 27 100       87 $expect->{$attr} = 0 unless defined $expect->{$attr};
47 27 100       87 $passed = 0 if $got{$attr} != $expect->{$attr};
48             }
49 9         116 $test->ok( $passed, $name );
50              
51             # verbose by default as signal failures are rare (for me) and may be
52             # hard to reproduce
53             $test->diag(
54             sprintf
55             "Got: code=%-3d signal=%-2d iscore=%d\nExpected: code=%-3d signal=%-2d iscore=%d\n",
56 3         9 map( { $got{$_} } @sigattr ),
57 9 100       6656 map( { $expect->{$_} } @sigattr )
  3         17  
58             ) unless $passed;
59              
60 9         572 return $passed;
61             }
62              
63             # for any non-zero exit code make the exit code 1
64             sub exit_is_nonzero {
65 4     4 1 9150 my ($status) = @_;
66 4 100       64 $status = 256 | ( $status & 255 ) unless $status >> 8 == 0;
67 4         103 return $status;
68             }
69              
70             1;
71             __END__