0x1949 Team - FAZEMRX - MANAGER
Edit File: Test::Warn.3pm
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" 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 .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Warn 3pm" .TH Warn 3pm "2022-08-13" "perl v5.34.0" "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"; # whichever 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 "\fBwarning_is\fR \fI\s-1BLOCK STRING, TEST_NAME\s0\fR" 4 .IX Item "warning_is BLOCK STRING, TEST_NAME" Tests that \s-1BLOCK\s0 gives the specified warning exactly once. .Sp 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 test succeeds if the \s-1BLOCK\s0 doesn't give any warning. .Sp Another way to say that there are no warnings in the block is: .Sp .Vb 1 \& warnings_are {foo()} [], "no warnings" .Ve .Sp If you want to test for a warning given by Carp you have to write something like: .Sp .Vb 1 \& warning_is {carp "msg"} {carped => \*(Aqmsg\*(Aq}, "Test for a carped warning"; .Ve .Sp 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 exact line, try something like: .Sp .Vb 1 \& warning_like {warn "foo"} qr/at XYZ.dat line 5/ .Ve .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 \&\f(CW\*(C`warning_is()\*(C'\fR and \f(CW\*(C`warnings_are()\*(C'\fR 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. .Sp 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 "\fBwarnings_are\fR \fI\s-1BLOCK ARRAYREF, TEST_NAME\s0\fR" 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 \f(CW\*(C`[]\*(C'\fR, then the test succeeds if the \s-1BLOCK\s0 doesn't give any warning. .Sp Please read also the notes to \&\f(CW\*(C`warning_is()\*(C'\fR as these methods are only aliases. .Sp If you want more than one test for carped warnings, try this: .Sp .Vb 1 \& warnings_are {carp "c1"; carp "c2"} {carped => [\*(Aqc1\*(Aq,\*(Aqc2\*(Aq]; .Ve .Sp or .Sp .Vb 1 \& warnings_are {foo()} ["Warning 1", {carped => ["Carp 1", "Carp 2"]}, "Warning 2"]; .Ve .Sp Note that \f(CW\*(C`{carped => ...}\*(C'\fR must always be a hash ref. .IP "\fBwarning_like\fR \fI\s-1BLOCK REGEXP, TEST_NAME\s0\fR" 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. .Sp 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 \f(CW"WARNING at _\|_FILE_\|_ line _\|_LINE_\|_"\fR. So you can check for a warning in the file \f(CW\*(C`Foo.pm\*(C'\fR on line 5 with: .Sp .Vb 1 \& warning_like {bar()} qr/at Foo.pm line 5/, "Testname" .Ve .Sp I don't know whether it makes sense to do such a test :\-( .Sp However, you should be prepared as a matching with \f(CW\*(Aqat\*(Aq\fR, \f(CW\*(Aqfile\*(Aq\fR, \f(CW\*(Aq\ed\*(Aq\fR or similar will always pass. .Sp Consider \f(CW\*(C`qr/^foo/\*(C'\fR if you want to test for warning \f(CW"foo something"\fR in file \fIfoo.pl\fR. .Sp You can also write the regexp in a string as \f(CW"/.../"\fR instead of using the \f(CW\*(C`qr/.../\*(C'\fR syntax. .Sp 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 and \&\f(CW\*(C`warnings_are()\*(C'\fR you can test for warnings via \f(CW\*(C`carp\*(C'\fR with: .Sp .Vb 1 \& warning_like {bar()} {carped => qr/bar called too early/i}; .Ve .Sp Similar to \&\f(CW\*(C`warning_is()\*(C'\fR and \&\f(CW\*(C`warnings_are()\*(C'\fR, .Sp \&\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 "\fBwarning_like\fR \fI\s-1BLOCK STRING, TEST_NAME\s0\fR" 4 .IX Item "warning_like BLOCK STRING, TEST_NAME" Tests whether a \s-1BLOCK\s0 gives exactly one warning of the passed category. .Sp 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 warnings occurring at compile time 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, that it isn't possible yet, to test for own categories, created with warnings::register. .IP "\fBwarnings_like\fR \fI\s-1BLOCK ARRAYREF, TEST_NAME\s0\fR" 4 .IX Item "warnings_like BLOCK ARRAYREF, TEST_NAME" Tests to see that \s-1BLOCK\s0 gives exactly the number of the specified warnings, in the defined order. .Sp Please read also the notes to \&\f(CW\*(C`warning_like()\*(C'\fR 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 "\fBwarnings_exist\fR \fI\s-1BLOCK\s0 STRING|ARRAYREF, \s-1TEST_NAME\s0\fR" 4 .IX Item "warnings_exist BLOCK STRING|ARRAYREF, TEST_NAME" Same as warning_like, but will \f(CW\*(C`warn()\*(C'\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 \f(CW\*(C`qr/category_name/\*(C'\fR. In some case this works, like for category \f(CW\*(Aquninitialized\*(Aq\fR. For \f(CW\*(Aqutf8\*(Aq\fR it does not work. Perl does not have a list of warnings, so it is not possible to generate one for \f(CW\*(C`Test::Warn\*(C'\fR. .PP 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. .SS "\s-1XS\s0 warnings" .IX Subsection "XS warnings" As described in https://rt.cpan.org/Ticket/Display.html?id=42070&results=3c71d1b101a730e185691657f3b02f21 or https://github.com/hanfried/test\-warn/issues/1 \s-1XS\s0 warnings might not be caught. .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, <bigj \s-1AT\s0 kamelfreund.de> .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2002 by Janek Schleicher .PP Copyright 2007\-2014 by Alexandr Ciornii, <http://chorny.net/> .PP Copyright 2015\-2018 by Janek Schleicher .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.