blib/lib/App/SimpleScan.pm

filter: Statementstmt 90.9% 330 / 363 Branchbran 75.4% 89 / 118 Conditioncond 73.3% 11 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 96.1% 50 / 52 Podpod 100% 20 / 20 totaltotal 87.5% 506 / 578 CC 78 SCAR 16.3
CC
78
Cov
87%
transform_test_specs
13.5
handle_options
7.2
install_options
7.1
CRAP 5.1
« bin/simple_report ↑ Summary blib/lib/App/SimpleScan/Substitution.pm »
1 package App::SimpleScan;
2  
3 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 our $VERSION = '4.0.1';
8  
9 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use WWW::Mechanize::Pluggable;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Test::WWW::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use App::SimpleScan::TestSpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use App::SimpleScan::Substitution;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 40 ▶use Module::Pluggable search_path => [qw(App::SimpleScan::Plugin)];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 40 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 __PACKAGE__->mk_accessors(qw(sub_engine tests test_count
24 next_line_callbacks _deps));
25  
26 40 $|++; ##no critic
27  
28 40 ▶use App::SimpleScan::TestSpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 40 my $reference_mech = WWW::Mechanize::Pluggable->new;
31 40 my $sub_engine = App::SimpleScan::Substitution->new;
32  
33 40 my @local_pragma_support =
34 (
35 ['agent' => \&_do_agent],
36 ['nocache' => \&_do_nocache],
37 ['cache' => \&_do_cache],
38 );
39  
40 # Variables and setup for basic command-line options.
41 40 my($generate, $run, $warn, $override, $defer, $debug);
42 40 my($cache_from_cmdline, $no_agent);
43 40 my($run_status);
44  
45 # Option-to-variable mappings for Getopt::Long
46 40 my %basic_options =
47 ('generate' => \$generate,
48 'run' => \$run,
49 'warn' => \$warn,
50 'override' => \$override,
51 'defer' => \$defer,
52 'debug' => \$debug,
53 'autocache' => \$cache_from_cmdline,
54 'no-agent' => \$no_agent,
55 'status' => \$run_status,
56 );
57  
58 40 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # Patterns to extract <variables> or >variables< from a string.
61 40 my $out_angled;
62 40 $out_angled = qr/ < ( [^<>] | (??{$out_angled}) )* > /x;
63 # open angle-bracket then ...
64 # non-angle chars ...
65 # or ...
66 # another angle-bracketed item ...
67 # if there are any ...
68 # and a close angle-bracket
69 40 my $in_angled;
70 40 $in_angled = qr/ > ( [^<>] | (??{$in_angled}) )* < /x;
71 # open angle-bracket then ...
72 # non-angle chars ...
73 # or ...
74 # another angle-bracketed item ...
75 # if there are any ...
76 # and a close angle-bracket
77 40 my $in_or_out_bracketed = qr/ ($out_angled) | ($in_angled) /x;
78  
79 ################################
80 # Basic class methods.
81  
82 # Create the object.
83 # - load and install plugins
84 # - make object available to test specs for callbacks
85 # - clear the tests and test count
86 # - process the command-line options
87 # - return the object
88 44 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
89 44 my ($class) = @_;
90 44 my $self = {};
91 44 bless $self, $class;
92  
93 44 $self->_deps(Graph->new);
94 44 $self->sub_engine($sub_engine);
95  
96 # initialize fields first; plugins may expect good values.
97 44 $self->next_line_callbacks([]);
98 44 $self->tests([]);
99 44 $self->test_count(0);
100 44 $self->{InputQueue} = [];
101  
102 # Load and install the plugins.
103 44 $self->_load_plugins();
104 44 $self->install_pragma_plugins;
105  
106 # TestSpec needs to be able to find the App object.
107 44 App::SimpleScan::TestSpec->app($self);
108  
109 # Read the command line and process the options.
110 44 $self->handle_options;
111  
112 44 return $self;
113 }
114  
115 # Read the test specs and turn them into tests.
116 # Add any additional code from the plugins.
117 # Return the tests as a string.
118 25 ▶sub create_tests {
Subroutine create_tests: called CC=1 SCAR=0.0
Pod documented
119 25 my ($self) = @_;
120  
121 25 $self->transform_test_specs;
122 25 $self->finalize_tests;
123 25 return join q{}, @{$self->tests};
124 }
125  
126 # If the tests should be run, run them.
127 # Return any exceptions to the caller.
128 22 ▶sub execute {
Subroutine execute: called CC=2 SCAR=6.9
Pod documented
129 22 my ($self, $code) = @_;
130 22 ▶ eval $code if ${$self->run}; ##no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->run;} 1 21 22
131 22 return $EVAL_ERROR;
132 }
133  
134 # Actually use the object.
135 # - create tests from input
136 # - run them if we should
137 # - print them if we should
138 25 ▶sub go {
Subroutine go: called CC=5 SCAR=16.9
Pod documented
139 25 my($self) = @_;
140 25 my $exit_code = 0;
141  
142 25 my $code = $self->create_tests;
143 # Note the dereference of the scalars here.
144  
145 25 ▶ if ($self->test_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_count) { } 22 3 25
146 22 ▶ if (my $result = $self->execute($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->execute($code)) 0 22 22
147 0 warn $result,"\n";
148 0 $exit_code = 1;
149 }
150 }
151 else {
152 3 ▶ if (${$self->warn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->warn;}) 0 3 3
153 0 $self->stack_test(qq(fail "No tests were found in your input file.\n"));
154 0 $exit_code = 1;
155 }
156 }
157  
158 25 ▶ if (${$self->generate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->generate;}) 24 1 25
159 24 print $code,"\n";
160 }
161  
162 25 return $exit_code;
163 }
164  
165 # Read files from command line or standard input.
166 # Turn these from test specs into test code.
167 25 ▶sub transform_test_specs {
Subroutine transform_test_specs: called CC=13 SCAR=26.0
Pod documented
168 25 my ($self) = @_;
169 25 local $_; ##no critic
170 25 ▶ while(defined($_ = $self->next_line)) { ##no critic
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->next_line)) 194 25 219
171 194 chomp;
172 # Discard comments.
173 194 ▶ /^\#/mx and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\#/mx and next 8 186 194
174  
175 # Discard blank lines.
176 186 ▶ /^\s*$/mx and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*$/mx and next 6 180 186
177  
178 # Handle pragmas.
179 /^%% # pragma signifier
180 \s* # optional space
181 (.*?) # arbitrary identifier
182 ( # either ...
183 (
184 (:\s+|\s+) # an optional colon, and whitespace
185 (.*)$ # then a optional value
186 )| # ... or
187 $ # nothing at all
188 180 ▶ )/mx and do {
Branch: 1 branch
BranchTrueFalseTotal
/^%% # pragma signifier \s* # optional space (.*?) # arbitrary identifier ( # either ... ( (:\s+|\s+) # an optional colon, and whitespace (.*)$ # then a optional value )| # ... or $ # nothing at all )/mx and do { if (my $code = $self->pragma($1)) { &$code($self, $5); } else { if (defined $5) { my $var = $1; if ($var =~ /$in_or_out_bracketed/) { next if $self->_queue_substituted_lines($_); } ; my(@data) = $self->expand_backticked($5); () = $self->_check_dependencies($var, @data); if ($status) { $self->_substitution_data($var, @data); } else { my @items = split(/$message/, $_, 0); my $between = @items < 3 ? "between" : "among"; $self->stack_test("fail", "\"Cannot", "add", "substitution", "for", "\$var:", "dependency", "loop", "\$between", "\$message\";\\n"); } ; } ; } ; next } 69 111 180
189 69 ▶ if (my $code = $self->pragma($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->pragma($1)) { } 2 67 69
190 2 $code->($self,$5);
191 }
192 else {
193 # It's a substitution if it has no other meaning.
194 67 ▶ if (defined $5) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $5) 67 0 67
195 67 my $var = $1;
196 # Check if the variable name needs to be substituted.
197 67 ▶ if ($var =~ /$in_or_out_bracketed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /$in_or_out_bracketed/) 3 64 67
198 3 ▶ next if $self->_queue_substituted_lines($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_queue_substituted_lines($_) 3 0 3
199 }
200 64 my @data = $self->expand_backticked($5);
201 64 my ($status, $message) = $self->_check_dependencies($var, @data);
202 64 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 64 0 64
203 64 $self->_substitution_data($var, @data);
204 }
205 else {
206 0 my @items = split $message;
207 0 ▶ my $between = (@items < 3) ? 'between' : 'among';
Branch: 1 branch
BranchTrueFalseTotal
@items < 3 ? : 0 0 0
208 0 $self->stack_test( qw(fail "Cannot add substitution for $var: dependency loop $between $message";\n));
209 }
210 }
211 }
212 66 next;
213 };
214  
215 # Commit any substitutions.
216 # We use 'next' because the substituted lines
217 # will have been queued on the input if there
218 # where any substitutions.
219 #
220 # We do this *after* pragma processing because
221 # if we have this:
222 # %%foo bar baz
223 # %%quux <foo> is the value
224 #
225 # and we expanded pragmas in place, we'd get
226 # %%foo bar baz
227 # %%quux bar
228 # %%quux baz
229 #
230 # which is probably *not* what is wanted.
231 # Putting this here makes sure that we only
232 # substitute into actual test specs.
233 111 ▶ next if $self->_queue_substituted_lines($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_queue_substituted_lines($_) 17 94 111
234  
235 # No substitutions in this line, so just process it.
236 94 my $item = App::SimpleScan::TestSpec->new($_);
237  
238 # Store it in case a plugin needs to look at the
239 # test spec in an overriding method.
240 94 $self->set_current_spec($item);
241  
242 94 ▶ if ($item->syntax_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->syntax_error) { } 50 44 94
243 50 $self->stack_code(<<"END_MSG");
244 50 # @{[$item->raw]}
245 # Possible syntax error in this test spec
246 END_MSG
247 }
248 else {
249 44 $item->as_tests;
250 44 local $_ = $item ->raw; ##no critic
251 44 s/\n//mx;
252 }
253 # Drop the spec (there isn't one active now).
254 94 $self->set_current_spec();
255 }
256 25 return;
257 }
258  
259 # Calls each plugin's test_modules method
260 # to stack any other test modules needed to
261 # properly handle the test code. (Plugins may
262 # want to generate test code that needs
263 # something like Test::Differences, etc. -
264 # this lets them load that module so the
265 # tests actually work.)
266 #
267 # Also adds the test plan.
268 #
269 # Finally, initializes the user agent (unless
270 # we're specifically directed *not* to do so).
271 25 ▶sub finalize_tests {
Subroutine finalize_tests: called CC=5 SCAR=18.7
Pod documented
272 25 my ($self) = @_;
273 25 my @tests = @{$self->tests};
274 25 my @prepends;
275 25 foreach my $plugin (__PACKAGE__->plugins) {
276 0 ▶ if ($plugin->can('test_modules')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("test_modules")) 0 0 0
277 0 foreach my $module ($plugin->test_modules) {
278 0 push @prepends, "use $module;\n";
279 }
280 }
281 }
282 # Handle conditional user agent initialization.
283 # This was added because some servers (e.g., WAP
284 # servers) refuse connections from known user agents,
285 # but others (e.g., Yahoo!'s web servers) refuse
286 # login attempts from non-browser user agents.
287 #
288 # Set the user agent unless --no-agent was given.
289  
290 25 ▶ if (!$self->no_agent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_agent) 0 25 25
291 0 push @prepends, qq(mech->agent_alias("Windows IE 6");\n);
292 }
293  
294 # Add the boilerplate testing stuff.
295 25 unshift @prepends,
296 (
297 25 "use Test::More tests=>@{[$self->test_count]};\n",
298 "use Test::WWW::Simple;\n",
299 "use strict;\n",
300 "\n",
301 );
302  
303  
304 25 $self->tests( [ @prepends, @tests ] );
305 25 return;
306 }
307  
308 #######################
309 # External utility methods.
310  
311 # Handle backticked values in substitutions.
312 71 ▶sub expand_backticked {
Subroutine expand_backticked: called CC=6 SCAR=18.0
Pod documented
313 40 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 71 my ($self, $text) = @_;
316  
317 # The state machine was a really cool idea, except it didn't work. :-P
318 # A little reading in Mastering Regular Expressions gave me the patterns
319 # shown below for matching quoted strings.
320  
321 # For an explanation of why this works, see Friedl, p. 262 ff.
322 # It's called "unrolling" the regex there.
323  
324 # Pattern: quote (nonspecial)*(escape anything (nonspecial)*)* quote
325 71 my $qregex = qr/'[^'\\]*(?:\\.[^'\\]*)*'/;
326 71 my $qqregex = qr/"[^"\\]*(?:\\.[^"\\]*)*"/;
327 71 my $qxregex = qr/`[^`\\]*(?:\\.[^`\\]*)*`/;
328  
329 # Plus we need the cleanup tokenizer: match anything nonblank. This
330 # picks up tokens that are not properly-balanced quoted strings.
331 # We'll trap those later, or not. We'll see.
332 71 my $cleanup = qr/\S+/;
333  
334 # An item is a quoted string of any flavor, or the cleanup item.
335 # We turn on the capturing parens here because it we match an item,
336 # we want it.
337 71 my $item = qr/($qqregex|$qregex|$qxregex|$cleanup)/;
338  
339 # So now, to extract the items, we just match this with /g against the
340 # incoming text. We know already this is a single line, so we don't need
341 # any other switches. Boy, that's simpler.
342 71 my @data = ($text =~ /$item/g);
343  
344 # Evaluate the tokens.
345 71 my @result;
346 71 local $_;
347 71 for (@data) {
348 149 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 149 149
349  
350 # Backticked: eval and process again.
351 149 ▶ if (/^\`(.*)`$/mx) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^\`(.*)`$/mx) { } 0 149 149
elsif (/^"(.*)"$/mx) { } 1 148 149
elsif (/^'(.*)'$/mx) { } 14 134 148
352 0 push @result, $self->expand_backticked(eval $_); ##no critic
353 }
354 # Double-quoted: eval it.
355 elsif (/^"(.*)"$/mx) {
356 1 my $to_be_evaled = $1;
357 1 my @substituted = $self->sub_engine->expand($to_be_evaled);
358 1 push @result, map { eval $_ } @substituted; ##no critic
359 }
360 # Single-quoted: remove quotes.
361 elsif (/^'(.*)'$/mx) {
362 14 push @result, $1;
363 }
364 # Just an unquoted token. Save it.
365 else {
366 134 push @result, $_;
367 }
368 }
369 71 return @result;
370 }
371  
372 188 ▶sub set_current_spec {
Subroutine set_current_spec: called CC=1 SCAR=0.0
Pod documented
373 188 my ($self, $testspec) = @_;
374 188 $self->{CurrentTestSpec} = $testspec;
375 188 return $testspec;
376 }
377  
378 0 ▶sub get_current_spec {
Subroutine get_current_spec: not called CC=1 SCAR=6.9
Pod documented
379 0 my ($self) = @_;
380 0 return $self->{CurrentTestSpec};
381 }
382  
383 # If there are any substitutions, build them, stack them on the input,
384 # and return true. Otherwise, just return false so the line will be passed on.
385 114 ▶sub _queue_substituted_lines {
Subroutine _queue_substituted_lines: called CC=3 SCAR=11.0
386 114 my ($self, $line) = @_;
387 114 my @results = $self->sub_engine->expand($line);
388  
389 114 ▶ if (@results != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@results != 1) { } 11 103 114
elsif ($results[0] ne $line) { } 9 94 103
390 # substitutions definitely happened
391 11 $self->queue_lines(@results);
392 11 return 1;
393 }
394 elsif ($results[0] ne $line) {
395 # single line is different, so substitution(s) happened
396 9 $self->queue_lines(@results);
397 9 return 1;
398 }
399 else {
400 # nothing happened, just process it as is
401 94 return 0;
402 }
403 }
404  
405 # Wrapper function for setter/getter that implements the
406 # override function (command-line substitutions override
407 # substitutions in the input).
408 80 ▶sub _substitution_data {
Subroutine _substitution_data: called CC=7 SCAR=19.5
409 80 my ($self, $pragma_name, @pragma_values) = @_;
410 80 ▶ if (! defined $pragma_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pragma_name) 1 79 80
411 1 croak 'No pragma specified';
412 }
413  
414 79 ▶ if (@pragma_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pragma_values) { } 71 8 79
415 71 ▶ if (${$self->override} and
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->override;} and $self->{'Predefs'}{$pragma_name}) { } 5 66 71
Condition100%3/3
${$self->override;} and $self->{"Predefs"}{$pragma_name}
!ll&&!rl&&r
6515
MC/DC100%2/2
${$self->override;} and $self->{"Predefs"}{$pragma_name}
${$self->override;} $self->{"Predefs"}{$pragma_name}
Truth table100%3/3
A and B
A= ${$self->override;}B= $self->{"Predefs"}{$pragma_name}
ABresult
0X0
100
111
416 $self->{Predefs}->{$pragma_name}) {
417 5 ▶ if (${$self->debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->debug;}) 2 3 5
418 2 $self->stack_code(qq(diag "Substitution $pragma_name not altered to '@pragma_values'";\n));
419 }
420 }
421 else {
422 66 $self->sub_engine->substitution_value($pragma_name, @pragma_values);
423 }
424 }
425 else {
426 8 $self->sub_engine->substitution_value($pragma_name);
427 }
428 return
429 5 wantarray ? @{$self->sub_engine->dictionary->{$pragma_name}}
430 79 ▶ : $self->sub_engine->dictionary->{$pragma_name};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 74 79
431 }
432  
433 1 ▶sub _delete_substitution {
Subroutine _delete_substitution: called CC=1 SCAR=0.0
434 1 my ($self, $substitution) = @_;
435 1 return $self->sub_engine->delete_substitution($substitution);
436 }
437  
438 ########################
439 # Options methods
440  
441 44 ▶sub handle_options {
Subroutine handle_options: called CC=7 SCAR=19.7
Pod documented
442 44 my ($self) = @_;
443  
444 # Handle options, including ones from the plugins.
445 44 $self->install_options(%basic_options);
446  
447 # The --define option has to be handled slightly differently.
448 # We set things up so that we have a hash of predefined variables
449 # in the object; that way, we can set them up appropriately, and
450 # know whether or not they should be checked for override/defer
451 # when a definition is found in the simple_scan input file.
452 44 $self->{Options}->{'define=s%'} = ($self->{Predefs} = {});
453  
454 44 foreach my $plugin (__PACKAGE__->plugins) {
455 1 ▶ if ($plugin->can('options')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("options")) 1 0 1
456 1 $self->install_options($plugin->options);
457 }
458 }
459  
460 44 $self->parse_command_line;
461  
462 44 foreach my $plugin (__PACKAGE__->plugins) {
463 1 ▶ if ($plugin->can('validate_options')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("validate_options")) 1 0 1
464 1 $plugin->validate_options($self);
465 }
466 }
467  
468 # If anything was predefined, save it in the substitutions.
469 44 for my $def (keys %{$self->{Predefs}}) {
470 $self->sub_engine->substitution_value($def,
471 10 (split /\s+/mx, $self->{Predefs}->{$def}));
472 }
473  
474 44 ▶ if (${$self->no_agent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->no_agent;}) { } 0 44 44
475 0 $self->sub_engine->substitution_value('agent', 'WWW::Mechanize::Pluggable');
476 }
477 else {
478 44 $self->sub_engine->substitution_value('agent', 'Windows IE 6');
479 44 $self->stack_code("mech->agent_alias('Windows IE 6');\n");
480 }
481  
482 44 $self->app_defaults;
483 44 return;
484 }
485  
486 # Set up application defaults.
487 44 ▶sub app_defaults {
Subroutine app_defaults: called CC=6 SCAR=17.9
Pod documented
488 44 my ($self) = @_;
489 # Assume --run if neither --run nor --generate.
490 44 ▶ if (!defined ${$self->generate()} and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$self->generate;} or defined ${$self->run;}) 15 29 44
Condition100%3/3
defined ${$self->generate;} or defined ${$self->run;}
l!l&&r!l&&!r
27215
MC/DC100%2/2
defined ${$self->generate;} or defined ${$self->run;}
defined ${$self->generate;} defined ${$self->run;}
Truth table100%3/3
A or B
A= defined ${$self->generate;}B= defined ${$self->run;}
ABresult
1X1
011
000
491 17 !defined ${$self->run()}) {
492 15 $self->run(\1);
493 }
494  
495 # Assume --defer if neither --defer nor --override.
496 44 ▶ if (!defined ${$self->defer()} and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$self->defer;} or defined ${$self->override;}) 40 4 44
Condition100%3/3
defined ${$self->defer;} or defined ${$self->override;}
l!l&&r!l&&!r
1340
MC/DC100%2/2
defined ${$self->defer;} or defined ${$self->override;}
defined ${$self->defer;} defined ${$self->override;}
Truth table100%3/3
A or B
A= defined ${$self->defer;}B= defined ${$self->override;}
ABresult
1X1
011
000
497 43 !defined ${$self->override()}) {
498 40 $self->defer(\1);
499 }
500  
501 # if --cache was supplied, turn caching on.
502 44 ▶ if (${$self->autocache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->autocache;}) 0 44 44
503 0 $self->stack_code(qq(cache;\n));
504 }
505  
506 44 return;
507 }
508  
509 # Transform the options specs (whether from here or
510 # from plugins) into methods that we can call to
511 # set/get the option values.
512 46 ▶sub install_options {
Subroutine install_options: called CC=7 SCAR=19.6
Pod documented
513 46 my ($self, @options) = @_;
514 # If plugin returned options as anonymous array, unpack it.
515 46 ▶ if (@options != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options != 2) 44 2 46
516 44 ▶ if (@options == 1 and ref $options[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@options == 1 and ref $options[0] eq "ARRAY") 0 44 44
Condition33%1/3
@options == 1 and ref $options[0] eq "ARRAY"
!ll&&!rl&&r
4400
MC/DC0%0/2
@options == 1 and ref $options[0] eq "ARRAY"
@options == 1 ref $options[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @options == 1B= ref $options[0] eq "ARRAY"
ABresult
0X0
100
111
517 0 @options = @{ $options[0] };
518 }
519 }
520 46 ▶ if (! defined $self->{Options}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Options"}) 44 2 46
521 44 $self->{Options} = {};
522 }
523  
524 # precompilation versions of the possible methods. These
525 # get compiled right when we need them, causing $option
526 # to be capturd as a closure.
527  
528 46 ▶ while (@options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 398 46 444
529 # This coding is deliberate.
530 #
531 # We want a separate copy of $option and
532 # $receiver each time; we don't want a new
533 # copy of @options, because we want to keep
534 # effectively shifting two values off each
535 # time around the loop.
536 #
537 # Note that the generated method returns a
538 # reference to the variable that the option is
539 # stored into; this makes it simpler to code
540 # the accessor here. If you use an array or
541 # hash to receive values in your Getopt spec,
542 # you'll have to dereference it properly in
543 # your code.
544 398 (my($option, $receiver), @options) = @options;
545  
546 # Method names containing dashes are a no-no;
547 # swap them to underscores. (This is okay because
548 # no one outside this module should be trying to
549 # call these methods directly.)
550 398 $option =~ s/-/_/mxg;
551  
552 398 $self->{Options}->{$option} = $receiver;
553  
554 # Ensure that the variables have been cleared if we create another
555 # App::SimpleScan object (normally we won't, but our tests do).
556 398 ${ $receiver } = undef;
557  
558 # Create method if it doesn't exist.
559 398 ▶ if (! $self->can($option)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($option)) 343 55 398
560 40 ▶ use Sub::Installer;
Subroutine BEGIN: called CC=1 SCAR=0.0
561 __PACKAGE__->install_sub(
562 464 ▶ { $option => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
563 464 my ($self, $value) = @_;
564 464 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 55 409 464
565 55 $self->{Options}->{$option} = $value;
566 }
567 464 return $self->{Options}->{$option};
568 }
569 }
570 343 );
571 }
572 }
573 46 return;
574 }
575  
576 # Load all the plugins.
577 44 ▶sub _load_plugins {
Subroutine _load_plugins: called CC=6 SCAR=18.2
578 44 my($self) = @_;
579  
580 # Load plugins.
581 44 foreach my $plugin (__PACKAGE__->plugins) {
582 1 eval "use $plugin"; ##no critic
583 1 ▶ $EVAL_ERROR and die "Plugin $plugin failed to load: $EVAL_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
$App::SimpleScan::EVAL_ERROR and die "Plugin $plugin failed to load: $App::SimpleScan::EVAL_ERROR\n" 0 1 1
584 }
585  
586 # Install source filters
587 44 $self->{Filters} = [];
588 44 foreach my $plugin (__PACKAGE__->plugins) {
589 1 ▶ if ($plugin->can('filters')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("filters")) 1 0 1
590 1 push @{$self->{Filters}}, $plugin->filters();
591 }
592 # Initialize plugin data if possible.
593 1 ▶ if ($plugin->can('init')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("init")) 1 0 1
594 1 $plugin->init($self);
595 }
596 }
597  
598 44 return;
599 }
600  
601 # Call Getopt::Long to parse the command line.
602 45 ▶sub parse_command_line {
Subroutine parse_command_line: called CC=3 SCAR=12.0
Pod documented
603 45 my ($self) = @_;
604 45 ▶ return GetOptions(%{$self->{Options}}) if ref $self->{Options} eq 'HASH' and keys %{$self->{Options}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"Options"} eq "HASH" and keys %{$$self{"Options"};} 45 0 45
Condition33%1/3
ref $self->{"Options"} eq "HASH" and keys %{$$self{"Options"};}
!ll&&!rl&&r
0045
MC/DC0%0/2
ref $self->{"Options"} eq "HASH" and keys %{$$self{"Options"};}
ref $self->{"Options"} eq "HASH" keys %{$$self{"Options"};}
Truth table33%1/3
A and B
A= ref $self->{"Options"} eq "HASH"B= keys %{$$self{"Options"};}
ABresult
0X0
100
111
605 }
606  
607 # Install any pragmas supplied by plugins.
608 # We reuse this same code to install all of
609 # the locally defined pragmas.
610 44 ▶sub install_pragma_plugins {
Subroutine install_pragma_plugins: called CC=5 SCAR=16.1
Pod documented
611 44 my ($self) = @_;
612  
613 44 foreach my $plugin (@local_pragma_support,
614 __PACKAGE__->plugins) {
615 133 ▶ if (ref $plugin eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $plugin eq 'ARRAY') { } 132 1 133
elsif ($plugin->can('pragmas')) { } 1 0 1
616 132 $self->pragma(@{ $plugin });
617 }
618 elsif ($plugin->can('pragmas')) {
619 1 foreach my $pragma_spec ($plugin->pragmas) {
620 1 $self->pragma(@{ $pragma_spec });
621 }
622 }
623 }
624 44 return;
625 }
626  
627 ########################
628 # Pragma methods and handlers
629  
630 # Find the pragma code associated with the name.
631 202 ▶sub pragma {
Subroutine pragma: called CC=3 SCAR=11.0
Pod documented
632 202 my ($self, $name, $pragma) = @_;
633 202 ▶ die "You forgot the pragma name\n" if ! defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 202 202
634 202 ▶ if (defined $pragma) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pragma) 133 69 202
635 133 $self->{Pragma}->{$name} = $pragma;
636 }
637 202 return $self->{Pragma}->{$name};
638 }
639  
640 # %%agent pragma handler. Verify that the argument
641 # is a valid WW::Mechanize agent alias string, and
642 # stack code to change it as appropriate.
643 0 ▶sub _do_agent {
Subroutine _do_agent: not called CC=2 SCAR=17.9
644 0 my ($self, $rest) = @_;
645 0 $rest = reverse $rest;
646 0 my ($maybe_agent) = ($rest =~/^\s*(.*)$/mx);
647  
648 0 $maybe_agent = reverse $maybe_agent;
649 0 ▶ if (grep { $_ eq $maybe_agent } $reference_mech->known_agent_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $maybe_agent;} $reference_mech->known_agent_aliases) 0 0 0
650 0 $self->_substitution_data('agent', $maybe_agent)
651 }
652 0 $self->stack_code(qq(user_agent("$maybe_agent");\n));
653 0 return;
654 }
655  
656 # %%cache - turn on Test::WWW::Simple's cache.
657 1 ▶sub _do_cache {
Subroutine _do_cache: called CC=1 SCAR=0.0
658 1 my ($self,$rest) = @_;
659 1 $self->stack_code("cache();\n");
660 1 return;
661 }
662  
663 # %%nocache - turn off Test::WWW::Simple's cache.
664 1 ▶sub _do_nocache {
Subroutine _do_nocache: called CC=1 SCAR=0.0
665 1 my ($self,$rest) = @_;
666 1 $self->stack_code("no_cache();\n");
667 1 return;
668 }
669  
670 ##########################
671 # Input queueing
672  
673 # Handle input queueing. If there's anything queued,
674 # return it first; otherwise, just read another line
675 # from the magic input filehandle.
676 223 ▶sub next_line {
Subroutine next_line: called CC=5 SCAR=16.2
Pod documented
677 223 my ($self) = shift;
678 223 my $next_line;
679  
680 # Call and plugin-installed input callbacks.
681 # These can do whatever they like to the line stack, the
682 # object, etc.
683 223 foreach my $callback (@ {$self->next_line_callbacks() }) {
684 0 $callback->($self);
685 }
686  
687 # If we have lines on the input queue, read from there.
688 223 ▶ if (defined $self->{InputQueue}->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'InputQueue'}[0]) { } 69 154 223
689 69 $next_line = shift @{ $self->{InputQueue} };
690 }
691  
692 # Else we read lines from the standard input.
693 else {
694 154 $next_line = <>;
695 154 ▶ if (defined $next_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_line) 129 25 154
696 129 $next_line =~ s/\n//mx;
697 129 ▶ if ($run_status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_status) 0 129 129
698 0 print STDERR "# |Processing '$next_line' (line $.)\n";
699 }
700 }
701 }
702  
703 # record the text of the last line read for plugins to access
704 # if they need it.
705 223 $self->last_line($next_line);
706  
707 223 return $next_line;
708 }
709  
710 # Preserve current line so that plugins can look at it
711 # if they want to.
712 223 ▶sub last_line {
Subroutine last_line: called CC=2 SCAR=6.9
Pod documented
713 223 my ($self, $line) = @_;
714 223 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 198 25 223
715 198 $self->{CurrentLine} = $line;
716 }
717 223 return $self->{CurrentLine};
718 }
719  
720 # Handle input stacking by pragmas. Add any new lines
721 # to the head of the queue.
722 22 ▶sub queue_lines {
Subroutine queue_lines: called CC=1 SCAR=0.0
Pod documented
723 22 my ($self, @lines) = @_;
724 22 $self->{InputQueue} = [ @lines, @{ $self->{InputQueue} } ];
725 22 return;
726 }
727  
728 ###########################
729 # Output queueing
730  
731 # stack_code just adds code to the array holding
732 # the generated program.
733 143 ▶sub stack_code {
Subroutine stack_code: called CC=1 SCAR=0.0
Pod documented
734 143 my ($self, @code) = @_;
735 143 my @old_code = @{$self->tests};
736 143 $self->tests([@old_code, @code]);
737 return
738 143 }
739  
740 # stack_test adds code to the array holding
741 # the generated program, and bumps the test
742 # count so we can use the proper number of tests
743 # in our test plan.
744 45 ▶sub stack_test {
Subroutine stack_test: called CC=2 SCAR=7.0
Pod documented
745 45 my($self, @code) = @_;
746 45 for my $filter (@{$self->{Filters}}) {
747 # Called with $self to make it appear
748 # as if it's a method call from this package.
749 0 @code = $filter->($self, @code);
750 }
751 45 $self->stack_code(@code);
752 45 return $self->test_count($self->test_count()+1);
753 }
754  
755 ##################################
756 # Dependency checking
757  
758 # It's necessary to make sure that the substitution pragmas
759 # don't have looping dependencies; these would cause the
760 # input stack to grow without limit as it tries to resolve
761 # all of the substitutions.
762 #
763 # All we have to do is make sure that the graph of variable
764 # relations is a directed acyclic graph. Since actually writing
765 # all that would be a pain, we use Graph.pm to manage it for us.
766  
767 64 ▶sub _check_dependencies {
Subroutine _check_dependencies: called CC=3 SCAR=11.1
768 64 my ($self, $var, @dependencies) = @_;
769 64 my $graph = $self->_deps;
770  
771 # drop anything that's not a variable definition.
772 64 @dependencies = grep { /^<.*>$/mx } @dependencies;
773  
774 # No variables, no dependencies.
775 64 ▶ if (! @dependencies) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dependencies) 62 2 64
776 62 return 1, 'no dependencies';
777 }
778  
779 # Add the new dependencies.
780 2 $self->_depend($var, @dependencies);
781  
782 # Run a topological sort to make sure that the
783 # graph remains a DAG. If not, returns a cycle.
784 # Note that it's possible that there's more than one
785 # cycle, though not liekely, since we're checking
786 # every time we add a new variable.
787 2 ▶ unless ($graph->is_dag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($graph->is_dag) 0 2 2
788 0 return (0, $graph->find_a_cycle);
789 }
790  
791 2 return 1, "dependencies OK";
792 }
793  
794 2 ▶sub _depend {
Subroutine _depend: called CC=4 SCAR=15.6
795 2 my($self, $item, @dependencies) = @_;
796  
797 2 ▶ if (!defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 2 2
798 0 die "You don't want to do that anymore";
799 }
800  
801 2 ▶ if (!@dependencies) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dependencies) 0 2 2
802 0 return ([ $self->_deps->successors($item) ]);
803 }
804  
805 # Add these dependencies for the item.
806 2 $self->_deps->add_edge($item, $_) for @dependencies; ## no critic
807 2 return;
808 }
809  
810 40 1; # Magic true value required at end of module
811 __END__
« bin/simple_report ↑ Summary blib/lib/App/SimpleScan/Substitution.pm »