1Warn(3)               User Contributed Perl Documentation              Warn(3)
2
3
4

NAME

6       Test::Warn - Perl extension to test methods for warnings
7

SYNOPSIS

9         use Test::Warn;
10
11         warning_is    {foo(-dri => "/")} "Unknown Parameter 'dri'", "dri != dir gives warning";
12         warnings_are  {bar(1,1)} ["Width very small", "Height very small"];
13
14         warning_is    {add(2,2)} undef, "No warnings for calc 2+2"; # or
15         warnings_are  {add(2,2)} [],    "No warnings for calc 2+2"; # what reads better :-)
16
17         warning_like  {foo(-dri => "/")} qr/unknown param/i, "an unknown parameter test";
18         warnings_like {bar(1,1)} [qr/width.*small/i, qr/height.*small/i];
19
20         warning_is    {foo()} {carped => "didn't find the right parameters"};
21         warnings_like {foo()} [qr/undefined/,qr/undefined/,{carped => qr/no result/i}];
22
23         warning_like {foo(undef)}                 'uninitialized';
24         warning_like {bar(file => '/etc/passwd')} 'io';
25
26         warning_like {eval q/"$x"; $x;/}
27                      [qw/void uninitialized/],
28                      "some warnings at compile time";
29
30         warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
31

DESCRIPTION

