File Coverage

examples/simple_scan
Criterion Covered Total %
statement 76 92 82.6
branch 34 48 70.8
condition 7 12 58.3
subroutine 9 9 100.0
pod n/a
total 126 161 78.2


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2 9     9   53391 use warnings;
  9         14  
  9         586  
3 9     9   45 use strict;
  9         14  
  9         302  
4 9     9   9537 use Getopt::Long;
  9         159404  
  9         58  
5 9     9   8177 use Regexp::Common;
  9         27104  
  9         84  
6 9     9   1678408 use WWW::Mechanize;
  9         1867675  
  9         501  
7 9     9   5825 use Test::WWW::Simple;
  9         51  
  9         106  
8              
9 9     9   90 use constant SHEBANG => "#!$^X";
  9         19  
  9         36658  
10 9         2151892 my($generate, $run, $warn);
11              
12 9         102 GetOptions('generate' => \$generate,
13             'run' => \$run,
14             'warn' => \$warn);
15              
16             # Assume run if no flags at all.
17 9 50 33     5296 $run++ unless $run or $generate;
18              
19 9         29 my @tests;
20             my @countries;
21 9         24 my $agent = "Windows IE 6";
22 9         18 my $number_of_tests;
23 9         1289 my @lines = <>;
24 9         106 while(@lines) {
25 96         183 $_ = shift @lines;
26 96         194 chomp;
27             # Discard comments.
28 96 100       267 /^#/ and next;
29              
30             # Discard blank lines.
31 83 100       342 /^\s*$/ and next;
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 100       140 /^%%cache/ and do {
39 2         4 push @tests, "cache();\n";
40 2         7 next;
41             };
42              
43             # %%nocache
44             # This pragma tells simple_scan to not cache URLs.
45 59 100       135 /^%%nocache/ and do {
46 2         5 push @tests, "no_cache();\n";
47 2         4 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 50       154 /^%%xx:/ and do {
55 0 0       0 if (/^%%xx: (([a-z1-9]{2,}(\s+|$))+)/) {
56 0         0 my $countries = $1;
57 0         0 @countries = split(/\s+/, $countries);
58             }
59             else {
60 0         0 die "Invalid '%%xx:' pragma: must be '%%xx: ' followed by two (or more) character country IDs.\n";
61             }
62 0         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 100       151 /%%agent: (.*)/ and do {
69 4         11 $agent = $1;
70 4         17 my @aliases = WWW::Mechanize::known_agent_aliases();
71 4 50       52 unless (grep {/$agent/} @aliases) {
  24         138  
72 0         0 die "$agent is an invalid user agent alias/";
73             }
74 4         10 push @tests, qq!Test::WWW::Simple::user_agent("$agent");\n!;
75 4         12 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 50       113 if (@countries) {
82 0         0 my @localized;
83 0         0 for my $country (@countries) {
84 0         0 my $localized;
85             # don't create localized tests if the test is not localizable.
86 0 0       0 unless (/>xx
87 0         0 emit_a_test($_);
88 0         0 last;
89             }
90             # Localize.
91 0         0 ($localized = $_) =~ s/>xx
92 0         0 push @localized, $localized;
93             }
94 0         0 emit_a_test(@localized);
95              
96             # Discard the unlocalized line.
97 0         0 next;
98             }
99              
100             # No localization in effect. Just make a test.
101             else {
102 53         120 emit_a_test($_);
103             }
104              
105             }
106              
107 9 100       45 if (defined $number_of_tests) {
108 8         16 unshift @tests,
109 8         54 "@{[SHEBANG]}\nuse Test::WWW::Simple tests=>$number_of_tests;\n";
110 8 50       28 print @tests if $generate;
111 8 50   8   831 eval(join '',@tests) if $run;
  8         69  
  8         17  
  8         97  
112 8 50       7621 $@ and warn $@,"\n";
113             }
114             else {
115 1 50       83 warn "# No tests were found in your input file.\n"
116             if $warn;
117             }
118              
119             sub emit_a_test {
120 53     53   139 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         64 local $_;
125 53         100 for (@input) {
126 53         136 s/>agent
127 53         373 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 100       27890 $which = uc($which) if defined $which;
142              
143             # Add url and which way the pattern should match to the comment.
144 53 100       164 push @comment, "[$url]" if defined $url;
145              
146             # avoid a warning if the test spec had a syntax error.
147 53 100 66     249 push @comment, "[/$regex/ " . ($which eq 'Y' ? "should" : "shouldn't") . " match]"
    100          
148             if defined $regex and defined $which;
149              
150             # Avoid undef warnings if no regex switches were used.
151 53 100       129 $switches = "" unless defined $switches;
152              
153             # Warn about possible errors.
154 53 50 66     199 unless (defined $url and defined $regex and defined $which) {
      66        
155 27         82 push @tests, "# $_\n","# Possible syntax error in this test spec\n";
156 27 50       48 warn "$_: syntax error" if $warn;
157 27         143 next;
158             }
159              
160 26 100       42 push @tests, qq!page_@{[$which eq 'Y' ? "" : "un"]}! .
  26         116  
161 26         139 qq!like("$url", qr/$regex/$switches, "@{[join " ",@comment]}");\n!;
162 26         165 $number_of_tests++;
163             }
164             }
165             __END__