.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" 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" '' . ds C` . ds C' '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 >0, 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. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Warn 3pm" .TH Warn 3pm "2016-12-25" "perl v5.24.1" "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 warnings for calc 2+2"; # or \& warnings_are {add(2,2)} [], "No warnings for 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 find 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 STRING, TEST_NAME\s0" 4 .IX Item "warning_is BLOCK STRING, TEST_NAME" Tests that \s-1BLOCK\s0 gives the specified warning exactly once. The test fails if the \s-1BLOCK\s0 warns more than once or does not warn at all. 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 are no warnings in the block is \f(CW\*(C`warnings_are {foo()} [], "no warnings"\*(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 \*(L"at\*(R". Thus 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 something like \f(CW\*(C`warning_like {warn "foo"} qr/at XYZ.dat line 5/\*(C'\fR. .Sp Warn messages with a trailing newline (like \f(CW\*(C`warn "foo\en"\*(C'\fR) don't produce the \f(CW\*(C`at \-e line 1\*(C'\fR message by Perl. Up to Test::Warn 0.30 such warning weren't supported by \f(CW\*(C`warning_is {warn "foo\en"} "foo\en"\*(C'\fR. Starting with version 0.31 they are supported, but also marked as experimental. .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 the same name to improve readability. .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 ARRAYREF, TEST_NAME\s0" 4 .IX Item "warnings_are BLOCK ARRAYREF, TEST_NAME" Tests to see that \s-1BLOCK\s0 gives exactly the specified warnings. The test fails if the warnings from \s-1BLOCK\s0 are not exactly the ones in \s-1ARRAYREF.\s0 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 test for carped warnings, try this: \&\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 ...}> must always be a hash ref. .IP "warning_like \s-1BLOCK REGEXP, TEST_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 by the given regexp. If the string is undef, then the tests succeeds if the \s-1BLOCK\s0 doesn't give any warning. .Sp The \s-1REGEXP\s0 is matched against the whole warning line, which in general has the form \*(L"\s-1WARNING\s0 at _\|_FILE_\|_ line _\|_LINE_\|_\*(R". So you can check for a warning in the file Foo.pm on line 5 with \&\f(CW\*(C`warning_like {bar()} qr/at Foo.pm line 5/, "Testname"\*(C'\fR. Perhaps it is not sensible to perform such a test; however, you should be aware that matching on a sweeping regular expression or similar will always pass. Consider 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 As with \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 STRING, TEST_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. Also see \*(L"\s-1BUGS AND LIMITATIONS\*(R"\s0. .Sp Thanks to the grouping in a tree, it's possible to test simply for an 'io' warning, instead of testing for a 'closed|exec|layer|newline|pipe|unopened' warning. .Sp Note that compile-time warnings can only be caught 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 also that it isn't yet possible to test for categories you created yourself with \f(CW\*(C`warnings::register\*(C'\fR. .IP "warnings_like \s-1BLOCK ARRAYREF, TEST_NAME\s0" 4 .IX Item "warnings_like BLOCK ARRAYREF, TEST_NAME" Tests to see that \s-1BLOCK\s0 gives exactly the number of the specified 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 AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" Category check is done as qr/category_name/. In some case this works, like for category 'uninitialized'. For 'utf8' it does not work. Perl does not have a list of warnings, so it is not possible to generate one for Test::Warn. If you want to add a warning to a category, send a pull request. Modifications should be done to \f(CW%warnings_in_category\fR. You should look into perl source to check how warning is looking exactly. .PP Please note that warnings with newlines inside are very awkward. The only sensible way to handle them is to use the \f(CW\*(C`warning_like\*(C'\fR or \&\f(CW\*(C`warnings_like\*(C'\fR methods. The background is that there is no really safe way to distinguish between warnings with newlines and a stacktrace. .PP If a method has its 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 fully tested. Please take note if you use this this calling style, and report any bugs you find. .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 must be changed. .PP Please feel free to suggest 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\-2014 by Alexandr Ciornii, .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.