| 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
Condition33%1/3
$run or $generate
MC/DC0%0/2
$run or $generate
$run
$generate
Truth table33%1/3
A or B
A= $runB= $generate
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 96 | $_ = shift @lines; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 96 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 27 | # Discard comments. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 96 | ▶ | /^#/ and next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # Discard blank lines. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 83 | ▶ | /^\s*$/ and next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (/^%%xx: (([a-z1-9]{2,}(\s+|$))+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | $agent = $1; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | my @aliases = WWW::Mechanize::known_agent_aliases(); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | unless (grep {/$agent/} @aliases) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | ▶ | eval(join '',@tests) if $run; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | $@ and warn $@,"\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | warn "# No tests were found in your input file.\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $regex and defined $which
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | # Warn about possible errors. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 53 | ▶ | unless (defined $url and defined $regex and defined $which) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $url and defined $regex and defined $which
defined $url and defined $regex
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
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 27 | push @tests, "# $_\n","# Possible syntax error in this test spec\n"; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 27 | ▶ | warn "$_: syntax error" if $warn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 27 | next; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 26 | ▶ | push @tests, qq!page_@{[$which eq 'Y' ? "" : "un"]}! . | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 26 | qq!like("$url", qr/$regex/$switches, "@{[join " ",@comment]}");\n!; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 26 | $number_of_tests++; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||||||||||||||||