blib/lib/Devel/EvalError.pm

filter: Statementstmt 79.2% 84 / 106 Branchbran 52.1% 24 / 46 Conditioncond 36.8% 7 / 19 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 76.1% 16 / 21 Podpod 100% 9 / 9 totaltotal 66.6% 142 / 213 CC 32 SCAR 16.7
CC
32
Cov
67%
_expected
10.4
Reason
9.4
ExpectOne
8.7
CRAP 5.3
↑ Summary
1 package Devel::EvalError;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4  
5 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
8 1 $VERSION= 0.001_002;
9 1 my $idx= 0;
10 1 for my $name (
11 '_prevReason', # Value from $@ when c'ted, restored to $@ in d'tor
12 '_reasons', # List of possible failure reasons from $SIG{__DIE__}
13 '_prevHandler', # Value of $SIG{__DIE__} before c'ted
14 '_newHandler', # Address of $SIG{__DIE__} after c'ted
15 '_succeeded', # Whether the eval() was successful
16 '_knowReason', # True if $@ survived
17 ) {
18 6 my $code= "sub $name() { $idx; }";
19 6 ▶ eval "$code; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "$code; 1" or die "Couldn't compile constant ($code): $@\n" 6 0 6
20 or die "Couldn't compile constant ($code): $@\n";
21 6 $idx++;
22 }
23 }
24  
25  
26 0 ▶sub _croak { # Die but report the caller's line number
Subroutine _croak: not called CC=1 SCAR=6.9
27 0 require Carp;
28 0 Carp::croak( @_ );
29 }
30  
31 0 ▶sub _confess { # Die with stack trace
Subroutine _confess: not called CC=1 SCAR=6.9
32 0 require Carp;
33 0 local $Carp::CarpLevel= $Carp::CarpLevel + 1;
34 0 Carp::confess( @_ );
35 }
36  
37 0 ▶sub _cluck { # Warn with stack trace
Subroutine _cluck: not called CC=1 SCAR=6.9
38 0 require Carp;
39 0 local $Carp::CarpLevel= $Carp::CarpLevel + 1;
40 0 Carp::cluck( @_ );
41 }
42  
43  
44 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
45 2 my( $we )= @_;
46 2 ▶ _croak( "Devel::EvalError: new() via an object is not allowed" )
Branch: 1 branch
BranchTrueFalseTotal
if ref $we 0 2 2
47 if ref $we;
48 2 my $me= bless [], $we;
49 2 $me->_init();
50 2 return $me;
51 }
52  
53  
54 2 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
55 2 my( $me )= @_;
56 2 ▶ _confess( "Devel::EvalError: Erase() required before _init()" )
Branch: 1 branch
BranchTrueFalseTotal
if $me->[1] 0 2 2
57 if $me->[_reasons];
58 2 $me->[_prevReason]= $@;
59 2 $me->[_reasons]= my $reasons= [];
60 2 $me->[_prevHandler]= my $prevHandler= $SIG{__DIE__};
61 2 ▶ my $newHandler= sub { _handleDie( $reasons, $prevHandler, @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 $me->[_newHandler]= 0 + $newHandler;
63 2 $SIG{__DIE__}= $newHandler;
64 2 return;
65 }
66  
67  
68 1 ▶sub _handleDie {
Subroutine _handleDie: called CC=3 SCAR=11.2
69 1 my $list= shift @_;
70 1 my $handler= shift @_;
71 1 ▶ push @{ $list },
Branch: 1 branch
BranchTrueFalseTotal
1 == @_ ? : 1 0 1
72 ( 1 == @_ ) ? $_[0] : join '', @_;
73 1 ▶ $handler->( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if $handler 0 1 1
74 if $handler;
75 1 return;
76 }
77  
78  
79 2 ▶sub Erase {
Subroutine Erase: called CC=2 SCAR=7.0
Pod documented
80 2 my( $me )= @_;
81 2 $me->_revertHandler();
82 2 my $prevReason= $me->[_prevReason];
83 2 ▶ $@= $prevReason
Branch: 1 branch
BranchTrueFalseTotal
if $prevReason 2 0 2
84 if $prevReason;
85 2 return;
86 }
87  
88  
89 0 ▶sub Reuse {
Subroutine Reuse: not called CC=1 SCAR=6.9
Pod documented
90 0 my( $me )= @_;
91 0 $me->Erase();
92 0 $me->_init();
93 0 return $me;
94 }
95  
96  
97 1 ▶sub ExpectOne {
Subroutine ExpectOne: called CC=5 SCAR=21.6
Pod documented
98 1 my $me= shift @_;
99 1 my $okay;
100 1 ▶ if( @_ <= 1 && ! defined $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ <= 1 and not defined $_[0]) { } 1 0 1
elsif (1 == @_ and 1 eq $_[0]) { } 0 0 0
Condition33%1/3
@_ <= 1 and not defined $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@_ <= 1 and not defined $_[0]
@_ <= 1 not defined $_[0]
Truth table33%1/3
A and not(B)
A= @_ <= 1B= not defined $_[0]
ABresult
0X0
100
111
101 1 $okay= 0;
102 ▶ } elsif( 1 == @_ && 1 eq $_[0] ) {
Condition0%0/3
1 == @_ and 1 eq $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @_ and 1 eq $_[0]
1 == @_ 1 eq $_[0]
Truth table0%0/3
A and B
A= 1 == @_B= 1 eq $_[0]
ABresult
0X0
100
111
103 0 $okay= 1;
104 }
105 1 $me->_expected( $okay, @_ );
106 1 return $me;
107 }
108  
109  
110 1 ▶sub ExpectNonEmpty {
Subroutine ExpectNonEmpty: called CC=3 SCAR=11.4
Pod documented
111 1 my $me= shift @_;
112 1 ▶ my $okay= 0 == @_ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
0 == @_ ? : 0 1 1
113 1 $me->_expected( $okay, @_ );
114 1 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
115 }
116  
117  
118 2 ▶sub _expected {
Subroutine _expected: called CC=8 SCAR=23.4
119 2 my $me= shift @_;
120 2 my $okay= shift @_;
121 2 my $reason= $@; # Copy early as $@ can easily change
122 2 my $caller= (caller 1)[3];
123 2 $me->_revertHandler();
124 2 ▶ _croak( 'Devel::EvalError: $caller() called on Erase()d object' )
Branch: 1 branch
BranchTrueFalseTotal
unless $me->[1] 0 2 2
125 if ! $me->[_reasons];
126 2 ▶ _croak( 'Devel::EvalError: $caller() called on object more than once' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $me->[4] 0 2 2
127 if defined $me->[_succeeded];
128 2 $me->[_succeeded]= $okay;
129 2 ▶ if( ! defined $okay ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $okay) 0 2 2
130 _croak(
131 "Devel::EvalError: $caller() misused, passed( ",
132 0 ▶ join( ", ", map { defined $_ ? $_ : "(undef)" } @_ ),
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
133 " )",
134 );
135 }
136 2 ▶ if( ! $okay && $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $okay and $reason) 1 1 2
Condition66%2/3
not $okay and $reason
!ll&&!rl&&r
101
MC/DC50%1/2
not $okay and $reason
not $okay $reason
Truth table66%2/3
not(A) and B
A= not $okayB= $reason
ABresult
0X0
100
111
137 1 $me->[_knowReason]= 1;
138 0 ▶ push @{ $me->[_reasons] }, $reason
Branch: 1 branch
BranchTrueFalseTotal
if $reason ne $me->[1][-1] 0 1 1
139 if $reason ne $me->[_reasons][-1];
140 }
141 }
142  
143  
144 4 ▶sub _revertHandler {
Subroutine _revertHandler: called CC=4 SCAR=14.1
145 4 my( $me )= @_;
146 return
147 4 ▶ if ! $me->[_reasons];
Branch: 1 branch
BranchTrueFalseTotal
unless $me->[1] 0 4 4
148 4 my $handler= $me->[_prevHandler];
149 4 ▶ if( '0' ne $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 ne $handler) 2 2 4
150 2 my $current= $SIG{__DIE__};
151 2 $SIG{__DIE__}= $handler;
152 2 ▶ if( $current != $me->[_newHandler] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current != $me->[3]) 0 2 2
153 0 _cluck(
154 '$SIG{__DIE__} changed out from under Devel::EvalError',
155 );
156 }
157 2 $me->[_prevHandler]= 0;
158 }
159 4 return;
160 }
161  
162  
163 0 ▶sub AllReasons {
Subroutine AllReasons: not called CC=2 SCAR=17.9
Pod documented
164 0 my( $me )= @_;
165 0 ▶ return @{ $me->[_reasons] || [] };
Condition0%0/2
$me->[1] || []
l!l
00
MC/DC0%0/1
$me->[1] || []
$me->[1]
Truth table0%0/2
A
A= $me->[1]
Aresult
11
00
166 }
167  
168  
169 2 ▶sub Reason {
Subroutine Reason: called CC=7 SCAR=22.4
Pod documented
170 2 my( $me )= @_;
171 2 ▶ my @reasons= @{ $me->[_reasons] || [] };
Condition50%1/2
$me->[1] || []
l!l
20
MC/DC0%0/1
$me->[1] || []
$me->[1]
Truth table50%1/2
A
A= $me->[1]
Aresult
11
00
172 2 ▶ return ''
Branch: 1 branch
BranchTrueFalseTotal
if not defined $me->[4] or $me->[4] 1 1 2
Condition66%2/3
not defined $me->[4] or $me->[4]
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $me->[4] or $me->[4]
not defined $me->[4] $me->[4]
Truth table66%2/3
not(A) or B
A= not defined $me->[4]B= $me->[4]
ABresult
1X1
011
000
173 if ! defined $me->[_succeeded] # if eval() not called (yet?)
174 || $me->[_succeeded]; # or eval() succeeded.
175 1 ▶ if( ! @reasons ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@reasons) 0 1 1
176 # We "know" eval() failed but we never intercepted a die() message.
177 # There are several possibilities (likeliest first):
178 # 1) $ee->ExpectNonEmpty( eval { getList(); } );
179 # but getList() returned an empty list.
180 # 2) $ee->ExpectOne( eval { return; } ); # or similar
181 # 3) Some unexpected way for eval() to fail without our
182 # $SIG{__DIE__} handler ever getting called. This might be
183 # a Perl bug or something strange that XS code can do or such.
184 # 4) Somebody meddling with our object's internals. C'est le Vie.
185 # 5) etc.
186 0 return 'Unknown failure reason or returned empty list!';
187 }
188 1 ▶ return $reasons[-1]
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @reasons or $me->[5] 1 0 1
Condition33%1/3
1 == @reasons or $me->[5]
l!l&&r!l&&!r
100
MC/DC0%0/2
1 == @reasons or $me->[5]
1 == @reasons $me->[5]
Truth table33%1/3
A or B
A= 1 == @reasonsB= $me->[5]
ABresult
1X1
011
000
189 if 1 == @reasons || $me->[_knowReason];
190 0 return join "\nTHEN ", @reasons;
191 }
192  
193  
194 2 ▶sub Succeeded {
Subroutine Succeeded: called CC=2 SCAR=7.0
Pod documented
195 2 my( $me )= @_;
196 2 my $okay= $me->[_succeeded];
197 2 ▶ _croak(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $okay 0 2 2
198 'Devel::EvalError: Expect*() not called before checking for success',
199 ) if ! defined $okay;
200 2 return $okay;
201 }
202  
203  
204 1 ▶sub Failed {
Subroutine Failed: called CC=1 SCAR=0.0
Pod documented
205 1 my( $me )= @_;
206 1 return ! $me->Succeeded();
207 }
208  
209  
210  
211 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
212 2 my( $me )= @_;
213 2 $me->Erase();
214 }
215  
216  
217 1 'Devel::EvalError';
218 __END__
↑ Summary