A good style of Perl programming calls for a lot of diverse regression tests.
This module provides a few convenience methods for testing warning based-code.
If you are not already familiar with the Test::More manpage now would be the time to go take a look.
FUNCTIONSwarning_isBLOCKSTRING,TEST_NAME
Tests that BLOCK gives the specified warning exactly once.
The test fails if the BLOCK warns more than once or does not warn at all. If the string is undef,
then the test succeeds if the BLOCK doesn't give any warning.
Another way to say that there are no warnings in the block is:
warnings_are {foo()} [], "no warnings"
If you want to test for a warning given by Carp you have to write something like:
warning_is {carp "msg"} {carped => 'msg'}, "Test for a carped warning";
The test will fail if a "normal" warning is found instead of a "carped" one.
Note: "warn "foo"" would print something like "foo at -e line 1". This method ignores everything
after the "at". Thus to match this warning you would have to call "warning_is {warn "foo"} "foo",
"Foo succeeded"". If you need to test for a warning at an exact line, try something like:
warning_like {warn "foo"} qr/at XYZ.dat line 5/
Warn messages with a trailing newline (like "warn "foo\n"") don't produce the "at -e line 1" message
by Perl. Up to Test::Warn 0.30 such warning weren't supported by "warning_is {warn "foo\n"}
"foo\n"". Starting with version 0.31 they are supported, but also marked as experimental.
"warning_is()" and "warnings_are()" are only aliases to the same method. So you also could write
"warning_is {foo()} [], "no warning"" or something similar.
I decided to give two methods the same name to improve readability.
A true value is returned if the test succeeds, false otherwise.
The test name is optional, but recommended.
warnings_areBLOCKARRAYREF,TEST_NAME
Tests to see that BLOCK gives exactly the specified warnings. The test fails if the warnings from
BLOCK are not exactly the ones in ARRAYREF. If the ARRAYREF is equal to "[]", then the test succeeds
if the BLOCK doesn't give any warning.
Please read also the notes to "warning_is()" as these methods are only aliases.
If you want more than one test for carped warnings, try this:
warnings_are {carp "c1"; carp "c2"} {carped => ['c1','c2'];
or
warnings_are {foo()} ["Warning 1", {carped => ["Carp 1", "Carp 2"]}, "Warning 2"];
Note that "{carped => ...}" must always be a hash ref.
warning_likeBLOCKREGEXP,TEST_NAME
Tests that BLOCK gives exactly one warning and it can be matched by the given regexp.
If the string is undef, then the tests succeeds if the BLOCK doesn't give any warning.
The REGEXP is matched against the whole warning line, which in general has the form "WARNING at
__FILE__ line __LINE__". So you can check for a warning in the file "Foo.pm" on line 5 with:
warning_like {bar()} qr/at Foo.pm line 5/, "Testname"
I don't know whether it makes sense to do such a test :-(
However, you should be prepared as a matching with 'at', 'file', '\d' or similar will always pass.
Consider "qr/^foo/" if you want to test for warning "foo something" in file foo.pl.
You can also write the regexp in a string as "/.../" 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).
Similar to "warning_is()" and "warnings_are()" you can test for warnings via "carp" with:
warning_like {bar()} {carped => qr/bar called too early/i};
Similar to "warning_is()" and "warnings_are()",
"warning_like()" and "warnings_like()" are only aliases to the same methods.
A true value is returned if the test succeeds, false otherwise.
The test name is optional, but recommended.
warning_likeBLOCKSTRING,TEST_NAME
Tests whether a BLOCK gives exactly one warning of the passed category.
The categories are grouped in a tree, like it is expressed in perllexwarn. Also see "BUGS AND
LIMITATIONS".
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.
Note, that warnings occurring at compile time can only be caught in an eval block. So
warning_like {eval q/"$x"; $x;/}
[qw/void uninitialized/],
"some warnings at compile time";
will work, while it wouldn't work without the eval.
Note, that it isn't possible yet, to test for own categories, created with warnings::register.
warnings_likeBLOCKARRAYREF,TEST_NAME
Tests to see that BLOCK gives exactly the number of the specified warnings, in the defined order.
Please read also the notes to "warning_like()" as these methods are only aliases.
Similar to "warnings_are()", you can test for multiple warnings via "carp" and for warning
categories, too:
warnings_like {foo()}
[qr/bar warning/,
qr/bar warning/,
{carped => qr/bar warning/i},
'io'
],
"I hope you'll never have to write a test for so many warnings :-)";
warnings_existBLOCKSTRING|ARRAYREF,TEST_NAME
Same as warning_like, but will "warn()" 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.
warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
warnings_exist {...} ['uninitialized'], "Expected warning is thrown";
EXPORT
"warning_is", "warnings_are", "warning_like", "warnings_like", "warnings_exist" by default.