examples/simple_scan

filter: Statementstmt 82.6% 76 / 92 Branchbran 74.0% 37 / 50 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 77.0% 131 / 170 CC 13 SCAR 21.4
CC
13
Cov
78%
emit_a_test
13.1
BEGIN
1.0
BEGIN
1.0
CRAP 8.5
↑ Summary blib/lib/Test/WWW/Simple.pm »
1 #!/usr/bin/env perl
2 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Test::WWW::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use constant SHEBANG => "#!$^X";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 my($generate, $run, $warn);
11  
12 9 GetOptions('generate' => \$generate,
13 'run' => \$run,
14 'warn' => \$warn);
15  
16 # Assume run if no flags at all.
17 9 ▶$run++ unless $run or $generate;
Branch: 1 branch
BranchTrueFalseTotal
unless $run or $generate 9 0 9
Condition33%1/3
$run or $generate
l!l&&r!l&&!r
009
MC/DC0%0/2
$run or $generate
$run $generate
Truth table33%1/3
A or B
A= $runB= $generate
ABresult
1X1
011
000
18  
19 9 my @tests;
20 my @countries;
21 9 my $agent = "Windows IE 6";
22 9 my $number_of_tests;
23 9 my @lines = <>;
24 9 ▶while(@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 96 9 105
25 96 $_ = shift @lines;
26 96 chomp;
27 # Discard comments.
28 96 ▶ /^#/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^#/ and next 13 83 96
29  
30 # Discard blank lines.
31 83 ▶ /^\s*$/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*$/ and next 22 61 83
32  
33 # First, look for any of our pragmas.
34 # These all start with %% and a pragma name.
35  
36 # %%cache
37 # This pragma tells simple_scan to cache all URLs.
38 61 ▶ /^%%cache/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^%%cache/ and do { push @tests, "cache();\n"; next } 2 59 61
39 2 push @tests, "cache();\n";
40 2 next;
41 };
42  
43 # %%nocache
44 # This pragma tells simple_scan to not cache URLs.
45 59 ▶ /^%%nocache/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^%%nocache/ and do { push @tests, "no_cache();\n"; next } 2 57 59
46 2 push @tests, "no_cache();\n";
47 2 next;
48 };
49  
50 # %xx: es de au ..
51 # This pragma lets us define a list of country IDs
52 # to be substituted into the URLs following it. We look for ">xx<"
53 # in the URLs to find the characters we'll replace.
54 57 ▶ /^%%xx:/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^%%xx:/ and do { if (/^%%xx: (([a-z1-9]{2,}(\s+|$))+)/) { my $countries = $1; @countries = split(/\s+/, $countries, 0); } else { die "Invalid '%%xx:' pragma: must be '%%xx: ' followed by two (or more) character country IDs.\n"; } ; next } 0 57 57
55 0 ▶ if (/^%%xx: (([a-z1-9]{2,}(\s+|$))+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%xx: (([a-z1-9]{2,}(\s+|$))+)/) { } 0 0 0
56 0 my $countries = $1;
57 0 @countries = split(/\s+/, $countries);
58 }
59 else {
60 0 die "Invalid '%%xx:' pragma: must be '%%xx: ' followed by two (or more) character country IDs.\n";
61 }
62 0 next;
63 };
64  
65 # %agent: agent_alias
66 # This pragma tells us to switch the user agent to the one specified.
67 # if it's not a valid user agent, we die.
68 57 ▶ /%%agent: (.*)/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/%%agent: (.*)/ and do { $agent = $1; my(@aliases) = WWW::Mechanize::known_agent_aliases(); unless (grep {/$agent/;} @aliases) { die "$agent is an invalid user agent alias/"; } ; push @tests, qq[Test::WWW::Simple::user_agent("$agent");\n]; next } 4 53 57
69 4 $agent = $1;
70 4 my @aliases = WWW::Mechanize::known_agent_aliases();
71 4 ▶ unless (grep {/$agent/} @aliases) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/$agent/;} @aliases) 0 4 4
72 0 die "$agent is an invalid user agent alias/";
73 }
74 4 push @tests, qq!Test::WWW::Simple::user_agent("$agent");\n!;
75 4 next;
76 };
77  
78 # if an xx: pragma is in effect, substitute the country for ">xx<"
79 # everywhere in the input record (so we can include the country in the
80 # comment!).
81 53 ▶ if (@countries) {
Branch: 1 branch
BranchTrueFalseTotal
if (@countries) { } 0 53 53
82 0 my @localized;
83 0 for my $country (@countries) {
84 0 my $localized;
85 # don't create localized tests if the test is not localizable.
86 0 ▶ unless (/>xx</) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/>xx</) 0 0 0
87 0 emit_a_test($_);
88 0 last;
89 }
90 # Localize.
91 0 ($localized = $_) =~ s/>xx</$country/g;
92 0 push @localized, $localized;
93 }
94 0 emit_a_test(@localized);
95  
96 # Discard the unlocalized line.
97 0 next;
98 }
99  
100 # No localization in effect. Just make a test.
101 else {
102 53 emit_a_test($_);
103 }
104  
105 }
106  
107 9 ▶if (defined $number_of_tests) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number_of_tests) { } 8 1 9
108 8 unshift @tests,
109 8 "@{[SHEBANG]}\nuse Test::WWW::Simple tests=>$number_of_tests;\n";
110 8 ▶ print @tests if $generate;
Branch: 1 branch
BranchTrueFalseTotal
if $generate 0 8 8
111 8 ▶ eval(join '',@tests) if $run;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $run 8 0 8
112 8 ▶ $@ and warn $@,"\n";
Branch: 1 branch
BranchTrueFalseTotal
$@ and warn $@, "\n" 0 8 8
113 }
114 else {
115 1 ▶ warn "# No tests were found in your input file.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 1 1
116 if $warn;
117 }
118  
119 53 ▶sub emit_a_test {
Subroutine emit_a_test: called CC=13 SCAR=25.8
120 53 my (@input) = @_;
121 # All these extra undefs are the result of using Regexp::Common to
122 # capture the first two fields. Less confusing and error-prone than
123 # actually coding the regexes oneself.
124 53 local $_;
125 53 for (@input) {
126 53 s/>agent</$agent/;
127 53 my($url, undef, undef, undef, undef, undef, undef, undef,
128 undef, undef, $regex, undef,
129 undef, $switches,
130 $which,
131 @comment) =
132 m[$RE{URI}{HTTP}{-keep}\s+ # a URL
133 $RE{delimited}{-delim=>'/'}{-keep} # a regex, in slashes
134 (([sixogim]+)\s+|\s+) # possibly with switches
135 ([yY]|[nN])\s+ # should/shoudn't match
136 (.*)$]x; # test comment
137  
138 # Clean things up a bit.
139  
140 # Make sure the "which" is uppercase for later tests.
141 53 ▶ $which = uc($which) if defined $which;
Branch: 1 branch
BranchTrueFalseTotal
if defined $which 26 27 53
142  
143 # Add url and which way the pattern should match to the comment.
144 53 ▶ push @comment, "[$url]" if defined $url;
Branch: 1 branch
BranchTrueFalseTotal
if defined $url 26 27 53
145  
146 # avoid a warning if the test spec had a syntax error.
147 53 ▶ push @comment, "[/$regex/ " . ($which eq 'Y' ? "should" : "shouldn't") . " match]"
Branch: 2 branches
BranchTrueFalseTotal
if defined $regex and defined $which 26 27 53
$which eq 'Y' ? : 22 4 26
Condition66%2/3
defined $regex and defined $which
!ll&&!rl&&r
27026
MC/DC50%1/2
defined $regex and defined $which
defined $regex defined $which
Truth table66%2/3
A and B
A= defined $regexB= defined $which
ABresult
0X0
100
111
148 if defined $regex and defined $which;
149  
150 # Avoid undef warnings if no regex switches were used.
151 53 ▶ $switches = "" unless defined $switches;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $switches 51 2 53
152  
153 # Warn about possible errors.
154 53 ▶ unless (defined $url and defined $regex and defined $which) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url and defined $regex and defined $which) 27 26 53
Condition66%4/6
defined $url and defined $regex and defined $which
!ll&&!rl&&r
27026
defined $url and defined $regex
!ll&&!rl&&r
27026
MC/DC33%1/3
defined $url and defined $regex and defined $which
defined $url defined $regex defined $which
Truth table50%2/4
A and B and C
A= defined $urlB= defined $regexC= defined $which
ABCresult
0XX0
10X0
1100
1111
155 27 push @tests, "# $_\n","# Possible syntax error in this test spec\n";
156 27 ▶ warn "$_: syntax error" if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 27 27
157 27 next;
158 }
159  
160 26 ▶ push @tests, qq!page_@{[$which eq 'Y' ? "" : "un"]}! .
Branch: 1 branch
BranchTrueFalseTotal
$which eq 'Y' ? : 22 4 26
161 26 qq!like("$url", qr/$regex/$switches, "@{[join " ",@comment]}");\n!;
162 26 $number_of_tests++;
163 }
164 }
165 __END__
↑ Summary blib/lib/Test/WWW/Simple.pm »