blib/lib/Test/PLP.pm

filter: Statementstmt 97.1% 101 / 104 Branchbran 66.6% 12 / 18 Conditioncond 50.0% 12 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 80.1% 141 / 176 CC 19 SCAR 18.2
CC
19
Cov
86%
_getplp
12.5
_plp_run
6.1
plp_is
3.1
CRAP 6.2
« blib/lib/PLP/Fields.pm ↑ Summary blib/lib/PLP/Backend/CGI.pm »
1 package Test::PLP;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use PLP::Functions qw( DecodeURI );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 require PLP::Backend::CGI;
8 2 require PerlIO::scalar;
9  
10 2 our $VERSION = '1.00';
11  
12 2 ▶use Test::Builder::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use base 'Test::Builder::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @EXPORT = qw( plp_is plp_ok );
15  
16 2 ▶$PLP::use_cache = 0 if $PLP::use_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $PLP::use_cache 0 2 2
17 #TODO: caching on (change file names)
18  
19 2 open ORGOUT, '>&', *STDOUT;
20  
21 sub is_string ($$;$) {
22 my $tb = __PACKAGE__->builder;
23 $tb->is_eq(@_);
24 }
25  
26 2 ▶eval {
Condition50%1/2
eval { do { require Test::LongString; 'Test::LongString'->import('max', 128); BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} my $formatter = *Test::LongString::_display; my $parent = \&{$formatter;}; *{$formatter;} = sub { my $s = &{$parent;}; $s =~ s/\\x\{0a\}/\n /g; return $s; } } } || 1
l!l
20
MC/DC0%0/1
eval { do { require Test::LongString; 'Test::LongString'->import('max', 128); BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} my $formatter = *Test::LongString::_display; my $parent = \&{$formatter;}; *{$formatter;} = sub { my $s = &{$parent;}; $s =~ s/\\x\{0a\}/\n /g; return $s; } } } || 1
eval { do { require Test::LongString; 'Test::LongString'->import('max', 128); BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} my $formatter = *Test::LongString::_display; my $parent = \&{$formatter;}; *{$formatter;} = sub { my $s = &{$parent;}; $s =~ s/\\x\{0a\}/\n /g; return $s; } } }
Truth table50%1/2
A
A= eval { do { require Test::LongString; 'Test::LongString'->import('max', 128); BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} my $formatter = *Test::LongString::_display; my $parent = \&{$formatter;}; *{$formatter;} = sub { my $s = &{$parent;}; $s =~ s/\\x\{0a\}/\n /g; return $s; } } }
Aresult
11
00
27 # optionally replace unformatted is_string by LongString prettification
28 2 require Test::LongString;
29 2 Test::LongString->import(max => 128);
30  
31 # override output method to not escape newlines
32 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 my $formatter = *Test::LongString::_display;
34 2 my $parent = \&{$formatter};
35 2 ▶ *{$formatter} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 2 my $s = &{$parent};
37 2 $s =~ s/\Q\x{0a}/\n /g;
38 # align lines to: "____expected: "
39 2 return $s;
40 2 };
41 } or 1;
42  
43 25 ▶sub _plp_run {
Subroutine _plp_run: called CC=6 SCAR=18.1
44 25 my ($src, $env, $input) = @_;
45  
46 %ENV = (
47 REQUEST_METHOD => 'GET',
48 REQUEST_URI => "/$src/test/123",
49 QUERY_STRING => 'test=1&test=2',
50 GATEWAY_INTERFACE => 'CGI/1.1',
51
52 SCRIPT_NAME => '/plp.cgi',
53 SCRIPT_FILENAME => "./plp.cgi",
54 PATH_INFO => "/$src/test/123",
55 PATH_TRANSLATED => "./$src/test/123",
56 DOCUMENT_ROOT => ".",
57
58 25 ▶ $env ? %{$env} : (),
Branch: 1 branch
BranchTrueFalseTotal
$env ? : 2 23 25
59 ); # Apache/2.2.4 CGI environment
60  
61 25 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) 2 23 25
62 2 ▶ $ENV{CONTENT_LENGTH} //= length $input;
Condition33%1/3
$ENV{"CONTENT_LENGTH"} //= length $input
l!l&&r!l&&!r
020
MC/DC0%0/2
$ENV{"CONTENT_LENGTH"} //= length $input
$ENV{"CONTENT_LENGTH"} length $input
Truth table33%1/3
A //= B
A= $ENV{"CONTENT_LENGTH"}B= length $input
ABresult
1X1
011
000
63 2 ▶ $ENV{CONTENT_TYPE} //= 'application/x-www-form-urlencoded';
Condition100%2/2
$ENV{"CONTENT_TYPE"} //= "application/x-www-form-urlencoded"
l!l
11
MC/DC100%1/1
$ENV{"CONTENT_TYPE"} //= "application/x-www-form-urlencoded"
$ENV{"CONTENT_TYPE"}
Truth table100%2/2
A
A= $ENV{"CONTENT_TYPE"}
Aresult
11
00
64 2 close STDIN;
65 2 open STDIN, '<', $input;
66 }
67  
68 25 close STDOUT;
69 25 open STDOUT, '>', \my $output; # STDOUT buffered to scalar
70 25 select STDOUT; # output before start() (which selects PLPOUT)
71 25 eval {
72 4 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
73 # include warnings in stdout (but modified to distinguish)
74 4 my $msg = shift;
75 4 ▶ my $eol = $msg =~ s/(\s*\z)// && $1;
Condition33%1/3
$msg =~ s/(\s*\z)// && $1
!ll&&!rl&&r
004
MC/DC0%0/2
$msg =~ s/(\s*\z)// && $1
$msg =~ s/(\s*\z)// $1
Truth table33%1/3
A && B
A= $msg =~ s/(\s*\z)//B= $1
ABresult
0X0
100
111
76 4 print "<warning>$msg</warning>$eol"
77 25 };
78 25 PLP::everything();
79 };
80 25 my $failure = $@;
81 25 select ORGOUT; # return to original STDOUT
82 25 ▶ die $failure if $failure;
Branch: 1 branch
BranchTrueFalseTotal
if $failure 0 25 25
83  
84 25 return $output;
85 }
86  
87 25 ▶sub plp_is {
Subroutine plp_is: called CC=3 SCAR=11.3
Pod undocumented
88 25 my ($src, $env, $input, $expect, $name) = @_;
89 25 my $tb = __PACKAGE__->builder;
90 25 local $Test::Builder::Level = $Test::Builder::Level + 1;
91  
92 25 my $output = eval { _plp_run($src, $env, $input) };
93 25 ▶ if (my $failure = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $@) 0 25 25
94 0 $tb->ok(0, $name);
95 0 $tb->diag(" Error: $failure");
96 0 return;
97 }
98  
99 25 ▶ if (defined $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expect) 24 1 25
100 24 $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
101 24 return is_string($output, $expect, $name);
102 }
103  
104 1 $tb->ok(defined $output, $name);
105 1 return $output;
106 }
107  
108 2 ▶sub _getwarning {
Subroutine _getwarning: called CC=1 SCAR=0.0
109 # captures the first warning produced by the given code string
110 2 my ($code, $line, $file) = @_;
111  
112 2 ▶ local $SIG{__WARN__} = sub { die @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
113 # warnings module runs at BEGIN, so we need to use icky expression evals
114 2 eval qq(# line $line "$file"\n$code; return);
115 2 my $res = $@;
116 2 chomp $res;
117 2 return $res;
118 }
119  
120 24 ▶sub _getplp {
Subroutine _getplp: called CC=11 SCAR=25.3
121 24 my ($file, %replace) = @_;
122  
123 24 (my $name = $file) =~ s/[.][^.]+$//;
124 24 $file = "$name.html";
125 24 ▶ my $src = delete $replace{-input} // "$name.plp";
Condition50%1/2
delete $replace{"-input"} // "$name.plp"
l!l
024
MC/DC0%0/1
delete $replace{"-input"} // "$name.plp"
delete $replace{"-input"}
Truth table50%1/2
A
A= delete $replace{"-input"}
Aresult
11
00
126 24 ▶ my $input = -e "$name.txt" && "$name.txt";
Condition100%2/2
-e "$name.txt" && "$name.txt"
!ll
222
MC/DC100%1/1
-e "$name.txt" && "$name.txt"
-e "$name.txt"
Truth table100%2/2
A
A= -e "$name.txt"
Aresult
00
11
127 24 ▶ $name =~ s/^(\d*)-// and $name .= " ($1)";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^(\d*)-// and $name .= " ($1)" 24 0 24
128 24 DecodeURI($name);
129  
130 24 my $env = delete $replace{-env};
131  
132 24 my $output;
133 24 ▶ if (open my $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $file) 24 0 24
134 24 local $/ = undef; # slurp
135 24 $output = readline $fh;
136 24 close $fh;
137 }
138  
139 24 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 24 0 24
140 24 ▶ $replace{HEAD} //= "Content-Type: text/html\nX-PLP-Version: $PLP::VERSION\n";
Condition50%1/2
$replace{"HEAD"} //= "Content-Type: text/html\nX-PLP-Version: $PLP::VERSION\n"
l!l
024
MC/DC0%0/1
$replace{"HEAD"} //= "Content-Type: text/html\nX-PLP-Version: $PLP::VERSION\n"
$replace{"HEAD"}
Truth table50%1/2
A
A= $replace{"HEAD"}
Aresult
11
00
141 24 ▶ $replace{VERSION } //= $PLP::VERSION;
Condition33%1/3
$replace{"VERSION"} //= $PLP::VERSION
l!l&&r!l&&!r
0240
MC/DC0%0/2
$replace{"VERSION"} //= $PLP::VERSION
$replace{"VERSION"} $PLP::VERSION
Truth table33%1/3
A //= B
A= $replace{"VERSION"}B= $PLP::VERSION
ABresult
1X1
011
000
142 24 ▶ $replace{SCRIPT_NAME } //= $src;
Condition33%1/3
$replace{"SCRIPT_NAME"} //= $src
l!l&&r!l&&!r
0240
MC/DC0%0/2
$replace{"SCRIPT_NAME"} //= $src
$replace{"SCRIPT_NAME"} $src
Truth table33%1/3
A //= B
A= $replace{"SCRIPT_NAME"}B= $src
ABresult
1X1
011
000
143 24 ▶ $replace{SCRIPT_FILENAME} //= "./$src";
Condition50%1/2
$replace{"SCRIPT_FILENAME"} //= "./$src"
l!l
024
MC/DC0%0/1
$replace{"SCRIPT_FILENAME"} //= "./$src"
$replace{"SCRIPT_FILENAME"}
Truth table50%1/2
A
A= $replace{"SCRIPT_FILENAME"}
Aresult
11
00
144  
145 24 chomp $output;
146 24 $output =~ s/\$$_/$replace{$_}/g for keys %replace;
147 24 $output =~ s{
148 <eval \s+ line="([^"]*)"> (.*?) </eval>
149 2 }{ _getwarning($2, $1, $src) }msxge;
150 }
151  
152 24 return ($src, $env, $input, $output, $name);
153 }
154  
155 24 ▶sub plp_ok {
Subroutine plp_ok: called CC=1 SCAR=0.0
Pod undocumented
156 24 local $Test::Builder::Level = $Test::Builder::Level + 1;
157 24 plp_is(_getplp(@_));
158 }
159  
« blib/lib/PLP/Fields.pm ↑ Summary blib/lib/PLP/Backend/CGI.pm »