blib/lib/Test/Warn.pm

filter: Statementstmt 98.1% 156 / 159 Branchbran 89.5% 43 / 48 Conditioncond 95.2% 20 / 21 MC/DCmcdc 92.3% 12 / 13 Subroutinesub 100% 24 / 24 Podpod 100% 3 / 3 totaltotal 96.2% 258 / 268 CC 37 SCAR 11.9
CC
37
Cov
96%
_diag_found_warning
5.1
_diag_exp_warning
5.1
_canonical_exp_warning
4.0
CRAP 3.3
↑ Summary
1 =head1 NAME
2  
3 Test::Warn - Perl extension to test methods for warnings
4  
5 =head1 SYNOPSIS
6  
7 use Test::Warn;
8  
9 warning_is {foo(-dri => "/")} "Unknown Parameter 'dri'", "dri != dir gives warning";
10 warnings_are {bar(1,1)} ["Width very small", "Height very small"];
11  
12 warning_is {add(2,2)} undef, "No warnings for calc 2+2"; # or
13 warnings_are {add(2,2)} [], "No warnings for calc 2+2"; # whichever reads better :-)
14  
15 warning_like {foo(-dri => "/")} qr/unknown param/i, "an unknown parameter test";
16 warnings_like {bar(1,1)} [qr/width.*small/i, qr/height.*small/i];
17  
18 warning_is {foo()} {carped => "didn't find the right parameters"};
19 warnings_like {foo()} [qr/undefined/,qr/undefined/,{carped => qr/no result/i}];
20  
21 warning_like {foo(undef)} 'uninitialized';
22 warning_like {bar(file => '/etc/passwd')} 'io';
23  
24 warning_like {eval q/"$x"; $x;/}
25 [qw/void uninitialized/],
26 "some warnings at compile time";
27  
28 warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
29  
30 =head1 DESCRIPTION
31  
32 A good style of Perl programming calls for a lot of diverse regression tests.
33  
34 This module provides a few convenience methods for testing warning based-code.
35  
36 If you are not already familiar with the L<Test::More> manpage
37 now would be the time to go take a look.
38  
39 =head2 FUNCTIONS
40  
41 =over 4
42  
43 =item B<warning_is> I<BLOCK STRING, TEST_NAME>
44  
45 Tests that BLOCK gives the specified warning exactly once.
46  
47 The test fails if the BLOCK warns more than once or does not warn at all.
48 If the string is undef, then the test succeeds if the BLOCK doesn't
49 give any warning.
50  
51 Another way to say that there are no warnings in the block
52 is:
53  
54 warnings_are {foo()} [], "no warnings"
55  
56 If you want to test for a warning given by Carp
57 you have to write something like:
58  
59 warning_is {carp "msg"} {carped => 'msg'}, "Test for a carped warning";
60  
61 The test will fail if a "normal" warning is found instead of a "carped" one.
62  
63 Note: C<warn "foo"> would print something like C<foo at -e line 1>.
64 This method ignores everything after the "at". Thus to match this warning
65 you would have to call C<< warning_is {warn "foo"} "foo", "Foo succeeded" >>.
66 If you need to test for a warning at an exact line,
67 try something like:
68  
69 warning_like {warn "foo"} qr/at XYZ.dat line 5/
70  
71 Warn messages with a trailing newline (like C<warn "foo\n">) don't produce the C<at -e line 1> message by Perl.
72 Up to Test::Warn 0.30 such warning weren't supported by C<< warning_is {warn "foo\n"} "foo\n" >>.
73 Starting with version 0.31 they are supported, but also marked as experimental.
74  
75 L<C<warning_is()>|/warning_is-BLOCK-STRING-TEST_NAME> and L<C<warnings_are()>|/warnings_are-BLOCK-ARRAYREF-TEST_NAME>
76 are only aliases to the same method. So you also could write
77 C<< warning_is {foo()} [], "no warning" >> or something similar.
78  
79 I decided to give two methods the same name to improve readability.
80  
81 A true value is returned if the test succeeds, false otherwise.
82  
83 The test name is optional, but recommended.
84  
85 =item B<warnings_are> I<BLOCK ARRAYREF, TEST_NAME>
86  
87 Tests to see that BLOCK gives exactly the specified warnings.
88 The test fails if the warnings from BLOCK are not exactly the ones in ARRAYREF.
89 If the ARRAYREF is equal to C<< [] >>,
90 then the test succeeds if the BLOCK doesn't give any warning.
91  
92 Please read also the notes to
93 L<C<warning_is()>|/warning_is-BLOCK-STRING-TEST_NAME>
94 as these methods are only aliases.
95  
96 If you want more than one test for carped warnings, try this:
97  
98 warnings_are {carp "c1"; carp "c2"} {carped => ['c1','c2'];
99  
100 or
101  
102 warnings_are {foo()} ["Warning 1", {carped => ["Carp 1", "Carp 2"]}, "Warning 2"];
103  
104 Note that C<< {carped => ...} >> must always be a hash ref.
105  
106 =item B<warning_like> I<BLOCK REGEXP, TEST_NAME>
107  
108 Tests that BLOCK gives exactly one warning and it can be matched by
109 the given regexp.
110  
111 If the string is undef, then the tests succeeds if the BLOCK doesn't
112 give any warning.
113  
114 The REGEXP is matched against the whole warning line,
115 which in general has the form C<< "WARNING at __FILE__ line __LINE__" >>.
116 So you can check for a warning in the file C<Foo.pm> on line 5 with:
117  
118 warning_like {bar()} qr/at Foo.pm line 5/, "Testname"
119  
120 I don't know whether it makes sense to do such a test :-(
121  
122 However, you should be prepared as a matching with C<'at'>, C<'file'>, C<'\d'>
123 or similar will always pass.
124  
125 Consider C<< qr/^foo/ >> if you want to test for warning C<"foo something"> in file F<foo.pl>.
126  
127 You can also write the regexp in a string as C<"/.../">
128 instead of using the C<< qr/.../ >> syntax.
129  
130 Note that the slashes are important in the string,
131 as strings without slashes are reserved for warning categories
132 (to match warning categories as can be seen in the perllexwarn man page).
133  
134 Similar to
135 L<< C<warning_is()>|/warning_is-BLOCK-STRING-TEST_NAME >> and
136 L<< C<warnings_are()>|/warnings_are-BLOCK-ARRAYREF-TEST_NAME >>
137 you can test for warnings via C<carp> with:
138  
139 warning_like {bar()} {carped => qr/bar called too early/i};
140  
141 Similar to
142 L<< C<warning_is()>|/warning_is-BLOCK-STRING-TEST_NAME >> and
143 L<< C<warnings_are()>|/warnings_are-BLOCK-ARRAYREF-TEST_NAME >>,
144  
145 L<< C<warning_like()>|/warning_like-BLOCK-REGEXP-TEST_NAME >> and
146 L<< C<warnings_like()>|/warnings_like-BLOCK-ARRAYREF-TEST_NAME >>
147 are only aliases to the same methods.
148  
149 A true value is returned if the test succeeds, false otherwise.
150  
151 The test name is optional, but recommended.
152  
153 =item B<warning_like> I<BLOCK STRING, TEST_NAME>
154  
155 Tests whether a BLOCK gives exactly one warning of the passed category.
156  
157 The categories are grouped in a tree,
158 like it is expressed in L<perllexwarn>.
159 Also see L</BUGS AND LIMITATIONS>.
160  
161  
162 Thanks to the grouping in a tree,
163 it's possible to test simply for an 'io' warning,
164 instead of testing for a 'closed|exec|layer|newline|pipe|unopened' warning.
165  
166 Note, that warnings occurring at compile time
167 can only be caught in an eval block. So
168  
169 warning_like {eval q/"$x"; $x;/}
170 [qw/void uninitialized/],
171 "some warnings at compile time";
172  
173 will work, while it wouldn't work without the eval.
174  
175 Note, that it isn't possible yet,
176 to test for own categories,
177 created with L<warnings::register>.
178  
179 =item B<warnings_like> I<BLOCK ARRAYREF, TEST_NAME>
180  
181 Tests to see that BLOCK gives exactly the number of the specified
182 warnings, in the defined order.
183  
184 Please read also the notes to
185 L<< C<warning_like()>|/warning_like-BLOCK-REGEXP-TEST_NAME >>
186 as these methods are only aliases.
187  
188 Similar to
189 L<< C<warnings_are()>|/warnings_are-BLOCK-ARRAYREF-TEST_NAME >>,
190 you can test for multiple warnings via C<carp>
191 and for warning categories, too:
192  
193 warnings_like {foo()}
194 [qr/bar warning/,
195 qr/bar warning/,
196 {carped => qr/bar warning/i},
197 'io'
198 ],
199 "I hope you'll never have to write a test for so many warnings :-)";
200  
201 =item B<warnings_exist> I<BLOCK STRING|ARRAYREF, TEST_NAME>
202  
203 Same as warning_like, but will C<< warn() >> all warnings that do not match the supplied regex/category,
204 instead of registering an error. Use this test when you just want to make sure that specific
205 warnings were generated, and couldn't care less if other warnings happened in the same block
206 of code.
207  
208 warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
209  
210 warnings_exist {...} ['uninitialized'], "Expected warning is thrown";
211  
212 =back
213  
214 =head2 EXPORT
215  
216 C<warning_is>,
217 C<warnings_are>,
218 C<warning_like>,
219 C<warnings_like>,
220 C<warnings_exist> by default.
221  
222 =head1 BUGS AND LIMITATIONS
223  
224 Category check is done as C<< qr/category_name/ >>. In some case this works, like for
225 category C<'uninitialized'>. For C<'utf8'> it does not work. Perl does not have a list
226 of warnings, so it is not possible to generate one for C<Test::Warn>.
227  
228 If you want to add a warning to a category, send a pull request. Modifications
229 should be done to C<< %warnings_in_category >>. You should look into perl source to check
230 how warning is looking exactly.
231  
232 Please note that warnings with newlines inside are very awkward.
233 The only sensible way to handle them is to use the C<warning_like> or
234 C<warnings_like> methods. The background is that there is no
235 really safe way to distinguish between warnings with newlines and a
236 stacktrace.
237  
238 If a method has its own warn handler,
239 overwriting C<$SIG{__WARN__}>,
240 my test warning methods won't get these warnings.
241  
242 The C<warning_like BLOCK CATEGORY, TEST_NAME> method isn't fully tested.
243 Please take note if you use this this calling style,
244 and report any bugs you find.
245  
246 =head2 XS warnings
247  
248 As described in https://rt.cpan.org/Ticket/Display.html?id=42070&results=3c71d1b101a730e185691657f3b02f21 or https://github.com/hanfried/test-warn/issues/1 XS warnings might not be caught.
249  
250 =head1 SEE ALSO
251  
252 Have a look to the similar L<Test::Exception> module. L<Test::Trap>
253  
254 =head1 THANKS
255  
256 Many thanks to Adrian Howard, chromatic and Michael G. Schwern,
257 who have given me a lot of ideas.
258  
259 =head1 AUTHOR
260  
261 Janek Schleicher, E<lt>bigj AT kamelfreund.deE<gt>
262  
263 =head1 COPYRIGHT AND LICENSE
264  
265 Copyright 2002 by Janek Schleicher
266  
267 Copyright 2007-2014 by Alexandr Ciornii, L<http://chorny.net/>
268  
269 Copyright 2015-2018 by Janek Schleicher
270  
271 This library is free software; you can redistribute it and/or modify
272 it under the same terms as Perl itself.
273  
274 =cut
275  
276  
277 package Test::Warn;
278  
279 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
280 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
282  
283 7 ▶use Sub::Uplevel 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 7 our $VERSION = '0.37';
286  
287 7 require Exporter;
288  
289 7 our @ISA = qw(Exporter);
290  
291 7 our %EXPORT_TAGS = ( 'all' => [ qw(
292 @EXPORT
293 ) ] );
294  
295 7 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
296  
297 7 our @EXPORT = qw(
298 warning_is warnings_are
299 warning_like warnings_like
300 warnings_exist
301 );
302  
303 7 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
304 7 my $Tester = Test::Builder->new;
305  
306 {
307 7 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
308 7 *warning_is = *warnings_are;
309 7 *warning_like = *warnings_like;
310 }
311  
312 281 ▶sub warnings_are (&$;$) {
Subroutine warnings_are: called CC=3 SCAR=11.0
Pod documented
313 281 my $block = shift;
314 281 ▶ my @exp_warning = map {_canonical_exp_warning($_)}
Condition100%2/2
shift() || []
l!l
2774
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
315 _to_array_if_necessary( shift() || [] );
316 281 my $testname = shift;
317 281 my @got_warning = ();
318 940 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
319 940 my ($called_from) = caller(0); # to find out Carping methods
320 940 push @got_warning, _canonical_got_warning($called_from, shift());
321 281 };
322 281 uplevel 1,$block;
323 281 my $ok = _cmp_is( \@got_warning, \@exp_warning );
324 281 $Tester->ok( $ok, $testname );
325 281 ▶ $ok or _diag_found_warning(@got_warning),
Branch: 1 branch
BranchTrueFalseTotal
$ok or _diag_found_warning(@got_warning), _diag_exp_warning(@exp_warning) 122 159 281
326 _diag_exp_warning(@exp_warning);
327 281 return $ok;
328 }
329  
330  
331 547 ▶sub warnings_like (&$;$) {
Subroutine warnings_like: called CC=3 SCAR=11.0
Pod documented
332 547 my $block = shift;
333 547 ▶ my @exp_warning = map {_canonical_exp_warning($_)}
Condition100%2/2
shift() || []
l!l
5398
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
334 _to_array_if_necessary( shift() || [] );
335 547 my $testname = shift;
336 547 my @got_warning = ();
337 1856 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
338 1856 my ($called_from) = caller(0); # to find out Carping methods
339 1856 push @got_warning, _canonical_got_warning($called_from, shift());
340 547 };
341 547 uplevel 1,$block;
342 547 my $ok = _cmp_like( \@got_warning, \@exp_warning );
343 547 $Tester->ok( $ok, $testname );
344 547 ▶ $ok or _diag_found_warning(@got_warning),
Branch: 1 branch
BranchTrueFalseTotal
$ok or _diag_found_warning(@got_warning), _diag_exp_warning(@exp_warning) 230 317 547
345 _diag_exp_warning(@exp_warning);
346 547 return $ok;
347 }
348  
349 7 ▶sub warnings_exist (&$;$) {
Subroutine warnings_exist: called CC=3 SCAR=11.0
Pod documented
350 7 my $block = shift;
351 7 ▶ my @exp_warning = map {_canonical_exp_warning($_)}
Condition50%1/2
shift() || []
l!l
70
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
352 _to_array_if_necessary( shift() || [] );
353 7 my $testname = shift;
354 7 my @got_warning = ();
355 7 my $exp_idx = 0;
356 12 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
357 12 my ($called_from) = caller(0); # to find out Carping methods
358 12 my $wrn_text=shift;
359 12 my $wrn_rec=_canonical_got_warning($called_from, $wrn_text);
360 12 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($exp_idx < @exp_warning and _cmp_got_to_exp_warning_like($wrn_rec, $exp_warning[$exp_idx])) 6 6 12
Condition100%3/3
$exp_idx < @exp_warning and _cmp_got_to_exp_warning_like($wrn_rec, $exp_warning[$exp_idx])
!ll&&!rl&&r
336
MC/DC100%2/2
$exp_idx < @exp_warning and _cmp_got_to_exp_warning_like($wrn_rec, $exp_warning[$exp_idx])
$exp_idx < @exp_warning _cmp_got_to_exp_warning_like($wrn_rec, $exp_warning[$exp_idx])
Truth table100%3/3
A and B
A= $exp_idx < @exp_warningB= _cmp_got_to_exp_warning_like($wrn_rec, $exp_warning[$exp_idx])
ABresult
0X0
100
111
361 $exp_idx < @exp_warning and
362 _cmp_got_to_exp_warning_like($wrn_rec,$exp_warning[$exp_idx])
363 ) {
364 6 push @got_warning, $wrn_rec;
365 6 $exp_idx++;
366 6 return;
367 }
368 6 warn $wrn_text;
369 7 };
370 7 uplevel 1,$block;
371 7 my $ok = _cmp_like( \@got_warning, \@exp_warning );
372 7 $Tester->ok( $ok, $testname );
373 7 ▶ $ok or _diag_found_warning(@got_warning),
Branch: 1 branch
BranchTrueFalseTotal
$ok or _diag_found_warning(@got_warning), _diag_exp_warning(@exp_warning) 5 2 7
374 _diag_exp_warning(@exp_warning);
375 7 return $ok;
376 }
377  
378  
379 835 ▶sub _to_array_if_necessary {
Subroutine _to_array_if_necessary: called CC=2 SCAR=6.9
380 835 ▶ return (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 528 307 835
381 }
382  
383 2808 ▶sub _canonical_got_warning {
Subroutine _canonical_got_warning: called CC=2 SCAR=6.9
384 2808 my ($called_from, $msg) = @_;
385 2808 ▶ my $warn_kind = $called_from eq 'Carp' ? 'carped' : 'warn';
Branch: 1 branch
BranchTrueFalseTotal
$called_from eq 'Carp' ? : 2080 728 2808
386 2808 my @warning_stack = split /\n/, $msg; # some stuff of uplevel is included
387 2808 return {$warn_kind => $warning_stack[0]}; # return only the real message
388 }
389  
390 1794 ▶sub _canonical_exp_warning {
Subroutine _canonical_exp_warning: called CC=4 SCAR=13.9
391 1794 my ($exp) = @_;
392 1794 ▶ if (ref($exp) eq 'HASH') { # could be {carped => ...}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp eq "HASH") 1088 706 1794
393 1088 ▶ my $to_carp = $exp->{carped} or return; # undefined message are ignored
Branch: 1 branch
BranchTrueFalseTotal
my $to_carp = $exp->{"carped"} or return 1076 12 1088
394 return (ref($to_carp) eq 'ARRAY') # is {carped => [ ..., ...] }
395 1076 ▶ ? map({ {carped => $_} } grep {defined $_} @$to_carp)
Branch: 1 branch
BranchTrueFalseTotal
ref $to_carp eq 'ARRAY' ? : 390 686 1076
396 : +{carped => $to_carp};
397 }
398 706 return {warn => $exp};
399 }
400  
401 503 ▶sub _cmp_got_to_exp_warning {
Subroutine _cmp_got_to_exp_warning: called CC=4 SCAR=13.9
402 503 my ($got_kind, $got_msg) = %{ shift() };
403 503 my ($exp_kind, $exp_msg) = %{ shift() };
404 503 ▶ return 0 if ($got_kind eq 'warn') && ($exp_kind eq 'carped');
Branch: 1 branch
BranchTrueFalseTotal
if $got_kind eq "warn" and $exp_kind eq "carped" 1 502 503
Condition100%3/3
$got_kind eq "warn" and $exp_kind eq "carped"
!ll&&!rl&&r
3721301
MC/DC100%2/2
$got_kind eq "warn" and $exp_kind eq "carped"
$got_kind eq "warn" $exp_kind eq "carped"
Truth table100%3/3
A and B
A= $got_kind eq "warn"B= $exp_kind eq "carped"
ABresult
0X0
100
111
405 502 my $cmp;
406 502 ▶ if ($exp_msg =~ /\n$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp_msg =~ /\n$/s) { } 6 496 502
407 6 $cmp = "$got_msg\n" eq $exp_msg;
408 } else {
409 496 $cmp = $got_msg =~ /^\Q$exp_msg\E at .+ line \d+\.?$/s;
410 }
411 502 return $cmp;
412 }
413  
414 1000 ▶sub _cmp_got_to_exp_warning_like {
Subroutine _cmp_got_to_exp_warning_like: called CC=4 SCAR=13.9
415 1000 my ($got_kind, $got_msg) = %{ shift() };
416 1000 my ($exp_kind, $exp_msg) = %{ shift() };
417 1000 ▶ return 0 if ($got_kind eq 'warn') && ($exp_kind eq 'carped');
Branch: 1 branch
BranchTrueFalseTotal
if $got_kind eq "warn" and $exp_kind eq "carped" 1 999 1000
Condition100%3/3
$got_kind eq "warn" and $exp_kind eq "carped"
!ll&&!rl&&r
7322671
MC/DC100%2/2
$got_kind eq "warn" and $exp_kind eq "carped"
$got_kind eq "warn" $exp_kind eq "carped"
Truth table100%3/3
A and B
A= $got_kind eq "warn"B= $exp_kind eq "carped"
ABresult
0X0
100
111
418 999 ▶ if (my $re = $Tester->maybe_regex($exp_msg)) { #qr// or '//'
Branch: 1 branch
BranchTrueFalseTotal
if (my $re = $Tester->maybe_regex($exp_msg)) { } 996 3 999
419 996 my $cmp = $got_msg =~ /$re/;
420 996 return $cmp;
421 } else {
422 3 return Test::Warn::Categorization::warning_like_category($got_msg,$exp_msg);
423 }
424 }
425  
426  
427 281 ▶sub _cmp_is {
Subroutine _cmp_is: called CC=4 SCAR=13.9
428 281 my @got = @{ shift() };
429 281 my @exp = @{ shift() };
430 281 ▶ scalar @got == scalar @exp or return 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @got == scalar @exp or return 0 183 98 281
431 183 my $cmp = 1;
432 183 ▶ $cmp &&= _cmp_got_to_exp_warning($got[$_],$exp[$_]) for (0 .. $#got);
Condition100%3/3
$cmp &&= _cmp_got_to_exp_warning($got[$_], $exp[$_])
!ll&&!rl&&r
1961442
MC/DC100%2/2
$cmp &&= _cmp_got_to_exp_warning($got[$_], $exp[$_])
$cmp _cmp_got_to_exp_warning($got[$_], $exp[$_])
Truth table100%3/3
A &&= B
A= $cmpB= _cmp_got_to_exp_warning($got[$_], $exp[$_])
ABresult
0X0
100
111
433 183 return $cmp;
434 }
435  
436 554 ▶sub _cmp_like {
Subroutine _cmp_like: called CC=4 SCAR=13.9
437 554 my @got = @{ shift() };
438 554 my @exp = @{ shift() };
439 554 ▶ scalar @got == scalar @exp or return 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @got == scalar @exp or return 0 356 198 554
440 356 my $cmp = 1;
441 356 ▶ $cmp &&= _cmp_got_to_exp_warning_like($got[$_],$exp[$_]) for (0 .. $#got);
Condition100%3/3
$cmp &&= _cmp_got_to_exp_warning_like($got[$_], $exp[$_])
!ll&&!rl&&r
35121870
MC/DC100%2/2
$cmp &&= _cmp_got_to_exp_warning_like($got[$_], $exp[$_])
$cmp _cmp_got_to_exp_warning_like($got[$_], $exp[$_])
Truth table100%3/3
A &&= B
A= $cmpB= _cmp_got_to_exp_warning_like($got[$_], $exp[$_])
ABresult
0X0
100
111
442 356 return $cmp;
443 }
444  
445 478 ▶sub _diag_found_warning {
Subroutine _diag_found_warning: called CC=5 SCAR=16.2
446 478 foreach (@_) {
447 1490 ▶ if (ref($_) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 1490 0 1490
448 1490 ▶ ${$_}{carped} ? $Tester->diag("found carped warning: ${$_}{carped}")
Branch: 1 branch
BranchTrueFalseTotal
do { $_ }->{'carped'} ? : 1108 382 1490
449 382 : $Tester->diag("found warning: ${$_}{warn}");
450 } else {
451 0 $Tester->diag( "found warning: $_" );
452 }
453 }
454 478 ▶ $Tester->diag( "didn't find a warning" ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 68 410 478
455 }
456  
457 478 ▶sub _diag_exp_warning {
Subroutine _diag_exp_warning: called CC=5 SCAR=16.2
458 478 foreach (@_) {
459 1426 ▶ if (ref($_) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 1426 0 1426
460 1426 ▶ ${$_}{carped} ? $Tester->diag("expected to find carped warning: ${$_}{carped}")
Branch: 1 branch
BranchTrueFalseTotal
do { $_ }->{'carped'} ? : 1064 362 1426
461 362 : $Tester->diag("expected to find warning: ${$_}{warn}");
462 } else {
463 0 $Tester->diag( "expected to find warning: $_" );
464 }
465 }
466 478 ▶ $Tester->diag( "didn't expect to find a warning" ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 66 412 478
467 }
468  
469 package Test::Warn::Categorization;
470  
471 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
472  
473 7 my $bits = \%warnings::Bits;
474 my @warnings = sort grep {
475 7 my $warn_bits = $bits->{$_};
476 #!grep { $_ ne $warn_bits && ($_ & $warn_bits) eq $_ } values %$bits;
477 } keys %$bits;
478  
479 # Create a warning name category (e.g. 'utf8') to map to a list of warnings.
480 # The warnings are strings that will be OR'ed together into a
481 # regular expression: qr/...|...|.../.
482 7 my %warnings_in_category = (
483 'utf8' => ['Wide character in \w+\b',],
484 );
485  
486 3 ▶sub _warning_category_regexp {
Subroutine _warning_category_regexp: called CC=3 SCAR=11.3
487 3 my $category = shift;
488 3 ▶ my $category_bits = $bits->{$category} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $category_bits = $bits->{$category} or return 3 0 3
489 my @category_warnings
490 3 = grep { ($bits->{$_} & $category_bits) eq $bits->{$_} } @warnings;
491  
492 my @list =
493 0 ▶ map { exists $warnings_in_category{$_}? (@{ $warnings_in_category{$_}}) : ($_) }
Branch: 1 branch
BranchTrueFalseTotal
exists $warnings_in_category{$_} ? : 0 3 3
494 @category_warnings;
495 3 my $re = join "|", @list;
496 3 return qr/$re/;
497 }
498  
499 3 ▶sub warning_like_category {
Subroutine warning_like_category: called CC=2 SCAR=7.0
500 3 my ($warning, $category) = @_;
501 3 ▶ my $re = _warning_category_regexp($category) or
Branch: 1 branch
BranchTrueFalseTotal
my $re = _warning_category_regexp($category) or carp("Unknown warning category '${category}'"), return 3 0 3
502 carp("Unknown warning category '$category'"),return;
503 3 my $ok = $warning =~ /$re/;
504 3 return $ok;
505 }
506  
507 7 1;
↑ Summary