blib/lib/Test/Fatal.pm

filter: Statementstmt 95.2% 60 / 63 Branchbran 65.0% 13 / 20 Conditioncond 60.0% 9 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 79.0% 98 / 124 CC 17 SCAR 14.4
CC
17
Cov
84%
__ANON__
7.5
lives_ok
5.2
__ANON__
5.1
CRAP 4.2
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Test::Fatal;
4 # ABSTRACT: incredibly simple helpers for testing code with exceptions
5 3 $Test::Fatal::VERSION = '0.019';
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod use Test::More;
9 #pod use Test::Fatal;
10 #pod
11 #pod use System::Under::Test qw(might_die);
12 #pod
13 #pod is(
14 #pod exception { might_die; },
15 #pod undef,
16 #pod "the code lived",
17 #pod );
18 #pod
19 #pod like(
20 #pod exception { might_die; },
21 #pod qr/turns out it died/,
22 #pod "the code died as expected",
23 #pod );
24 #pod
25 #pod isa_ok(
26 #pod exception { might_die; },
27 #pod 'Exception::Whatever',
28 #pod 'the thrown exception',
29 #pod );
30 #pod
31 #pod =head1 DESCRIPTION
32 #pod
33 #pod Test::Fatal is an alternative to the popular L<Test::Exception>. It does much
34 #pod less, but should allow greater flexibility in testing exception-throwing code
35 #pod with about the same amount of typing.
36 #pod
37 #pod It exports one routine by default: C<exception>.
38 #pod
39 #pod B<Achtung!> C<exception> intentionally does not manipulate the call stack.
40 #pod User-written test functions that use C<exception> must be careful to avoid
41 #pod false positives if exceptions use stack traces that show arguments. For a more
42 #pod magical approach involving globally overriding C<caller>, see
43 #pod L<Test::Exception>.
44 #pod
45 #pod =cut
46  
47 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use Try::Tiny 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 3 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 our @EXPORT = qw(exception);
53 3 our @EXPORT_OK = qw(exception success dies_ok lives_ok);
54  
55 #pod =func exception
56 #pod
57 #pod my $exception = exception { ... };
58 #pod
59 #pod C<exception> takes a bare block of code and returns the exception thrown by
60 #pod that block. If no exception was thrown, it returns undef.
61 #pod
62 #pod B<Achtung!> If the block results in an I<unblessed false> exception, such as 0
63 #pod or the empty string, Test::Fatal itself will die. Since either of these cases
64 #pod indicates a serious problem with the system under testing, this behavior is
65 #pod considered a I<feature>. Note that this issue is only known to occur on perls
66 #pod before 5.14.
67 #pod
68 #pod Exercise caution if you must test for these conditions: wrapping C<exception {
69 #pod ... }> in an C<ok()> block will not give you the result you need, so make sure
70 #pod you use C<is()> instead. You can also directly use L<Try::Tiny>'s try/catch
71 #pod mechanism, the underlying exception handling system of Test::Fatal.
72 #pod
73 #pod Note that there is no TAP assert being performed. In other words, no "ok" or
74 #pod "not ok" line is emitted. It's up to you to use the rest of C<exception> in an
75 #pod existing test like C<ok>, C<isa_ok>, C<is>, et cetera. Or you may wish to use
76 #pod the C<dies_ok> and C<lives_ok> wrappers, which do provide TAP output.
77 #pod
78 #pod C<exception> does I<not> alter the stack presented to the called block, meaning
79 #pod that if the exception returned has a stack trace, it will include some frames
80 #pod between the code calling C<exception> and the thing throwing the exception.
81 #pod This is considered a I<feature> because it avoids the occasionally twitchy
82 #pod C<Sub::Uplevel> mechanism.
83 #pod
84 #pod B<Achtung!> This is not a great idea:
85 #pod
86 #pod sub exception_like(&$;$) {
87 #pod my ($code, $pattern, $name) = @_;
88 #pod like( &exception($code), $pattern, $name );
89 #pod }
90 #pod
91 #pod exception_like(sub { }, qr/foo/, 'foo appears in the exception');
92 #pod
93 #pod If the code in the C<...> is going to throw a stack trace with the arguments to
94 #pod each subroutine in its call stack (for example via C<Carp::confess>,
95 #pod the test name, "foo appears in the exception" will itself be matched by the
96 #pod regex. Instead, write this:
97 #pod
98 #pod like( exception { ... }, qr/foo/, 'foo appears in the exception' );
99 #pod
100 #pod If you really want a test function that passes the test name, wrap the
101 #pod arguments in an array reference to hide the literal text from a stack trace:
102 #pod
103 #pod sub exception_like(&$) {
104 #pod my ($code, $args) = @_;
105 #pod my ($pattern, $name) = @$args;
106 #pod like( &exception($code), $pattern, $name );
107 #pod }
108 #pod
109 #pod exception_like(sub { }, [ qr/foo/, 'foo appears in the exception' ] );
110 #pod
111 #pod To aid in avoiding the problem where the pattern is seen in the exception
112 #pod because of the call stack, C<$Carp::MaxArgNums> is locally set to -1 when the
113 #pod code block is called. If you really don't want that, set it back to whatever
114 #pod value you like at the beginning of the code block. Obviously, this solution
115 #pod doens't affect all possible ways that args of subroutines in the call stack
116 #pod might taint the test. The intention here is to prevent some false passes from
117 #pod people who didn't read the documentation. Your punishment for reading it is
118 #pod that you must consider whether to do anything about this.
119 #pod
120 #pod B<Achtung>: One final bad idea:
121 #pod
122 #pod isnt( exception { ... }, undef, "my code died!");
123 #pod
124 #pod It's true that this tests that your code died, but you should really test that
125 #pod it died I<for the right reason>. For example, if you make an unrelated mistake
126 #pod in the block, like using the wrong dereference, your test will pass even though
127 #pod the code to be tested isn't really run at all. If you're expecting an
128 #pod inspectable exception with an identifier or class, test that. If you're
129 #pod expecting a string exception, consider using C<like>.
130 #pod
131 #pod =cut
132  
133 3 our ($REAL_TBL, $REAL_CALCULATED_TBL) = (1, 1);
134  
135 15 ▶sub exception (&) {
Subroutine exception: called CC=1 SCAR=0.2
Pod documented
136 15 my $code = shift;
137  
138 15 ▶ return try {
Subroutine __ANON__: called CC=5 SCAR=16.3
139 15 ▶ my $incremented = defined $Test::Builder::Level
Branch: 1 branch
BranchTrueFalseTotal
defined $Test::Builder::Level ? : 15 0 15
140 ? $Test::Builder::Level - $REAL_CALCULATED_TBL
141 : 0;
142 15 local $Test::Builder::Level = $REAL_CALCULATED_TBL;
143 15 ▶ if ($incremented) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incremented) 4 11 15
144 # each call to exception adds 5 stack frames
145 4 $Test::Builder::Level += 5;
146 4 for my $i (1..$incremented) {
147 # -2 because we want to see it from the perspective of the call to
148 # is() within the call to $code->()
149 5 my $caller = caller($Test::Builder::Level - 2);
150 5 ▶ if ($caller eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller eq __PACKAGE__) { } 0 5 5
151 # each call to exception adds 5 stack frames
152 0 $Test::Builder::Level = $Test::Builder::Level + 5;
153 }
154 else {
155 5 $Test::Builder::Level = $Test::Builder::Level + 1;
156 }
157 }
158 }
159  
160 15 local $REAL_CALCULATED_TBL = $Test::Builder::Level;
161 15 local $Carp::MaxArgNums = -1;
162 15 $code->();
163 10 return undef;
164 5 ▶ } catch {
Subroutine __ANON__: called CC=4 SCAR=20.1
165 5 ▶ return $_ if $_ or ref; # allow objects with a false boolean overload
Branch: 1 branch
BranchTrueFalseTotal
if $_ or ref $_ 5 0 5
Condition66%2/3
$_ or ref $_
l!l&&r!l&&!r
410
MC/DC0%0/2
$_ or ref $_
$_ ref $_
Truth table66%2/3
A or B
A= $_B= ref $_
ABresult
1X1
011
000
166  
167 0 ▶ my $problem = defined $_ ? 'false' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
168 0 Carp::confess("$problem exception caught by Test::Fatal::exception");
169 15 };
170 }
171  
172 #pod =func success
173 #pod
174 #pod try {
175 #pod should_live;
176 #pod } catch {
177 #pod fail("boo, we died");
178 #pod } success {
179 #pod pass("hooray, we lived");
180 #pod };
181 #pod
182 #pod C<success>, exported only by request, is a L<Try::Tiny> helper with semantics
183 #pod identical to L<C<finally>|Try::Tiny/finally>, but the body of the block will
184 #pod only be run if the C<try> block ran without error.
185 #pod
186 #pod Although almost any needed exception tests can be performed with C<exception>,
187 #pod success blocks may sometimes help organize complex testing.
188 #pod
189 #pod =cut
190  
191 3 ▶sub success (&;@) {
Subroutine success: called CC=1 SCAR=0.0
Pod documented
192 3 my $code = shift;
193 3 ▶ return finally( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
194 3 ▶ return if @_; # <-- only run on success
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
195 1 $code->();
196 3 }, @_ );
197 }
198  
199 #pod =func dies_ok
200 #pod
201 #pod =func lives_ok
202 #pod
203 #pod Exported only by request, these two functions run a given block of code, and
204 #pod provide TAP output indicating if it did, or did not throw an exception.
205 #pod These provide an easy upgrade path for replacing existing unit tests based on
206 #pod C<Test::Exception>.
207 #pod
208 #pod RJBS does not suggest using this except as a convenience while porting tests to
209 #pod use Test::Fatal's C<exception> routine.
210 #pod
211 #pod use Test::More tests => 2;
212 #pod use Test::Fatal qw(dies_ok lives_ok);
213 #pod
214 #pod dies_ok { die "I failed" } 'code that fails';
215 #pod
216 #pod lives_ok { return "I'm still alive" } 'code that does not fail';
217 #pod
218 #pod =cut
219  
220 3 my $Tester;
221  
222 # Signature should match that of Test::Exception
223 3 ▶sub dies_ok (&;$) {
Subroutine dies_ok: called CC=5 SCAR=16.3
Pod documented
224 3 my $code = shift;
225 3 my $name = shift;
226  
227 3 require Test::Builder;
228 3 ▶ $Tester ||= Test::Builder->new;
Condition66%2/3
$Tester ||= "Test::Builder"->new
l!l&&r!l&&!r
210
MC/DC0%0/2
$Tester ||= "Test::Builder"->new
$Tester "Test::Builder"->new
Truth table66%2/3
A ||= B
A= $TesterB= "Test::Builder"->new
ABresult
1X1
011
000
229  
230 3 my $tap_pos = $Tester->current_test;
231  
232 3 my $exception = exception( \&$code );
233  
234 3 ▶ $name ||= $tap_pos != $Tester->current_test
Branch: 1 branch
BranchTrueFalseTotal
$tap_pos != $Tester->current_test ? : 0 1 1
Condition66%2/3
$name ||= $tap_pos != $Tester->current_test ? "...and code should throw an exception" : "code should throw an exception"
l!l&&r!l&&!r
210
MC/DC0%0/2
$name ||= $tap_pos != $Tester->current_test ? "...and code should throw an exception" : "code should throw an exception"
$name $tap_pos != $Tester->current_test ? "...and code should throw an exception" : "code should throw an exception"
Truth table66%2/3
A ||= B
A= $nameB= $tap_pos != $Tester->current_test ? "...and code should throw an exception" : "code should throw an exception"
ABresult
1X1
011
000
235 ? "...and code should throw an exception"
236 : "code should throw an exception";
237  
238 3 my $ok = $Tester->ok( $exception, $name );
239 3 ▶ $ok or $Tester->diag( "expected an exception but none was raised" );
Branch: 1 branch
BranchTrueFalseTotal
$ok or $Tester->diag("expected an exception but none was raised") 2 1 3
240 3 return $ok;
241 }
242  
243 3 ▶sub lives_ok (&;$) {
Subroutine lives_ok: called CC=5 SCAR=16.5
Pod documented
244 3 my $code = shift;
245 3 my $name = shift;
246  
247 3 require Test::Builder;
248 3 ▶ $Tester ||= Test::Builder->new;
Condition33%1/3
$Tester ||= "Test::Builder"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$Tester ||= "Test::Builder"->new
$Tester "Test::Builder"->new
Truth table33%1/3
A ||= B
A= $TesterB= "Test::Builder"->new
ABresult
1X1
011
000
249  
250 3 my $tap_pos = $Tester->current_test;
251  
252 3 my $exception = exception( \&$code );
253  
254 3 ▶ $name ||= $tap_pos != $Tester->current_test
Branch: 1 branch
BranchTrueFalseTotal
$tap_pos != $Tester->current_test ? : 0 1 1
Condition66%2/3
$name ||= $tap_pos != $Tester->current_test ? "...and code should not throw an exception" : "code should not throw an exception"
l!l&&r!l&&!r
210
MC/DC0%0/2
$name ||= $tap_pos != $Tester->current_test ? "...and code should not throw an exception" : "code should not throw an exception"
$name $tap_pos != $Tester->current_test ? "...and code should not throw an exception" : "code should not throw an exception"
Truth table66%2/3
A ||= B
A= $nameB= $tap_pos != $Tester->current_test ? "...and code should not throw an exception" : "code should not throw an exception"
ABresult
1X1
011
000
255 ? "...and code should not throw an exception"
256 : "code should not throw an exception";
257  
258 3 my $ok = $Tester->ok( ! $exception, $name );
259 3 ▶ $ok or $Tester->diag( "expected return but an exception was raised" );
Branch: 1 branch
BranchTrueFalseTotal
$ok or $Tester->diag("expected return but an exception was raised") 2 1 3
260 3 return $ok;
261 }
262  
263 3 1;
264  
265 __END__
↑ Summary