8 use PLP::Functions qw( DecodeURI );
9 require PLP::Backend::CGI;
10 require PerlIO::scalar;
12 our $VERSION = '1.00';
15 our @EXPORT = qw( plp_is plp_ok );
17 $PLP::use_cache = 0 if $PLP::use_cache;
18 #TODO: caching on (change file names)
20 my $ORGDIR = '.'; # Cwd::getcwd();
21 open ORGOUT, '>&', *STDOUT;
24 require Test::LongString;
25 Test::LongString->import(max => 128);
27 # override output method to not escape newlines
28 no warnings 'redefine';
29 my $formatter = *Test::LongString::_display;
30 my $parent = \&{$formatter};
33 $s =~ s/\Q\x{0a}/\n /g;
34 # align lines to: "____expected: "
37 } or *is_string = \&is; # fallback to ugly unformatted is()
40 my ($src, $env, $in) = @_;
43 REQUEST_METHOD => 'GET',
44 REQUEST_URI => "/$src/test/123",
45 QUERY_STRING => 'test=1&test=2',
46 GATEWAY_INTERFACE => 'CGI/1.1',
48 SCRIPT_NAME => '/plp.cgi',
49 SCRIPT_FILENAME => "$ORGDIR/plp.cgi",
50 PATH_INFO => "/$src/test/123",
51 PATH_TRANSLATED => "$ORGDIR/$src/test/123",
52 DOCUMENT_ROOT => $ORGDIR,
55 ); # Apache/2.2.4 CGI environment
58 $ENV{CONTENT_LENGTH} //= length $in;
59 $ENV{CONTENT_TYPE} //= 'application/x-www-form-urlencoded';
65 open STDOUT, '>', \my $output; # STDOUT buffered to scalar
66 select STDOUT; # output before start() (which selects PLPOUT)
68 local $SIG{__WARN__} = sub {
69 # include warnings in stdout (but modified to distinguish)
71 my $eol = $msg =~ s/(\s*\z)// && $1;
72 print "<warning>$msg</warning>$eol"
77 select ORGOUT; # return to original STDOUT
79 return ($output, $failure);
83 my ($name, $src, $expect, $env, $in) = @_;
84 local $Test::Builder::Level = $Test::Builder::Level + 1;
86 my ($output, $failure) = _plp_run($src, $env, $in);
89 diag(" Error: $failure");
92 $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
93 is_string($output, $expect, $name);
97 # captures the first warning produced by the given code string
98 my ($code, $line, $file) = @_;
100 local $SIG{__WARN__} = sub { die @_ };
101 # warnings module runs at BEGIN, so we need to use icky expression evals
102 eval qq(# line $line "$file"\n$code; return);
109 my ($file, %replace) = @_;
110 local $Test::Builder::Level = $Test::Builder::Level + 1;
112 (my $name = $file) =~ s/[.][^.]+$//;
113 $file = "$name.html";
114 my $infile = delete $replace{-input} // "$name.plp";
115 my $addin = -e "$name.txt" && "$name.txt";
116 $name =~ s/^(\d*)-// and $name .= " ($1)";
120 local $/ = undef; # slurp
121 open my $fh, '<', $file or die "$!\n";
124 if (not defined $out) {
126 diag("error reading output from $file: $@");
130 my $env = delete $replace{-env};
131 $replace{HEAD} //= "Content-Type: text/html\nX-PLP-Version: $PLP::VERSION\n";
132 $replace{VERSION } //= $PLP::VERSION;
133 $replace{SCRIPT_NAME } //= $infile;
134 $replace{SCRIPT_FILENAME} //= "$ORGDIR/$infile";
137 $out =~ s/\$$_/$replace{$_}/g for keys %replace;
139 <eval \s+ line="([^"]*)"> (.*?) </eval>
140 }{ _getwarning($2, $1, $infile) }msxge;
142 plp_is($name, $infile, $out, $env, $addin);