.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Warn 3" .TH Warn 3 "2009-08-29" "perl v5.8.7" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::Warn \- Perl extension to test methods for warnings .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Test::Warn; \& \& warning_is {foo(\-dri => "/")} "Unknown Parameter \*(Aqdri\*(Aq", "dri != dir gives warning"; \& warnings_are {bar(1,1)} ["Width very small", "Height very small"]; \& \& warning_is {add(2,2)} undef, "No warning to calc 2+2"; # or \& warnings_are {add(2,2)} [], "No warning to calc 2+2"; # what reads better :\-) \& \& warning_like {foo(\-dri => "/")} qr/unknown param/i, "an unknown parameter test"; \& warnings_like {bar(1,1)} [qr/width.*small/i, qr/height.*small/i]; \& \& warning_is {foo()} {carped => "didn\*(Aqt found the right parameters"}; \& warnings_like {foo()} [qr/undefined/,qr/undefined/,{carped => qr/no result/i}]; \& \& warning_like {foo(undef)} \*(Aquninitialized\*(Aq; \& warning_like {bar(file => \*(Aq/etc/passwd\*(Aq)} \*(Aqio\*(Aq; \& \& warning_like {eval q/"$x"; $x;/} \& [qw/void uninitialized/], \& "some warnings at compile time"; \& \& warnings_exist {...} [qr/expected warning/], "Expected warning is thrown"; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" A good style of Perl programming calls for a lot of diverse regression tests. .PP This module provides a few convenience methods for testing warning based code. .PP If you are not already familiar with the Test::More manpage now would be the time to go take a look. .SS "\s-1FUNCTIONS\s0" .IX Subsection "FUNCTIONS" .IP "warning_is \s-1BLOCK\s0 \s-1STRING\s0, \s-1TEST_NAME\s0" 4 .IX Item "warning_is BLOCK STRING, TEST_NAME" Tests that \s-1BLOCK\s0 gives exactly the one specificated warning. The test fails if the \s-1BLOCK\s0 warns more then one times or doesn't warn. If the string is undef, then the tests succeeds if the \s-1BLOCK\s0 doesn't give any warning. Another way to say that there aren't any warnings in the block, is \f(CW\*(C`warnings_are {foo()} [], "no warnings in"\*(C'\fR. .Sp If you want to test for a warning given by carp, You have to write something like: \&\f(CW\*(C`warning_is {carp "msg"} {carped => \*(Aqmsg\*(Aq}, "Test for a carped warning"\*(C'\fR. The test will fail, if a \*(L"normal\*(R" warning is found instead of a \*(L"carped\*(R" one. .Sp Note: \f(CW\*(C`warn "foo"\*(C'\fR would print something like \f(CW\*(C`foo at \-e line 1\*(C'\fR. This method ignores everything after the at. That means, to match this warning you would have to call \f(CW\*(C`warning_is {warn "foo"} "foo", "Foo succeeded"\*(C'\fR. If you need to test for a warning at an exactly line, try better something like \f(CW\*(C`warning_like {warn "foo"} qr/at XYZ.dat line 5/\*(C'\fR. .Sp warning_is and warning_are are only aliases to the same method. So you also could write \&\f(CW\*(C`warning_is {foo()} [], "no warning"\*(C'\fR or something similar. I decided to give two methods to have some better readable method names. .Sp A true value is returned if the test succeeds, false otherwise. .Sp The test name is optional, but recommended. .IP "warnings_are \s-1BLOCK\s0 \s-1ARRAYREF\s0, \s-1TEST_NAME\s0" 4 .IX Item "warnings_are BLOCK ARRAYREF, TEST_NAME" Tests to see that \s-1BLOCK\s0 gives exactly the specificated warnings. The test fails if the \s-1BLOCK\s0 warns a different number than the size of the ARRAYREf would have expected. If the \s-1ARRAYREF\s0 is equal to [], then the test succeeds if the \s-1BLOCK\s0 doesn't give any warning. .Sp Please read also the notes to warning_is as these methods are only aliases. .Sp If you want more than one tests for carped warnings look that way: \&\f(CW\*(C`warnings_are {carp "c1"; carp "c2"} {carped =\*(C'\fR ['c1','c2'];> or \&\f(CW\*(C`warnings_are {foo()} ["Warning 1", {carped =\*(C'\fR [\*(L"Carp 1\*(R", \*(L"Carp 2\*(R"]}, \*(L"Warning 2\*(R"]>. Note that \f(CW\*(C`{carped =\*(C'\fR ...}> has always to be a hash ref. .IP "warning_like \s-1BLOCK\s0 \s-1REGEXP\s0, \s-1TEST_NAME\s0" 4 .IX Item "warning_like BLOCK REGEXP, TEST_NAME" Tests that \s-1BLOCK\s0 gives exactly one warning and it can be matched to the given regexp. If the string is undef, then the tests succeeds iff the \s-1BLOCK\s0 doesn't give any warning. .Sp The \s-1REGEXP\s0 is matched after the whole warn line, which consists in general of \*(L"\s-1WARNING\s0 at _\|_FILE_\|_ line _\|_LINE_\|_\*(R". So you can check for a warning in at File Foo.pm line 5 with \&\f(CW\*(C`warning_like {bar()} qr/at Foo.pm line 5/, "Testname"\*(C'\fR. I don't know whether it's sensful to do such a test :\-( However, you should be prepared as a matching with 'at', 'file', '\ed' or similar will always pass. Think to the qr/^foo/ if you want to test for warning \*(L"foo something\*(R" in file foo.pl. .Sp You can also write the regexp in a string as \*(L"/.../\*(R" instead of using the qr/.../ syntax. Note that the slashes are important in the string, as strings without slashes are reserved for warning categories (to match warning categories as can be seen in the perllexwarn man page). .Sp Similar to \f(CW\*(C`warning_is\*(C'\fR, you can test for warnings via \f(CW\*(C`carp\*(C'\fR with: \&\f(CW\*(C`warning_like {bar()} {carped =\*(C'\fR qr/bar called too early/i};> .Sp Similar to \f(CW\*(C`warning_is\*(C'\fR/\f(CW\*(C`warnings_are\*(C'\fR, \&\f(CW\*(C`warning_like\*(C'\fR and \f(CW\*(C`warnings_like\*(C'\fR are only aliases to the same methods. .Sp A true value is returned if the test succeeds, false otherwise. .Sp The test name is optional, but recommended. .IP "warning_like \s-1BLOCK\s0 \s-1STRING\s0, \s-1TEST_NAME\s0" 4 .IX Item "warning_like BLOCK STRING, TEST_NAME" Tests whether a \s-1BLOCK\s0 gives exactly one warning of the passed category. The categories are grouped in a tree, like it is expressed in perllexwarn. Note, that they have the hierarchical structure from perl 5.8.0, wich has a little bit changed to 5.6.1 or earlier versions (You can access the internal used tree with \f(CW$Test::Warn::Categorization::tree\fR, although I wouldn't recommend it) .Sp Thanks to the grouping in a tree, it's simple possible to test for an 'io' warning, instead for testing for a 'closed|exec|layer|newline|pipe|unopened' warning. .Sp Note, that warnings occuring at compile time, can only be catched in an eval block. So .Sp .Vb 3 \& warning_like {eval q/"$x"; $x;/} \& [qw/void uninitialized/], \& "some warnings at compile time"; .Ve .Sp will work, while it wouldn't work without the eval. .Sp Note, that it isn't possible yet, to test for own categories, created with warnings::register. .IP "warnings_like \s-1BLOCK\s0 \s-1ARRAYREF\s0, \s-1TEST_NAME\s0" 4 .IX Item "warnings_like BLOCK ARRAYREF, TEST_NAME" Tests to see that \s-1BLOCK\s0 gives exactly the number of the specificated warnings and all the warnings have to match in the defined order to the passed regexes. .Sp Please read also the notes to warning_like as these methods are only aliases. .Sp Similar to \f(CW\*(C`warnings_are\*(C'\fR, you can test for multiple warnings via \f(CW\*(C`carp\*(C'\fR and for warning categories, too: .Sp .Vb 7 \& warnings_like {foo()} \& [qr/bar warning/, \& qr/bar warning/, \& {carped => qr/bar warning/i}, \& \*(Aqio\*(Aq \& ], \& "I hope, you\*(Aqll never have to write a test for so many warnings :\-)"; .Ve .IP "warnings_exist \s-1BLOCK\s0 STRING|ARRAYREF, \s-1TEST_NAME\s0" 4 .IX Item "warnings_exist BLOCK STRING|ARRAYREF, TEST_NAME" Same as warning_like, but will \fIwarn()\fR all warnings that do not match the supplied regex/category, instead of registering an error. Use this test when you just want to make sure that specific warnings were generated, and couldn't care less if other warnings happened in the same block of code. .Sp .Vb 1 \& warnings_exist {...} [qr/expected warning/], "Expected warning is thrown"; \& \& warnings_exist {...} [\*(Aquninitialized\*(Aq], "Expected warning is thrown"; .Ve .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" \&\f(CW\*(C`warning_is\*(C'\fR, \&\f(CW\*(C`warnings_are\*(C'\fR, \&\f(CW\*(C`warning_like\*(C'\fR, \&\f(CW\*(C`warnings_like\*(C'\fR, \&\f(CW\*(C`warnings_exist\*(C'\fR by default. .SH "BUGS" .IX Header "BUGS" Please note that warnings with newlines inside are making a lot of trouble. The only sensible way to handle them is to use are the \f(CW\*(C`warning_like\*(C'\fR or \&\f(CW\*(C`warnings_like\*(C'\fR methods. Background for these problems is that there is no really secure way to distinguish between warnings with newlines and a tracing stacktrace. .PP If a method has it's own warn handler, overwriting \f(CW$SIG{_\|_WARN_\|_}\fR, my test warning methods won't get these warnings. .PP The \f(CW\*(C`warning_like BLOCK CATEGORY, TEST_NAME\*(C'\fR method isn't extremely tested. Please use this calling style with higher attention and tell me if you find a bug. .SH "TODO" .IX Header "TODO" Improve this documentation. .PP The code has some parts doubled \- especially in the test scripts. This is really awkward and has to be changed. .PP Please feel free to suggest me any improvements. .SH "SEE ALSO" .IX Header "SEE ALSO" Have a look to the similar Test::Exception module. Test::Trap .SH "THANKS" .IX Header "THANKS" Many thanks to Adrian Howard, chromatic and Michael G. Schwern, who have given me a lot of ideas. .SH "AUTHOR" .IX Header "AUTHOR" Janek Schleicher, .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2002 by Janek Schleicher .PP Copyright 2007\-2009 by Alexandr Ciornii, .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.