Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Test::Exception.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Test::Exception 3"
132 .TH Test::Exception 3 "2008-02-16" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Test::Exception \- Test exception based code
135 .SH "SYNOPSIS"
136 .IX Header "SYNOPSIS"
137 .Vb 2
138 \&  use Test::More tests => 5;
139 \&  use Test::Exception;
140 .Ve
141 .PP
142 .Vb 1
143 \&  # or if you don't need Test::More
144 .Ve
145 .PP
146 .Vb 1
147 \&  use Test::Exception tests => 5;
148 .Ve
149 .PP
150 .Vb 1
151 \&  # then...
152 .Ve
153 .PP
154 .Vb 2
155 \&  # Check that the stringified exception matches given regex
156 \&  throws_ok { $foo\->method } qr/division by zero/, 'zero caught okay';
157 .Ve
158 .PP
159 .Vb 2
160 \&  # Check an exception of the given class (or subclass) is thrown
161 \&  throws_ok { $foo\->method } 'Error::Simple', 'simple error thrown';
162 .Ve
163 .PP
164 .Vb 3
165 \&  # all Test::Exceptions subroutines are guaranteed to preserve the state 
166 \&  # of $@ so you can do things like this after throws_ok and dies_ok
167 \&  like $@, 'what the stringified exception should look like';
168 .Ve
169 .PP
170 .Vb 2
171 \&  # Check that something died \- we do not care why
172 \&  dies_ok { $foo\->method } 'expecting to die';
173 .Ve
174 .PP
175 .Vb 2
176 \&  # Check that something did not die
177 \&  lives_ok { $foo\->method } 'expecting to live';
178 .Ve
179 .PP
180 .Vb 2
181 \&  # Check that a test runs without an exception
182 \&  lives_and { is $foo\->method, 42 } 'method is 42';
183 .Ve
184 .PP
185 .Vb 1
186 \&  # or if you don't like prototyped functions
187 .Ve
188 .PP
189 .Vb 7
190 \&  throws_ok( sub { $foo\->method }, qr/division by zero/,
191 \&      'zero caught okay' );
192 \&  throws_ok( sub { $foo\->method }, 'Error::Simple', 
193 \&      'simple error thrown' );
194 \&  dies_ok( sub { $foo\->method }, 'expecting to die' );
195 \&  lives_ok( sub { $foo\->method }, 'expecting to live' );
196 \&  lives_and( sub { is $foo\->method, 42 }, 'method is 42' );
197 .Ve
198 .SH "DESCRIPTION"
199 .IX Header "DESCRIPTION"
200 This module provides a few convenience methods for testing exception based code. It is built with 
201 Test::Builder and plays happily with Test::More and friends.
202 .PP
203 If you are not already familiar with Test::More now would be the time to go take a look.
204 .PP
205 You can specify the test plan when you \f(CW\*(C`use Test::Exception\*(C'\fR in the same way as \f(CW\*(C`use Test::More\*(C'\fR.
206 See Test::More for details.
207 .PP
208 \&\s-1NOTE:\s0 Test::Exception only checks for exceptions. It will ignore other methods of stopping 
209 program execution \- including \fIexit()\fR. If you have an \fIexit()\fR in evalled code Test::Exception
210 will not catch this with any of its testing functions.
211 .IP "\fBthrows_ok\fR" 4
212 .IX Item "throws_ok"
213 Tests to see that a specific exception is thrown. \fIthrows_ok()\fR has two forms: 
214 .Sp
215 .Vb 2
216 \&  throws_ok BLOCK REGEX, TEST_DESCRIPTION
217 \&  throws_ok BLOCK CLASS, TEST_DESCRIPTION
218 .Ve
219 .Sp
220 In the first form the test passes if the stringified exception matches the give regular expression. For example:
221 .Sp
222 .Vb 1
223 \&    throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file';
224 .Ve
225 .Sp
226 If your perl does not support \f(CW\*(C`qr//\*(C'\fR you can also pass a regex-like string, for example:
227 .Sp
228 .Vb 1
229 \&    throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file';
230 .Ve
231 .Sp
232 The second form of \fIthrows_ok()\fR test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example:
233 .Sp
234 .Vb 1
235 \&    throws_ok { $foo\->bar } "Error::Simple", 'simple error';
236 .Ve
237 .Sp
238 Will only pass if the \f(CW\*(C`bar\*(C'\fR method throws an Error::Simple exception, or a subclass of an Error::Simple exception.
239 .Sp
240 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:
241 .Sp
242 .Vb 2
243 \&    my $SIMPLE = Error::Simple\->new;
244 \&    throws_ok { $foo\->bar } $SIMPLE, 'simple error';
245 .Ve
246 .Sp
247 Should a \fIthrows_ok()\fR test fail it produces appropriate diagnostic messages. For example:
248 .Sp
249 .Vb 4
250 \&    not ok 3 \- simple error
251 \&    #     Failed test (test.t at line 48)
252 \&    # expecting: Error::Simple exception
253 \&    # found: normal exit
254 .Ve
255 .Sp
256 Like all other Test::Exception functions you can avoid prototypes by passing a subroutine explicitly:
257 .Sp
258 .Vb 1
259 \&    throws_ok( sub {$foo\->bar}, "Error::Simple", 'simple error' );
260 .Ve
261 .Sp
262 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
263 .Sp
264 A description of the exception being checked is used if no optional test description is passed.
265 .IP "\fBdies_ok\fR" 4
266 .IX Item "dies_ok"
267 Checks that a piece of code dies, rather than returning normally. For example:
268 .Sp
269 .Vb 4
270 \&    sub div {
271 \&        my ( $a, $b ) = @_;
272 \&        return $a / $b;
273 \&    };
274 .Ve
275 .Sp
276 .Vb 1
277 \&    dies_ok { div( 1, 0 ) } 'divide by zero detected';
278 .Ve
279 .Sp
280 .Vb 2
281 \&    # or if you don't like prototypes
282 \&    dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' );
283 .Ve
284 .Sp
285 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
286 .Sp
287 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 \fIthrows_ok()\fR.
288 .Sp
289 The test description is optional, but recommended. 
290 .IP "\fBlives_ok\fR" 4
291 .IX Item "lives_ok"
292 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:
293 .Sp
294 .Vb 7
295 \&    sub read_file {
296 \&        my $file = shift;
297 \&        local $/;
298 \&        open my $fh, '<', $file or die "open failed ($!)\en";
299 \&        $file = <FILE>;
300 \&        return $file;
301 \&    };
302 .Ve
303 .Sp
304 .Vb 2
305 \&    my $file;
306 \&    lives_ok { $file = read_file('test.txt') } 'file read';
307 .Ve
308 .Sp
309 .Vb 2
310 \&    # or if you don't like prototypes
311 \&    lives_ok( sub { $file = read_file('test.txt') }, 'file read' );
312 .Ve
313 .Sp
314 Should a \fIlives_ok()\fR test fail it produces appropriate diagnostic messages. For example:
315 .Sp
316 .Vb 3
317 \&    not ok 1 \- file read
318 \&    #     Failed test (test.t at line 15)
319 \&    # died: open failed (No such file or directory)
320 .Ve
321 .Sp
322 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
323 .Sp
324 The test description is optional, but recommended. 
325 .IP "\fBlives_and\fR" 4
326 .IX Item "lives_and"
327 Run a test that may throw an exception. For example, instead of doing:
328 .Sp
329 .Vb 3
330 \&  my $file;
331 \&  lives_ok { $file = read_file('answer.txt') } 'read_file worked';
332 \&  is $file, "42", 'answer was 42';
333 .Ve
334 .Sp
335 You can use \fIlives_and()\fR like this:
336 .Sp
337 .Vb 3
338 \&  lives_and { is read_file('answer.txt'), "42" } 'answer is 42';
339 \&  # or if you don't like prototypes
340 \&  lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42');
341 .Ve
342 .Sp
343 Which is the same as doing
344 .Sp
345 .Vb 1
346 \&  is read_file('answer.txt'), "42\en", 'answer is 42';
347 .Ve
348 .Sp
349 unless \f(CW\*(C`read_file('answer.txt')\*(C'\fR dies, in which case you get the same kind of error as \fIlives_ok()\fR
350 .Sp
351 .Vb 3
352 \&  not ok 1 \- answer is 42
353 \&  #     Failed test (test.t at line 15)
354 \&  # died: open failed (No such file or directory)
355 .Ve
356 .Sp
357 A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
358 .Sp
359 The test description is optional, but recommended.
360 .SH "SKIPPING TEST::EXCEPTION TESTS"
361 .IX Header "SKIPPING TEST::EXCEPTION TESTS"
362 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:
363 .PP
364 .Vb 3
365 \&  use strict;
366 \&  use warnings;
367 \&  use Test::More;
368 .Ve
369 .PP
370 .Vb 4
371 \&  BEGIN {
372 \&      eval "use Test::Exception";
373 \&      plan skip_all => "Test::Exception needed" if $@;
374 \&  }
375 .Ve
376 .PP
377 .Vb 2
378 \&  plan tests => 2;
379 \&  # ... tests that need Test::Exception ...
380 .Ve
381 .PP
382 Note that we load Test::Exception in a \f(CW\*(C`BEGIN\*(C'\fR block ensuring that the subroutine prototypes are in place before the rest of the test script is compiled.
383 .SH "BUGS"
384 .IX Header "BUGS"
385 There are some edge cases in Perl's exception handling where Test::Exception will miss exceptions
386 thrown in \s-1DESTROY\s0 blocks. See the \s-1RT\s0 bug <http://rt.cpan.org/Ticket/Display.html?id=24678> for
387 details, along with the t/edge\-cases.t in the distribution test suite. These will be addressed in
388 a future Test::Exception release.
389 .PP
390 If you find any more bugs please let me know by e\-mail, or report the problem with 
391 <http://rt.cpan.org/>.
392 .SH "COMMUNITY"
393 .IX Header "COMMUNITY"
394 .IP "perl-qa" 4
395 .IX Item "perl-qa"
396 If you are interested in testing using Perl I recommend you visit <http://qa.perl.org/> and join the excellent perl-qa mailing list. See <http://lists.perl.org/showlist.cgi?name=perl\-qa> for details on how to subscribe.
397 .IP "perlmonks" 4
398 .IX Item "perlmonks"
399 You can find users of Test::Exception, including the module author, on  <http://www.perlmonks.org/>. Feel free to ask questions on Test::Exception there.
400 .IP "CPAN::Forum" 4
401 .IX Item "CPAN::Forum"
402 The \s-1CPAN\s0 Forum is a web forum for discussing Perl's \s-1CPAN\s0 modules.   The Test::Exception forum can be found at <http://www.cpanforum.com/dist/Test\-Exception>.
403 .IP "AnnoCPAN" 4
404 .IX Item "AnnoCPAN"
405 AnnoCPAN is a web site that allows community annotations of Perl module documentation. The Test::Exception annotations can be found at <http://annocpan.org/~ADIE/Test\-Exception/>.
406 .SH "TO DO"
407 .IX Header "TO DO"
408 If you think this module should do something that it doesn't (or does something that it shouldn't) please let me know.
409 .PP
410 You can see my current to do list at <http://adrianh.tadalist.com/lists/public/15421>, with an \s-1RSS\s0 feed of changes at <http://adrianh.tadalist.com/lists/feed_public/15421>.
411 .SH "ACKNOWLEDGMENTS"
412 .IX Header "ACKNOWLEDGMENTS"
413 Thanks to chromatic and Michael G Schwern for the excellent Test::Builder, without which this module wouldn't be possible.
414 .PP
415 Thanks to 
416 Adam Kennedy,
417 Andy Lester, 
418 Aristotle Pagaltzis, 
419 Ben Prew, 
420 Cees Hek,
421 Chris Dolan,
422 chromatic, 
423 Curt Sampson,
424 David Cantrell,
425 David Golden, 
426 David Wheeler, 
427 Janek Schleicher,
428 Jim Keenan, 
429 Jos I. Boumans, 
430 Joshua ben Jore,
431 Jost Krieger,
432 Mark Fowler, 
433 Michael G Schwern, 
434 Nadim Khemir,
435 Paul McCann,
436 Perrin Harkins, 
437 Peter Scott, 
438 Rob Muhlestein 
439 Scott R. Godin,
440 Steve Purkis,
441 Steve, 
442 Tim Bunce,
443 and various anonymous folk for comments, suggestions, bug reports and patches.
444 .SH "AUTHOR"
445 .IX Header "AUTHOR"
446 Adrian Howard <adrianh@quietstars.com>
447 .PP
448 If you can spare the time, please drop me a line if you find this module useful.
449 .SH "SEE ALSO"
450 .IX Header "SEE ALSO"
451 .IP "<http://del.icio.us/tag/Test::Exception>" 4
452 .IX Item "<http://del.icio.us/tag/Test::Exception>"
453 Delicious links on Test::Exception.
454 .IP "Test::Warn & Test::NoWarnings" 4
455 .IX Item "Test::Warn & Test::NoWarnings"
456 Modules to help test warnings.
457 .IP "Test::Builder" 4
458 .IX Item "Test::Builder"
459 Support module for building test libraries.
460 .IP "Test::Simple & Test::More" 4
461 .IX Item "Test::Simple & Test::More"
462 Basic utilities for writing tests.
463 .IP "<http://qa.perl.org/test\-modules.html>" 4
464 .IX Item "<http://qa.perl.org/test-modules.html>"
465 Overview of some of the many testing modules available on \s-1CPAN\s0.
466 .IP "<http://del.icio.us/tag/perl+testing>" 4
467 .IX Item "<http://del.icio.us/tag/perl+testing>"
468 Delicious links on perl testing.
469 .SH "LICENCE"
470 .IX Header "LICENCE"
471 Copyright 2002\-2007 Adrian Howard, All Rights Reserved.
472 .PP
473 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.