| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # -*- Perl -*- | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Test::UnixExit; | 
| 4 | 2 |  |  | 2 |  | 188557 | use 5.006; | 
|  | 2 |  |  |  |  | 18 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 44 |  | 
| 6 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 63 |  | 
| 7 | 2 |  |  | 2 |  | 12 | use Carp qw(croak); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 97 |  | 
| 8 | 2 |  |  | 2 |  | 13 | use Test::Builder; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 106 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | require Exporter; | 
| 13 | 2 |  |  | 2 |  | 13 | use vars qw(@ISA @EXPORT @EXPORT_OK); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 966 |  | 
| 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 | 13833 | my ( $status, $expect, $name ) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 13 | 100 | 100 |  |  | 135 | unless ( defined $status and defined $expect ) { | 
| 24 | 2 |  |  |  |  | 49 | croak "Usage: status expected-value [test-name]"; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 11 |  |  |  |  | 54 | my $ref = ref $expect; | 
| 28 | 11 | 100 |  |  |  | 152 | if ( $ref ne '' ) { | 
|  |  | 100 |  |  |  |  |  | 
| 29 | 5 | 100 |  |  |  | 40 | croak "expected-value must be integer or hash reference" unless $ref eq 'HASH'; | 
| 30 |  |  |  |  |  |  | } elsif ( $expect =~ m/^[0-9]+$/ ) { | 
| 31 | 5 |  |  |  |  | 72 | $expect = { code => $expect }; | 
| 32 |  |  |  |  |  |  | } else { | 
| 33 | 1 |  |  |  |  | 15 | croak "expected-value must be integer or hash reference"; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 9 |  |  |  |  | 74 | my @sigattr = qw(code signal iscore); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 9 | 100 |  |  |  | 79 | my %got = ( | 
| 39 |  |  |  |  |  |  | code   => $status >> 8, | 
| 40 |  |  |  |  |  |  | signal => $status & 127, | 
| 41 |  |  |  |  |  |  | iscore => $status & 128 ? 1 : 0 | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 9 |  |  |  |  | 36 | my $passed = 1; | 
| 45 | 9 |  |  |  |  | 33 | for my $attr (@sigattr) { | 
| 46 | 27 | 100 |  |  |  | 68 | $expect->{$attr} = 0 unless defined $expect->{$attr}; | 
| 47 | 27 | 100 |  |  |  | 73 | $passed = 0 if $got{$attr} != $expect->{$attr}; | 
| 48 |  |  |  |  |  |  | } | 
| 49 | 9 |  |  |  |  | 68 | $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 |  |  |  |  | 8 | map( { $got{$_} } @sigattr ), | 
| 57 | 9 | 100 |  |  |  | 5415 | map( { $expect->{$_} } @sigattr ) | 
|  | 3 |  |  |  |  | 14 |  | 
| 58 |  |  |  |  |  |  | ) unless $passed; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 9 |  |  |  |  | 567 | 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 | 9137 | my ($status) = @_; | 
| 66 | 4 | 100 |  |  |  | 47 | $status = 256 | ( $status & 255 ) unless $status >> 8 == 0; | 
| 67 | 4 |  |  |  |  | 103 | return $status; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | 1; | 
| 71 |  |  |  |  |  |  | __END__ |