33       A good style of Perl programming calls for a lot of diverse regression
34       tests.
35
36       This module provides a few convenience methods for testing warning
37       based code.
38
39       If you are not already familiar with the Test::More manpage now would
40       be the time to go take a look.
41
42   FUNCTIONS
43       warning_is BLOCK STRING, TEST_NAME
44           Tests that BLOCK gives the specified warning exactly once.  The
45           test fails if the BLOCK warns more than once or does not warn at
46           all.  If the string is undef, then the tests succeeds if the BLOCK
47           doesn't give any warning.  Another way to say that there are no
48           warnings in the block is "warnings_are {foo()} [], "no warnings"".
49
50           If you want to test for a warning given by Carp, you have to write
51           something like: "warning_is {carp "msg"} {carped => 'msg'}, "Test
52           for a carped warning"".  The test will fail if a "normal" warning
53           is found instead of a "carped" one.
54
55           Note: "warn "foo"" would print something like "foo at -e line 1".
56           This method ignores everything after the "at". Thus to match this
57           warning you would have to call "warning_is {warn "foo"} "foo", "Foo
58           succeeded"".  If you need to test for a warning at an exactly line,
59           try something like "warning_like {warn "foo"} qr/at XYZ.dat line
60           5/".
61
62           warning_is and warning_are are only aliases to the same method.  So
63           you also could write "warning_is {foo()} [], "no warning"" or
64           something similar.  I decided to give two methods the same name to
65           improve readability.
66
67           A true value is returned if the test succeeds, false otherwise.
68
69           The test name is optional, but recommended.
70
71       warnings_are BLOCK ARRAYREF, TEST_NAME
72           Tests to see that BLOCK gives exactly the specified warnings.  The
73           test fails if the warnings from BLOCK are not exactly the ones in
74           ARRAYREF.  If the ARRAYREF is equal to [], then the test succeeds
75           if the BLOCK doesn't give any warning.
76
77           Please read also the notes to warning_is as these methods are only
78           aliases.
79
80           If you want more than one test for carped warnings, try this:
81           "warnings_are {carp "c1"; carp "c2"} {carped =" ['c1','c2'];> or
82           "warnings_are {foo()} ["Warning 1", {carped =" ["Carp 1", "Carp
83           2"]}, "Warning 2"]>.  Note that "{carped =" ...}> must always be a
84           hash ref.
85
86       warning_like BLOCK REGEXP, TEST_NAME
87           Tests that BLOCK gives exactly one warning and it can be matched by
88           the given regexp.  If the string is undef, then the tests succeeds
89           if the BLOCK doesn't give any warning.
90
91           The REGEXP is matched against the whole warning line, which in
92           general has the form "WARNING at __FILE__ line __LINE__".  So you
93           can check for a warning in the file Foo.pm on line 5 with
94           "warning_like {bar()} qr/at Foo.pm line 5/, "Testname"".  I don't
95           know whether it's sensful to do such a test :-( However, you should
96           be prepared as a matching with 'at', 'file', '\d' or similar will
97           always pass.  Think to the qr/^foo/ if you want to test for warning
98           "foo something" in file foo.pl.
99
100           You can also write the regexp in a string as "/.../" instead of
101           using the qr/.../ syntax.  Note that the slashes are important in
102           the string, as strings without slashes are reserved for warning
103           categories (to match warning categories as can be seen in the
104           perllexwarn man page).
105
106           Similar to "warning_is", you can test for warnings via "carp" with:
107           "warning_like {bar()} {carped =" qr/bar called too early/i};>
108
109           Similar to "warning_is"/"warnings_are", "warning_like" and
110           "warnings_like" are only aliases to the same methods.
111
112           A true value is returned if the test succeeds, false otherwise.
113
114           The test name is optional, but recommended.
115
116       warning_like BLOCK STRING, TEST_NAME
117           Tests whether a BLOCK gives exactly one warning of the passed
118           category.  The categories are grouped in a tree, like it is
119           expressed in perllexwarn.  Note, that they have the hierarchical
120           structure from perl 5.8.0, wich has a little bit changed to 5.6.1
121           or earlier versions (You can access the internal used tree with
122           $Test::Warn::Categorization::tree, although I wouldn't recommend
123           it)
124
125           Thanks to the grouping in a tree, it's simple possible to test for
126           an 'io' warning, instead for testing for a
127           'closed|exec|layer|newline|pipe|unopened' warning.
128
129           Note, that warnings occuring at compile time, can only be catched
130           in an eval block. So
131
132             warning_like {eval q/"$x"; $x;/}
133                          [qw/void uninitialized/],
134                          "some warnings at compile time";
135
136           will work, while it wouldn't work without the eval.
137
138           Note, that it isn't possible yet, to test for own categories,
139           created with warnings::register.
140
141       warnings_like BLOCK ARRAYREF, TEST_NAME
142           Tests to see that BLOCK gives exactly the number of the specified
143           warnings and all the warnings have to match in the defined order to
144           the passed regexes.
145
146           Please read also the notes to warning_like as these methods are
147           only aliases.
148
149           Similar to "warnings_are", you can test for multiple warnings via
150           "carp" and for warning categories, too:
151
152             warnings_like {foo()}
153                           [qr/bar warning/,
154                            qr/bar warning/,
155                            {carped => qr/bar warning/i},
156                            'io'
157                           ],
158                           "I hope, you'll never have to write a test for so many warnings :-)";
159
160       warnings_exist BLOCK STRING|ARRAYREF, TEST_NAME
161           Same as warning_like, but will warn() all warnings that do not
162           match the supplied regex/category, instead of registering an error.
163           Use this test when you just want to make sure that specific
164           warnings were generated, and couldn't care less if other warnings
165           happened in the same block of code.
166
167             warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
168
169             warnings_exist {...} ['uninitialized'], "Expected warning is thrown";
170
171   EXPORT
172       "warning_is", "warnings_are", "warning_like", "warnings_like",
173       "warnings_exist" by default.
174

BUGS

176       Please note that warnings with newlines inside are making a lot of
177       trouble.  The only sensible way to handle them is to use are the
178       "warning_like" or "warnings_like" methods. Background for these
179       problems is that there is no really secure way to distinguish between
180       warnings with newlines and a tracing stacktrace.
181
182       If a method has it's own warn handler, overwriting $SIG{__WARN__}, my
183       test warning methods won't get these warnings.
184
185       The "warning_like BLOCK CATEGORY, TEST_NAME" method isn't extremely
186       tested.  Please use this calling style with higher attention and tell
187       me if you find a bug.
188

TODO

190       Improve this documentation.
191
192       The code has some parts doubled - especially in the test scripts.  This
193       is really awkward and must be changed.
194
195       Please feel free to suggest improvements.
196

SEE ALSO

198       Have a look to the similar modules: Test::Exception, Test::Trap.
199

THANKS

201       Many thanks to Adrian Howard, chromatic and Michael G. Schwern, who
202       have given me a lot of ideas.
203

AUTHOR

205       Janek Schleicher, <bigj AT kamelfreund.de>
206
208       Copyright 2002 by Janek Schleicher
209
210       Copyright 2007-2011 by Alexandr Ciornii, <http://chorny.net/>
211
212       This library is free software; you can redistribute it and/or modify it
213       under the same terms as Perl itself.
214
215
216
217perl v5.16.3                      2012-04-01                           Warn(3)
Impressum