blib/lib/Test/Exception.pm

filter: Statementstmt 97.1% 101 / 104 Branchbran 90.6% 29 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 94.2% 163 / 173 CC 21 SCAR 12.8
CC
21
Cov
94%
_quiet_caller
7.3
throws_ok
6.0
_exception_as_string
4.0
CRAP 3.6
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Test::Exception;
5 10 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Sub::Uplevel qw( uplevel );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our $VERSION = '0.43';
10 10 $VERSION = eval $VERSION;
11  
12 10 our @EXPORT = qw(dies_ok lives_ok throws_ok lives_and);
13  
14 10 my $Tester = Test::Builder->new;
15  
16 10 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
17 10 my $self = shift;
18 10 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 9 10
19 1 my $package = caller;
20 1 $Tester->exported_to( $package );
21 1 $Tester->plan( @_ );
22 };
23 10 $self->export_to_level( 1, $self, $_ ) foreach @EXPORT;
24 }
25  
26 =head1 NAME
27  
28 Test::Exception - Test exception-based code
29  
30 =head1 SYNOPSIS
31  
32 use Test::More tests => 5;
33 use Test::Exception;
34  
35 # or if you don't need Test::More
36  
37 use Test::Exception tests => 5;
38  
39 # then...
40  
41 # Check that the stringified exception matches given regex
42 throws_ok { $foo->method } qr/division by zero/, 'zero caught okay';
43  
44 # Check an exception of the given class (or subclass) is thrown
45 throws_ok { $foo->method } 'Error::Simple', 'simple error thrown';
46
47 # all Test::Exceptions subroutines are guaranteed to preserve the state
48 # of $@ so you can do things like this after throws_ok and dies_ok
49 like $@, 'what the stringified exception should look like';
50  
51 # Check that something died - we do not care why
52 dies_ok { $foo->method } 'expecting to die';
53  
54 # Check that something did not die
55 lives_ok { $foo->method } 'expecting to live';
56  
57 # Check that a test runs without an exception
58 lives_and { is $foo->method, 42 } 'method is 42';
59
60 # or if you don't like prototyped functions
61
62 throws_ok( sub { $foo->method }, qr/division by zero/,
63 'zero caught okay' );
64 throws_ok( sub { $foo->method }, 'Error::Simple',
65 'simple error thrown' );
66 dies_ok( sub { $foo->method }, 'expecting to die' );
67 lives_ok( sub { $foo->method }, 'expecting to live' );
68 lives_and( sub { is $foo->method, 42 }, 'method is 42' );
69  
70  
71 =head1 DESCRIPTION
72  
73 This module provides a few convenience methods for testing exception based code. It is built with
74 L<Test::Builder> and plays happily with L<Test::More> and friends.
75  
76 If you are not already familiar with L<Test::More> now would be the time to go take a look.
77  
78 You can specify the test plan when you C<use Test::Exception> in the same way as C<use Test::More>.
79 See L<Test::More> for details.
80  
81 NOTE: Test::Exception only checks for exceptions. It will ignore other methods of stopping
82 program execution - including exit(). If you have an exit() in evalled code Test::Exception
83 will not catch this with any of its testing functions.
84  
85 NOTE: This module uses L<Sub::Uplevel> and relies on overriding
86 C<CORE::GLOBAL::caller> to hide your test blocks from the call stack. If this
87 use of global overrides concerns you, the L<Test::Fatal> module offers a more
88 minimalist alternative.
89  
90 =cut
91  
92 72 ▶sub _quiet_caller (;$) { ## no critic Prototypes
Subroutine _quiet_caller: called CC=6 SCAR=19.9
93 72 my $height = $_[0];
94 72 $height++;
95  
96 72 ▶ if ( CORE::caller() eq 'DB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller() eq 'DB') { } 21 51 72
97 # passthrough the @DB::args trick
98 package DB;
99 21 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 21 0 21
100 21 ▶ if ( !@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 0 21 21
101 0 return (CORE::caller($height))[0..2];
102 }
103 else {
104 # If we got here, we are within a Test::Exception test, and
105 # something is producing a stacktrace. In case this is a full
106 # trace (i.e. confess() ), we have to make sure that the sub
107 # args are not visible. If we do not do this, and the test in
108 # question is throws_ok() with a regex, it will end up matching
109 # against itself in the args to throws_ok().
110 #
111 # While it is possible (and maybe wise), to test if we are
112 # indeed running under throws_ok (by crawling the stack right
113 # up from here), the old behavior of Test::Exception was to
114 # simply obliterate @DB::args altogether in _quiet_caller, so
115 # we are just preserving the behavior to avoid surprises
116 #
117 21 my @frame_info = CORE::caller($height);
118 21 @DB::args = ();
119 21 return @frame_info;
120 }
121 }
122  
123 # fallback if nothing above returns
124 0 return CORE::caller($height);
125 }
126 else {
127 51 ▶ if( wantarray and !@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and not @_) { } 0 51 51
Condition33%1/3
wantarray and not @_
!ll&&!rl&&r
0510
MC/DC0%0/2
wantarray and not @_
wantarray not @_
Truth table33%1/3
A and not(B)
A= wantarrayB= not @_
ABresult
0X0
100
111
128 0 return (CORE::caller($height))[0..2];
129 }
130 else {
131 51 return CORE::caller($height);
132 }
133 }
134 }
135  
136 33 ▶sub _try_as_caller {
Subroutine _try_as_caller: called CC=1 SCAR=0.0
137 33 my $coderef = shift;
138  
139 # local works here because Sub::Uplevel has already overridden caller
140 33 local *CORE::GLOBAL::caller;
141 10 ▶ { no warnings 'redefine'; *CORE::GLOBAL::caller = \&_quiet_caller; }
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 33 eval { uplevel 3, $coderef };
144 33 return $@;
145 };
146  
147  
148 48 ▶sub _is_exception {
Subroutine _is_exception: called CC=2 SCAR=6.9
149 48 my $exception = shift;
150 48 ▶ return ref $exception || $exception ne '';
Condition100%3/3
ref $exception || $exception ne ''
l!l&&r!l&&!r
15267
MC/DC100%2/2
ref $exception || $exception ne ''
ref $exception $exception ne ''
Truth table100%3/3
A || B
A= ref $exceptionB= $exception ne ''
ABresult
1X1
011
000
151 };
152  
153  
154 34 ▶sub _exception_as_string {
Subroutine _exception_as_string: called CC=4 SCAR=13.9
155 34 my ( $prefix, $exception ) = @_;
156 34 ▶ return "$prefix normal exit" unless _is_exception( $exception );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_exception($exception) 2 32 34
157 32 my $class = ref $exception;
158 32 ▶ $exception = "$class ($exception)"
Branch: 1 branch
BranchTrueFalseTotal
if $class and not "$exception" =~ /^\Q$class\E/ 9 23 32
Condition100%3/3
$class and not "$exception" =~ /^\Q$class\E/
!ll&&!rl&&r
2039
MC/DC100%2/2
$class and not "$exception" =~ /^\Q$class\E/
$class not "$exception" =~ /^\Q$class\E/
Truth table100%3/3
A and not(B)
A= $classB= not "$exception" =~ /^\Q$class\E/
ABresult
0X0
100
111
159 if $class && "$exception" !~ m/^\Q$class/;
160 32 chomp $exception;
161 32 return "$prefix $exception";
162 };
163  
164  
165 =over 4
166  
167 =item B<throws_ok>
168  
169 Tests to see that a specific exception is thrown. throws_ok() has two forms:
170  
171 throws_ok BLOCK REGEX, TEST_DESCRIPTION
172 throws_ok BLOCK CLASS, TEST_DESCRIPTION
173  
174 In the first form the test passes if the stringified exception matches the give regular expression. For example:
175  
176 throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file';
177  
178 If your perl does not support C<qr//> you can also pass a regex-like string, for example:
179  
180 throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file';
181  
182 The second form of throws_ok() test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example:
183  
184 throws_ok { $foo->bar } "Error::Simple", 'simple error';
185  
186 Will only pass if the C<bar> method throws an Error::Simple exception, or a subclass of an Error::Simple exception.
187  
188 You can get the same effect by passing an instance of the exception you want to look for. The following is equivalent to the previous example:
189  
190 my $SIMPLE = Error::Simple->new;
191 throws_ok { $foo->bar } $SIMPLE, 'simple error';
192  
193 Should a throws_ok() test fail it produces appropriate diagnostic messages. For example:
194  
195 not ok 3 - simple error
196 # Failed test (test.t at line 48)
197 # expecting: Error::Simple exception
198 # found: normal exit
199  
200 Like all other Test::Exception functions you can avoid prototypes by passing a subroutine explicitly:
201  
202 throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' );
203  
204 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
205  
206 A description of the exception being checked is used if no optional test description is passed.
207  
208 NOTE: Remember when you C<die $string_without_a_trailing_newline> perl will
209 automatically add the current script line number, input line number and a newline. This will
210 form part of the string that throws_ok regular expressions match against.
211  
212  
213 =cut
214  
215  
216 22 ▶sub throws_ok (&$;$) {
Subroutine throws_ok: called CC=6 SCAR=17.9
Pod documented
217 22 my ( $coderef, $expecting, $description ) = @_;
218 22 ▶ unless (defined $expecting) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expecting) 1 21 22
219 1 require Carp;
220 1 Carp::croak( "throws_ok: must pass exception class/object or regex" );
221 }
222 21 ▶ $description = _exception_as_string( "threw", $expecting )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $description 11 10 21
223 unless defined $description;
224 21 my $exception = _try_as_caller( $coderef );
225 21 my $regex = $Tester->maybe_regex( $expecting );
226 my $ok = $regex
227 ? ( $exception =~ m/$regex/ )
228 21 ▶ : eval {
Branch: 1 branch
BranchTrueFalseTotal
$regex ? : 12 9 21
229 9 ▶ $exception->isa( ref $expecting ? ref $expecting : $expecting )
Branch: 1 branch
BranchTrueFalseTotal
ref $expecting ? : 2 7 9
230 };
231 21 $Tester->ok( $ok, $description );
232 21 ▶ unless ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 9 12 21
233 9 $Tester->diag( _exception_as_string( "expecting:", $expecting ) );
234 9 $Tester->diag( _exception_as_string( "found:", $exception ) );
235 };
236 21 $@ = $exception;
237 21 return $ok;
238 };
239  
240  
241 =item B<dies_ok>
242  
243 Checks that a piece of code dies, rather than returning normally. For example:
244  
245 sub div {
246 my ( $a, $b ) = @_;
247 return $a / $b;
248 };
249  
250 dies_ok { div( 1, 0 ) } 'divide by zero detected';
251  
252 # or if you don't like prototypes
253 dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' );
254  
255 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
256  
257 Remember: This test will pass if the code dies for any reason. If you care about the reason it might be more sensible to write a more specific test using throws_ok().
258  
259 The test description is optional, but recommended.
260  
261 =cut
262  
263 6 ▶sub dies_ok (&;$) {
Subroutine dies_ok: called CC=1 SCAR=0.0
Pod documented
264 6 my ( $coderef, $description ) = @_;
265 6 my $exception = _try_as_caller( $coderef );
266 6 my $ok = $Tester->ok( _is_exception($exception), $description );
267 6 $@ = $exception;
268 6 return $ok;
269 }
270  
271  
272 =item B<lives_ok>
273  
274 Checks that a piece of code doesn't die. This allows your test script to continue, rather than aborting if you get an unexpected exception. For example:
275  
276 sub read_file {
277 my $file = shift;
278 local $/;
279 open my $fh, '<', $file or die "open failed ($!)\n";
280 $file = <FILE>;
281 return $file;
282 };
283  
284 my $file;
285 lives_ok { $file = read_file('test.txt') } 'file read';
286  
287 # or if you don't like prototypes
288 lives_ok( sub { $file = read_file('test.txt') }, 'file read' );
289  
290 Should a lives_ok() test fail it produces appropriate diagnostic messages. For example:
291  
292 not ok 1 - file read
293 # Failed test (test.t at line 15)
294 # died: open failed (No such file or directory)
295  
296 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
297  
298 The test description is optional, but recommended.
299  
300 =cut
301  
302 6 ▶sub lives_ok (&;$) {
Subroutine lives_ok: called CC=2 SCAR=6.9
Pod documented
303 6 my ( $coderef, $description ) = @_;
304 6 my $exception = _try_as_caller( $coderef );
305 6 my $ok = $Tester->ok( ! _is_exception( $exception ), $description );
306 6 ▶ $Tester->diag( _exception_as_string( "died:", $exception ) ) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 4 2 6
307 6 $@ = $exception;
308 6 return $ok;
309 }
310  
311  
312 =item B<lives_and>
313  
314 Run a test that may throw an exception. For example, instead of doing:
315  
316 my $file;
317 lives_ok { $file = read_file('answer.txt') } 'read_file worked';
318 is $file, "42", 'answer was 42';
319  
320 You can use lives_and() like this:
321  
322 lives_and { is read_file('answer.txt'), "42" } 'answer is 42';
323 # or if you don't like prototypes
324 lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42');
325  
326 Which is the same as doing
327  
328 is read_file('answer.txt'), "42\n", 'answer is 42';
329  
330 unless C<read_file('answer.txt')> dies, in which case you get the same kind of error as lives_ok()
331  
332 not ok 1 - answer is 42
333 # Failed test (test.t at line 15)
334 # died: open failed (No such file or directory)
335  
336 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
337  
338 The test description is optional, but recommended.
339  
340 =cut
341  
342 4 ▶sub lives_and (&;$) {
Subroutine lives_and: called CC=3 SCAR=11.0
Pod documented
343 4 my ( $test, $description ) = @_;
344 {
345 4 my $ok = \&Test::Builder::ok;
346 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 3 ▶ local *Test::Builder::ok = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
348 3 local $Test::Builder::Level = $Test::Builder::Level + 1;
349 3 ▶ $_[2] = $description unless defined $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[2] 2 1 3
350 3 $ok->(@_);
351 4 };
352 10 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
353 4 ▶ eval { $test->() } and return 1;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$test() } } and return 1 2 2 4
354 };
355 2 my $exception = $@;
356 2 ▶ if ( _is_exception( $exception ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_exception($exception)) 1 1 2
357 1 $Tester->ok( 0, $description );
358 1 $Tester->diag( _exception_as_string( "died:", $exception ) );
359 };
360 2 $@ = $exception;
361 2 return;
362 }
363  
364 =back
365  
366  
367 =head1 SKIPPING TEST::EXCEPTION TESTS
368  
369 Sometimes we want to use Test::Exception tests in a test suite, but don't want to force the user to have Test::Exception installed. One way to do this is to skip the tests if Test::Exception is absent. You can do this with code something like this:
370  
371 use strict;
372 use warnings;
373 use Test::More;
374
375 BEGIN {
376 eval "use Test::Exception";
377 plan skip_all => "Test::Exception needed" if $@;
378 }
379
380 plan tests => 2;
381 # ... tests that need Test::Exception ...
382  
383 Note that we load Test::Exception in a C<BEGIN> block ensuring that the subroutine prototypes are in place before the rest of the test script is compiled.
384  
385  
386 =head1 BUGS
387  
388 There are some edge cases in Perl's exception handling where Test::Exception will miss exceptions
389 thrown in DESTROY blocks. See the RT bug L<http://rt.cpan.org/Ticket/Display.html?id=24678> for
390 details, along with the t/edge-cases.t in the distribution test suite. These will be addressed in
391 a future Test::Exception release.
392  
393 If you find any more bugs please let me know by e-mail, or report the problem with
394 L<http://rt.cpan.org/>.
395  
396  
397 =head1 COMMUNITY
398  
399 =over 4
400  
401 =item perl-qa
402  
403 If you are interested in testing using Perl I recommend you visit L<http://qa.perl.org/> and join the excellent perl-qa mailing list. See L<http://lists.perl.org/showlist.cgi?name=perl-qa> for details on how to subscribe.
404  
405 =item perlmonks
406  
407 You can find users of Test::Exception, including the module author, on L<http://www.perlmonks.org/>. Feel free to ask questions on Test::Exception there.
408  
409 =item CPAN::Forum
410  
411 The CPAN Forum is a web forum for discussing Perl's CPAN modules. The Test::Exception forum can be found at L<http://www.cpanforum.com/dist/Test-Exception>.
412  
413 =item AnnoCPAN
414  
415 AnnoCPAN is a web site that allows community annotations of Perl module documentation. The Test::Exception annotations can be found at L<http://annocpan.org/~ADIE/Test-Exception/>.
416  
417 =back
418  
419  
420 =head1 TO DO
421  
422 If you think this module should do something that it doesn't (or does something that it shouldn't) please let me know.
423  
424 You can see my current to do list at L<http://adrianh.tadalist.com/lists/public/15421>, with an RSS feed of changes at L<http://adrianh.tadalist.com/lists/feed_public/15421>.
425  
426  
427 =head1 ACKNOWLEDGMENTS
428  
429 Thanks to chromatic and Michael G Schwern for the excellent Test::Builder, without which this module wouldn't be possible.
430  
431 Thanks to
432 Adam Kennedy,
433 Andy Lester,
434 Aristotle Pagaltzis,
435 Ben Prew,
436 Cees Hek,
437 Chris Dolan,
438 chromatic,
439 Curt Sampson,
440 David Cantrell,
441 David Golden,
442 David Tulloh,
443 David Wheeler,
444 J. K. O'Brien,
445 Janek Schleicher,
446 Jim Keenan,
447 Jos I. Boumans,
448 Joshua ben Jore,
449 Jost Krieger,
450 Mark Fowler,
451 Michael G Schwern,
452 Nadim Khemir,
453 Paul McCann,
454 Perrin Harkins,
455 Peter Rabbitson,
456 Peter Scott,
457 Ricardo Signes,
458 Rob Muhlestein,
459 Scott R. Godin,
460 Steve Purkis,
461 Steve,
462 Tim Bunce,
463 and various anonymous folk for comments, suggestions, bug reports and patches.
464  
465 =head1 AUTHOR
466  
467 Adrian Howard <adrianh@quietstars.com>
468  
469 If you can spare the time, please drop me a line if you find this module useful.
470  
471  
472 =head1 SEE ALSO
473  
474 =over 4
475  
476 =item L<http://del.icio.us/tag/Test::Exception>
477  
478 Delicious links on Test::Exception.
479  
480 =item L<Test::Fatal>
481  
482 A slightly different interface to testing exceptions, without overriding C<CORE::caller>.
483  
484 =item L<Test::Warnings> & L<Test::Warn> & L<Test::NoWarnings>
485  
486 Modules to help test warnings.
487  
488 =item L<Test::Builder>
489  
490 Support module for building test libraries.
491  
492 =item L<Test::Simple> & L<Test::More>
493  
494 Basic utilities for writing tests.
495  
496 =item L<http://qa.perl.org/test-modules.html>
497  
498 Overview of some of the many testing modules available on CPAN.
499  
500 =item L<http://del.icio.us/tag/perl+testing>
501  
502 Delicious links on perl testing.
503  
504 =back
505  
506  
507 =head1 LICENCE
508  
509 Copyright 2002-2007 Adrian Howard, All Rights Reserved.
510  
511 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
512  
513 =cut
514  
515 10 1;
↑ Summary