lib/Config/Any/WTScript.pm

filter: Statementstmt 19.1% 30 / 157 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 47.0% 8 / 17 Podpod 100% 2 / 2 totaltotal 15.0% 40 / 266 CC 42 SCAR 44.5
CC
42
Cov
12%
load
182.0
_parse
110.0
_parse_scalar
110.0
CRAP 85.5
↑ Summary lib/WWW/Mechanize/Script/Plugin/ResponseTimeTest.pm »
1 package Config::Any::WTScript;
2  
3 # ABSTRACT: Parse wtscript files.
4  
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw($VERSION @extensions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base 'Config::Any::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Text::Balanced qw(extract_codeblock extract_delimited);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant ST_FILE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant ST_TEST_BLOCK => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '0.100';
20  
21 # horizontal space regexp
22 1 my $reHS = qr/[\t ]/;
23 # sequence of any chars which doesn't contain ')', space chars and '=>'
24 1 my $reWORD = qr/(?: (?: [^=)\s] | [^)\s] (?!>) )+ )/x;
25 # eat comments regexp
26 1 my $reCOMMENT = qr/(?: \s*? ^ \s* \# .* )+/mx;
27  
28  
29 1 @extensions = qw(wts wtscript);
30  
31 sub extensions
32 0 ▶{
Subroutine extensions: not called CC=2 SCAR=17.9
Pod documented
33 0 my ( $class, @new_ext ) = @_;
34 0 ▶ @new_ext and @extensions = @new_ext;
Branch: 1 branch
BranchTrueFalseTotal
@new_ext and @Config::Any::WTScript::extensions = @new_ext 0 0 0
35 0 return @extensions;
36 }
37  
38  
39 sub load
40 0 ▶{
Subroutine load: not called CC=13 SCAR=52.0
Pod documented
41 0 my $class = shift;
42 0 my $filename = shift;
43  
44 0 my $data = read_file($filename);
45  
46 0 my ( $tests, $opts ) = eval { _parse($data) };
47  
48 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
49 {
50 0 my $exc = $@;
51 0 chomp $exc;
52  
53 0 ▶ my $parse_pos = pos($data) || 0;
Condition0%0/2
pos $data || 0
l!l
00
MC/DC0%0/1
pos $data || 0
pos $data
Truth table0%0/2
A
A= pos $data
Aresult
11
00
54  
55 # find reminder of string near error (without surrounding
56 # whitespace)
57 0 $data =~ /\G $reHS* (.*?) $reHS* $/gmx;
58 0 my $near = $1;
59 0 ▶ if ( $near eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($near eq '') { } 0 0 0
60 {
61 0 $near = 'at the end of line';
62 }
63 else
64 {
65 0 $near = "near '$near'";
66 }
67  
68 # count lines
69 0 my $line_num = () = substr( $data, 0, $parse_pos ) =~ m|$|gmx;
70 0 pos($data) = $parse_pos;
71 0 ▶ $line_num-- if $data =~ /\G \z/gx;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /\G \z/gx 0 0 0
72  
73 0 die <<MSG;
74 Config::WebTest: wtscript parsing error
75 Line $line_num $near: $exc
76 MSG
77 }
78  
79 0 my @configs;
80 0 foreach my $test_item (@$tests)
81 {
82 0 my %test = %$test_item;
83 0 my %cfg = (
84 request => {
85 agent => {},
86 method => 'get',
87 },
88 check => { response => 200, },
89 );
90 0 my $agent_cfg = $cfg{request}->{agent};
91 # convert params
92 0 ▶ defined( $test{user_agent} ) and $agent_cfg->{agent} = delete $test{user_agent};
Branch: 1 branch
BranchTrueFalseTotal
defined $test{"user_agent"} and $agent_cfg->{"agent"} = delete $test{"user_agent"} 0 0 0
93 defined( $test{handle_redirects} )
94 0 ▶ and $agent_cfg->{requests_redirectable} = delete $test{handle_redirects};
Branch: 1 branch
BranchTrueFalseTotal
defined $test{"handle_redirects"} and $agent_cfg->{"requests_redirectable"} = delete $test{"handle_redirects"} 0 0 0
95 0 ▶ defined( $test{proxies} ) and $agent_cfg->{proxy} = { @{ delete $test{proxies} } };
Branch: 1 branch
BranchTrueFalseTotal
defined $test{"proxies"} and $agent_cfg->{"proxy"} = {@{delete $test{"proxies"};}} 0 0 0
96 defined( $agent_cfg->{requests_redirectable} )
97 and looks_like_number( $agent_cfg->{requests_redirectable} )
98 0 ▶ and $agent_cfg->{requests_redirectable} = [qw(GET POST)];
Branch: 1 branch
BranchTrueFalseTotal
defined $agent_cfg->{"requests_redirectable"} and looks_like_number($agent_cfg->{"requests_redirectable"}) and $agent_cfg->{"requests_redirectable"} = ["GET", "POST"] 0 0 0
Condition0%0/3
defined $agent_cfg->{"requests_redirectable"} and looks_like_number($agent_cfg->{"requests_redirectable"})
!ll&&!rl&&r
000
MC/DC0%0/2
defined $agent_cfg->{"requests_redirectable"} and looks_like_number($agent_cfg->{"requests_redirectable"})
defined $agent_cfg->{"requests_redirectable"} looks_like_number($agent_cfg->{"requests_redirectable"})
Truth table0%0/3
A and B
A= defined $agent_cfg->{"requests_redirectable"}B= looks_like_number($agent_cfg->{"requests_redirectable"})
ABresult
0X0
100
111
99  
100 0 $cfg{request}->{uri} = delete $test{url};
101 0 ▶ defined $test{method} and $cfg{request}->{method} = lc delete $test{method};
Branch: 1 branch
BranchTrueFalseTotal
defined $test{"method"} and $cfg{"request"}{"method"} = lc delete $test{"method"} 0 0 0
102 defined $test{http_headers}
103 0 ▶ and $cfg{request}->{http_headers} = { @{ delete $test{http_headers} } };
Branch: 1 branch
BranchTrueFalseTotal
defined $test{"http_headers"} and $cfg{"request"}{"http_headers"} = {@{delete $test{"http_headers"};}} 0 0 0
104  
105 0 $cfg{check} = \%test;
106 0 $cfg{opts} = $opts;
107  
108 0 push @configs, \%cfg;
109 }
110  
111 0 return \@configs;
112 }
113  
114 sub _eval_in_playground
115 0 ▶{
Subroutine _eval_in_playground: not called CC=1 SCAR=6.9
116 0 my $code = shift;
117  
118 0 return eval <<CODE;
119 package Config::WebTest::PlayGround;
120  
121 no strict;
122 local \$^W; # aka no warnings in new perls
123  
124 $code
125 CODE
126 }
127  
128 sub _make_sub_in_playground
129 0 ▶{
Subroutine _make_sub_in_playground: not called CC=1 SCAR=6.9
130 0 my $code = shift;
131  
132 0 return _eval_in_playground("sub { local \$^W; $code }");
133 }
134  
135 sub _parse
136 0 ▶{
Subroutine _parse: not called CC=10 SCAR=47.0
137 0 my $data = shift;
138  
139 0 my $state = ST_FILE;
140 0 my $opts = {};
141 0 my $tests = [];
142 0 my $test = undef;
143  
144 PARSER:
145 0 while (1)
146 {
147 # eat whitespace and comments
148 0 $data =~ /\G $reCOMMENT /gcx;
149  
150 # eat whitespace
151 0 $data =~ /\G \s+/gcx;
152  
153 0 ▶ if ( $state == ST_FILE )
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 0) { } 0 0 0
elsif ($state == 1) { } 0 0 0
154 {
155 0 ▶ if ( $data =~ /\G \z/gcx )
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\G \z/cgx) { } 0 0 0
elsif ($data =~ /\G test_name (?=\W)/cgx) { } 0 0 0
156 {
157 # end of file
158 0 last PARSER;
159 }
160 elsif ( $data =~ /\G test_name (?=\W)/gcx )
161 {
162 # found new test block start
163 0 $test = {};
164 0 $state = ST_TEST_BLOCK;
165  
166 # find test block name
167 0 ▶ if ( $data =~ /\G $reHS* = $reHS* (?: \n $reHS*)?/gcx )
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\G $reHS* = $reHS* (?: \n $reHS*)?/cgx) 0 0 0
168 {
169 0 $test->{test_name} = _parse_scalar($data);
170  
171 die "Test name is missing\n"
172 0 ▶ unless defined $test->{test_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test->{"test_name"} 0 0 0
173 }
174 }
175 else
176 {
177 # expect global test parameter
178 0 my ( $name, $value ) = _parse_param($data);
179  
180 0 ▶ if ( defined $name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
181 {
182 0 _set_test_param( $opts, $name, $value );
183 }
184 else
185 {
186 0 die "Global test parameter or test block is expected\n";
187 }
188 }
189 }
190 elsif ( $state == ST_TEST_BLOCK )
191 {
192 0 ▶ if ( $data =~ /\G end_test (?=\W)/gcx )
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\G end_test (?=\W)/cgx) { } 0 0 0
193 {
194 0 push @$tests, $test;
195 0 $state = ST_FILE;
196 }
197 else
198 {
199 # expect test parameter
200 0 my ( $name, $value ) = _parse_param($data);
201  
202 0 ▶ if ( defined $name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
203 {
204 0 _set_test_param( $test, $name, $value );
205 }
206 else
207 {
208 0 die "Test parameter or end_test is expected\n";
209 }
210 }
211 }
212 else
213 {
214 0 die "Unknown state\n";
215 }
216 }
217  
218 0 return ( $tests, $opts );
219 }
220  
221 sub _set_test_param
222 0 ▶{
Subroutine _set_test_param: not called CC=4 SCAR=30.0
223 0 my $href = shift;
224 0 my $name = shift;
225 0 my $value = shift;
226  
227 0 ▶ if ( exists $href->{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $href->{$name}) { } 0 0 0
228 {
229 $href->{$name} = [ $href->{$name} ]
230 if ref( $href->{$name} )
231 0 ▶ and ref( $href->{$name} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $href->{$name} and ref $href->{$name} eq "ARRAY" 0 0 0
Condition0%0/3
ref $href->{$name} and ref $href->{$name} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $href->{$name} and ref $href->{$name} eq "ARRAY"
ref $href->{$name} ref $href->{$name} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $href->{$name}B= ref $href->{$name} eq "ARRAY"
ABresult
0X0
100
111
232 0 push @{ $href->{$name} }, $value;
233 }
234 else
235 {
236 0 $href->{$name} = $value;
237 }
238 }
239  
240 sub _parse_param
241 0 ▶{
Subroutine _parse_param: not called CC=3 SCAR=24.8
242 0 my $name;
243  
244 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\G ([a-zA-Z_]+) # param name $reHS* = $reHS* (?: \n $reHS*)? # = (and optional space chars) /cgx) { } 0 0 0
245 $_[0] =~ /\G ([a-zA-Z_]+) # param name
246 $reHS* = $reHS* (?: \n $reHS*)? # = (and optional space chars)
247 /gcx
248 )
249 {
250 0 $name = $1;
251 }
252 else
253 {
254 0 return;
255 }
256  
257 0 my $value = _parse_value( $_[0] );
258 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
259  
260 0 return ( $name, $value );
261 }
262  
263 sub _parse_value
264 0 ▶{
Subroutine _parse_value: not called CC=6 SCAR=37.4
265 0 ▶ if ( $_[0] =~ /\G \(/gcx )
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\G \(/cgx) { } 0 0 0
266 {
267 # list elem
268 #
269 # ( scalar
270 # ...
271 # scalar )
272 #
273 # ( scalar => scalar
274 # ...
275 # scalar => scalar )
276  
277 0 my @list = ();
278  
279 0 while (1)
280 {
281 # eat whitespace and comments
282 0 $_[0] =~ /\G $reCOMMENT /gcx;
283  
284 # eat whitespace
285 0 $_[0] =~ /\G \s+/gcx;
286  
287 # exit loop on closing bracket
288 0 ▶ last if $_[0] =~ /\G \)/gcx;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\G \)/cgx 0 0 0
289  
290 0 my $value = _parse_value( $_[0] );
291  
292 0 ▶ die "Missing right bracket\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
293 unless defined $value;
294  
295 0 push @list, $value;
296  
297 0 ▶ if ( $_[0] =~ /\G $reHS* => $reHS* /gcx )
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\G $reHS* => $reHS* /cgx) 0 0 0
298 {
299 # handles second part of scalar => scalar syntax
300 0 my $value = _parse_value( $_[0] );
301  
302 0 ▶ die "Missing right bracket\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
303 unless defined $value;
304  
305 0 push @list, $value;
306 }
307 }
308  
309 0 return \@list;
310 }
311 else
312 {
313 # may return undef
314 0 return _parse_scalar( $_[0] );
315 }
316 }
317  
318 sub _parse_scalar
319 0 ▶{
Subroutine _parse_scalar: not called CC=10 SCAR=47.0
320 0 my $parse_pos = pos $_[0];
321  
322 0 ▶ if ( $_[0] =~ /\G (['"])/gcx )
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /\G (['"])/cgx) { } 0 0 0
elsif ($_[0] =~ /\G \{/cgx) { } 0 0 0
323 {
324 0 my $delim = $1;
325  
326 0 pos( $_[0] ) = $parse_pos;
327 0 my ($extracted) = extract_delimited( $_[0] );
328 0 ▶ die "Can't find string terminator \"$delim\"\n"
Branch: 1 branch
BranchTrueFalseTotal
if $extracted eq "" 0 0 0
329 if $extracted eq '';
330  
331 0 ▶ if ( $delim eq "'" or $extracted !~ /[\$\@\%]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($delim eq q['] or not $extracted =~ /[\$\@\%]/) { } 0 0 0
Condition0%0/3
$delim eq "'" or not $extracted =~ /[\$\@\%]/
l!l&&r!l&&!r
000
MC/DC0%0/2
$delim eq "'" or not $extracted =~ /[\$\@\%]/
$delim eq "'" not $extracted =~ /[\$\@\%]/
Truth table0%0/3
A or not(B)
A= $delim eq "'"B= not $extracted =~ /[\$\@\%]/
ABresult
1X1
011
000
332 {
333 # variable interpolation impossible - just evalute string
334 # to get rid of escape chars
335 0 my $ret = _eval_in_playground($extracted);
336  
337 0 chomp $@;
338 0 ▶ die "Eval error\n$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
339  
340 0 return $ret;
341 }
342 else
343 {
344 # variable interpolation possible - evaluate as subroutine
345 # which will be used as callback
346 0 my $ret = _make_sub_in_playground($extracted);
347  
348 0 chomp $@;
349 0 ▶ die "Eval error\n$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
350  
351 0 return $ret;
352 }
353 }
354 elsif ( $_[0] =~ /\G \{/gcx )
355 {
356 0 pos( $_[0] ) = $parse_pos;
357 0 my ($extracted) = extract_codeblock( $_[0] );
358 0 ▶ die "Missing right curly bracket\n"
Branch: 1 branch
BranchTrueFalseTotal
if $extracted eq "" 0 0 0
359 if $extracted eq '';
360  
361 0 my $ret = _make_sub_in_playground($extracted);
362  
363 0 chomp $@;
364 0 ▶ die "Eval error\n$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
365  
366 0 return $ret;
367 }
368 else
369 {
370 0 $_[0] =~ /\G ((?: $reWORD $reHS+ )* $reWORD )/gcxo;
371 0 my $extracted = $1;
372  
373 # may return undef
374 0 return $extracted;
375 }
376 }
377  
378  
379 1 1;
380  
381 __END__
↑ Summary lib/WWW/Mechanize/Script/Plugin/ResponseTimeTest.pm »