5 unshift @INC, '../blib/lib';
13 use constant TESTS =>(
14 ["ok", "my warning", "my", "standard warning to find"],
15 ["not ok", "my warning", "another", "another warning instead of my warning"],
16 ["not ok", "warning general not", "^(?!warning general)", "quite only a sub warning"],
17 ["not ok", undef, "a warning", "no warning, but expected one"],
18 ["not ok", "a warning", undef, "warning, but didn't expect one"],
19 ["ok", undef, undef, "no warning"],
20 ["ok", '$!"%&/()=', '\$\!\"\%\&\/\(\)\=', "warning with crazy letters"],
21 ["not ok", "warning 1|warning 2", "warning1", "more than one warning"]
23 use constant SUBTESTS_PER_TESTS => 12;
25 use Test::Builder::Tester tests => TESTS() * SUBTESTS_PER_TESTS;
29 Test::Builder::Tester::color 'on';
31 use constant WARN_LINE => line_num +2;
33 warn $_ for grep $_, split m:\|:, (shift() || "");
36 use constant CARP_LINE => line_num +2;
38 carp $_ for grep $_, split m:\|:, (shift() || "");
41 use constant CARP_LEVELS => (0 .. 2);
42 sub _create_exp_warning {
43 my ($carplevel, $warning) = @_;
44 return $warning if $carplevel == 0;
45 return {carped => $warning} if $carplevel == 1;
46 return {carped => [$warning]} if $carplevel == 2;
49 test_warning_like(@$_) foreach TESTS();
51 sub test_warning_like {
52 my ($ok, $msg, $exp_warning, $testname) = @_;
53 for my $carp (CARP_LEVELS) {
54 *_found_msg = $carp ? *_found_carp_msg : *_found_warn_msg;
55 *_exp_msg = $carp ? *_exp_carp_msg : *_exp_warn_msg;
56 *_make_warn_or_carp = $carp ? *_make_carp : *_make_warn;
57 for my $t (undef, $testname) {
58 my @regexes = $exp_warning ? (qr/$exp_warning/, "/$exp_warning/")
59 : (undef, undef); # simpler to count the tests
60 for my $regex (@regexes) {
61 test_out "$ok 1" . ($t ? " - $t" : "");
64 test_diag _found_msg($_) for ($msg ? (split m-\|-, $msg) : $msg);
65 test_diag _exp_msg($regex);
67 warning_like {_make_warn_or_carp($msg)} _create_exp_warning($carp, $regex), $t;
68 test_test "$testname (with" . ($_ ? "" : "out") . " a testname)";
76 ? ( join " " => ("found warning:",
82 : "didn't found a warning";
87 ? "expected to find warning: $_[0]"
88 : "didn't expect to find a warning";
93 ? ( join " " => ("found carped warning:",
98 CARP_LINE) ) # Note the difference, that carp msg
99 : "didn't found a warning"; # aren't finished by '.'
104 ? "expected to find carped warning: $_[0]"
105 : "didn't expect to find a warning";