capture actual test warnings
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 20 Oct 2009 22:02:20 +0000 (00:02 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 21 Oct 2009 20:56:20 +0000 (22:56 +0200)
Compare expected `use warnings` output to warnings produced by similar code
instead of hardcoding text.  It doesn't really matter what is said, as long
it's the same using plp.

Fixes (so far and afaik) a slight change introduced by Perl v5.11.0 in
"Useless use of a constant in void context" (adding details about which
constant).

t/50-cgi.t

index d30bff2411fb5128561bff228e0a6d619a654e1d..d29fcd017bdcdc701b53a0ecbcdafa984208954c 100644 (file)
@@ -50,6 +50,16 @@ sub plp_is {
        is($output, $expect, $test);
 }
 
+sub getwarning {
+       # captures the first warning produced by the given code string
+       my ($code, $line, $file) = @_;
+
+       local $SIG{__WARN__} = sub { die @_ };
+       # warnings module runs at BEGIN, so we need to use icky expression evals
+       eval qq(# line $line "$file"\n$code; return);
+       return $@;
+};
+
 %ENV = (
        REQUEST_METHOD => 'GET',
        REQUEST_URI => "/$testfile/test/123",
@@ -104,11 +114,10 @@ rename "$base/$testfile", "$base/$testfile.inc";
 plp_is('include', "<($testfile.inc)> <: include '$testfile.inc'", "$HEAD\nok ok");
 unlink "$base/$testfile.inc";
 
+my $SYNTAXERR = getwarning("q\cq\n\cq; syntax(error", 1, $testfile);
 plp_is('fatal error', "runtime\n<: syntax(error :>\nruntime", <<TEST);
 $HEAD
-<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>syntax error at $testfile line 2, at EOF
-  (Might be a runaway multi-line \cq\cq string starting on line 1)
-</td></tr></table>
+<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$SYNTAXERR</td></tr></table>
 TEST
 
 SKIP: {
@@ -120,6 +129,8 @@ if (open my $dummy, "<", $INCFILE) {  # like PLP::source will
 }
 my $INCWARN = qq{Can't open "$INCFILE" ($!)};
 
+my $VOIDWARN = getwarning('42', 2, $testfile);
+
 plp_is('warnings', split /\n\n/, <<TEST, 2);
 1<: use warnings :>
 2<: 42 :>
@@ -129,8 +140,7 @@ plp_is('warnings', split /\n\n/, <<TEST, 2);
 6
 
 $HEAD
-Useless use of a constant in void context at $testfile line 2.
-1
+${VOIDWARN}1
 2
 3warning at $testfile line 3.