7 eval { require PerlIO::scalar };
8 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
12 require_ok('PLP::Backend::CGI') or BAIL_OUT();
14 $PLP::use_cache = 0 if $PLP::use_cache;
15 #TODO: caching on (change file names)
17 my $base = -w '/tmp' ? '/tmp' : cwd();
18 my $testfile = 'testfile.plp';
19 not -f "$base/$testfile" or BAIL_OUT("$testfile exists");
21 open ORGOUT, '>&', STDOUT;
24 my ($test, $plp, $expect) = @_;
26 local $Test::Builder::Level = $Test::Builder::Level + 1;
29 open my $testfh, '>', "$base/$testfile" or die $!;
30 print {$testfh} $plp or die $!;
31 close $testfh or die $!;
33 not $@ or fail("write $testfile"), diag(" Error: $@"), return;
36 open STDOUT, '>', \my $output; # STDOUT buffered to scalar
38 local $SIG{__WARN__} = sub { print $_[0] }; # enables warnings
41 select ORGOUT; # return to original STDOUT
43 not $@ or fail($test), diag(" Error: $@"), return;
44 is($output, $expect, $test);
48 REQUEST_METHOD => 'GET',
49 REQUEST_URI => "/$testfile/test/123",
50 QUERY_STRING => 'test=1&test=2',
51 GATEWAY_INTERFACE => 'CGI/1.1',
53 SCRIPT_NAME => '/plp.cgi',
54 SCRIPT_FILENAME => "$base/plp.cgi",
55 PATH_INFO => "/$testfile/test/123",
56 PATH_TRANSLATED => "$base/$testfile/test/123",
57 DOCUMENT_ROOT => $base,
58 ); # Apache/2.2.4 CGI environment
60 my $HEAD = <<EOT; # common header output
61 X-PLP-Version: $PLP::VERSION
62 Content-Type: text/html
65 plp_is('print', '0<: print print 2 :>3', "$HEAD\n0213");
67 plp_is('exit', '1<:exit:>not <(reached)>', "$HEAD\n1");
69 plp_is('<:=', '1<:=$foo=2:>3<:= $foo', "$HEAD\n1232");
71 plp_is('%get', '<: print $get{test} if defined $get{test} and not exists $get{test2}', "$HEAD\n2\n");
73 plp_is('%get array', '<:= @{$get{q/@test/}}', "$HEAD\n12\n");
76 '<: $headers{x_test}=2; print $header{x_PLP_version}; BEGIN { $header{"X-tesT"}=1 }',
77 "X-tesT: 2\n$HEAD\n$PLP::VERSION"
80 plp_is('%header', '.<: BEGIN{$header{x}="1\n2"} $header{x}=3', <<TEST);
84 .<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>Can't set headers after sending them at testfile.plp line 1.
85 (Output started at testfile.plp line 1.)
93 plp_is('PLP_END', '<: PLP_END{print 1}; PLP_END{print 2}; print 3', "$HEAD\n321");
95 plp_is('no warnings by default', '<: ignoreme :>ok', "$HEAD\nok");
97 rename "$base/$testfile", "$base/$testfile.inc";
98 plp_is('include', "<($testfile.inc)> <: include '$testfile.inc'", "$HEAD\nok ok");
99 unlink "$base/$testfile.inc";
101 plp_is('fatal error', "runtime\n<: syntax(error :>\nruntime", <<TEST);
103 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>syntax error at $testfile line 2, at EOF
104 (Might be a runaway multi-line \cq\cq string starting on line 1)
108 plp_is('warnings', split /\n\n/, <<TEST, 2);
116 <: include "missinginclude" :>
122 Useless use of a constant in void context at $testfile line 4.
128 warning at $testfile line 6.
131 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>Can't open "$base/missinginclude" (No such file or directory) at $testfile line 8.
134 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>Can't open "$base/missinginclude" (No such file or directory) at $testfile line 10.
138 plp_is('$PLP::ERROR',
139 '<: $PLP::ERROR = sub {print "Oh no: $_[0]"} :> <(missinginclude)>.',
140 qq{$HEAD\n Oh no: Can't open "$base/missinginclude" (No such file or directory) at $testfile line 1.\n\n}
146 plp_is('$PLP::DEBUG',
147 '<: $PLP::DEBUG = 2 :>1<(missinginclude)>2',
148 "Content-Type: text/plain\n\n$HEAD\n1"
151 plp_is('utf8', '<: use open qw/:std :utf8/; print chr 191', <<TEST);
152 X-PLP-Version: $PLP::VERSION
153 Content-Type: text/html; charset=utf-8
159 'ok <:=$ENV{SCRIPT_NAME}:> <:=$ENV{SCRIPT_FILENAME}',
160 "$HEAD\nok /$testfile $base/$testfile"
163 plp_is('%ENV (on apache)', @envtest);
166 REQUEST_METHOD => 'GET',
167 REQUEST_URI => "/$testfile/test/123",
168 QUERY_STRING => 'test=1&test=2',
169 GATEWAY_INTERFACE => 'CGI/1.1',
171 SCRIPT_NAME => "/$testfile", #XXX: .plp?
172 SCRIPT_FILENAME => "$base/$testfile",
173 PATH_INFO => '/test/123',
174 ); # lighttpd/1.4.7 CGI environment
176 plp_is('%ENV on lighttpd', @envtest);
178 unlink "$base/$testfile";