File Coverage

blib/lib/Test/WWW/Simple.pm
Criterion Covered Total %
statement 94 110 85.4
branch 24 34 70.5
condition 2 2 100.0
subroutine 19 22 86.3
pod 11 11 100.0
total 150 179 83.8


line stmt bran cond sub pod time code
1             package Test::WWW::Simple;
2              
3 25     25   2441368 use strict;
  25         60  
  25         1049  
4 25     25   213 use warnings;
  25         59  
  25         2236  
5              
6             our $VERSION = '0.41';
7              
8 25     25   6383 use Test::Builder;
  25         825571  
  25         819  
9 25     25   14192 use Test::LongString;
  25         67846  
  25         176  
10 25     25   9769 use Test::More;
  25         83569  
  25         202  
11 25     25   24549 use WWW::Mechanize::Pluggable;
  25         5413575  
  25         248  
12              
13             my $Test = Test::Builder->new; # The Test:: singleton
14             my $Mech = WWW::Mechanize::Pluggable->new(autocheck => 0);
15             # The Mech user agent and support methods
16             my $cache_results = 0; # default to not caching Mech fetches
17             our $last_url; # last URL fetched successfully by Mech
18             my %page_cache; # saves pages for %%cache; we probably
19             # will want to change this over to a
20             # tied hash later to allow for disk caching
21             # instead of just memory caching.
22             my %status_cache; # ditto
23              
24             $Test::WWW::display_length = 40; # length for display in error messages
25              
26             sub import {
27 32     32   342 my ($class, %args) = @_;
28 32         100 my $caller = caller;
29 25     25   95753 no strict 'refs';
  25         61  
  25         37127  
30 32         96 *{$caller.'::page_like_full'} = \&page_like_full;
  32         215  
31 32         86 *{$caller.'::page_unlike_full'} = \&page_unlike_full;
  32         152  
32 32         80 *{$caller.'::text_like'} = \&text_like;
  32         122  
33 32         74 *{$caller.'::text_unlike'} = \&text_unlike;
  32         194  
34 32         98 *{$caller.'::page_like'} = \&page_like;
  32         134  
35 32         84 *{$caller.'::page_unlike'} = \&page_unlike;
  32         157  
36 32         88 *{$caller.'::user_agent'} = \&user_agent;
  32         178  
37 32         78 *{$caller.'::cache'} = \&cache;
  32         121  
38 32         73 *{$caller.'::no_cache'} = \&no_cache;
  32         150  
39 32         72 *{$caller.'::mech'} = \&mech;
  32         155  
40 32         83 *{$caller.'::last_test'} = \&last_test;
  32         123  
41              
42 32         308 $Test->exported_to($caller);
43              
44             # Check the 'use' arguments to see if we have either
45             # 'agent', 'agent_string', or 'no_agent'.
46             #
47             # If we have none of these, assume 'Windows IE 6'.
48 32 50       685 if (defined $args{agent}) {
    50          
    100          
49             # This is a string suitable for passing to agent_alias.
50 0         0 my $alias = $args{agent};
51 0 0       0 if (grep { /^$alias\z/ } $Mech->known_agent_aliases()) {
  0         0  
52 0         0 $Mech->agent_alias($alias);
53             }
54             else {
55 0         0 die "'$alias' is not a valid WWW::Mechanize user agent alias\n";
56             }
57             }
58             elsif (defined $args{agent_string}) {
59 0         0 $Mech->agent('agent_string');
60             }
61             elsif(!defined $args{no_agent}) {
62 31         386 $Mech->agent_alias('Windows IE 6');
63             }
64             else {
65             # No action; no_agent was defined,
66             # so leave the user agent as "WWW::Mechanize/version".
67             }
68              
69 32 100       21738 if (defined $args{tests}) {
70 8         69 plan tests => $args{tests};
71             }
72             }
73              
74             sub _clean_text {
75 0     0   0 my $page = $Mech->content(format=>'text');
76 0         0 $page =~ s/\xa0/ /g;
77 0         0 return $page;
78             }
79              
80             sub text_like($$;$) {
81 2     2 1 411699 my($url, $regex, $comment) = @_;
82 2         11 my ($state, $content, $status_line) = _fetch($url);
83 2 50       25 $state
84             ? like_string(_clean_text(), $regex, $comment)
85             : fail "Fetch of $url failed: ".$status_line;
86             }
87              
88             sub text_unlike($$;$) {
89 0     0 1 0 my($url, $regex, $comment) = @_;
90 0         0 my ($state, $content, $status_line) = _fetch($url);
91 0 0       0 $state
92             ? unlike_string(_clean_text(), $regex, $comment)
93             : fail "Fetch of $url failed: ".$status_line;
94             }
95              
96             sub page_like($$;$) {
97 35     35 1 551537 my($url, $regex, $comment) = @_;
98 35         177 my ($state, $content, $status_line) = _fetch($url);
99 35 100       341 $state
100             ? like_string($content, $regex, $comment)
101             : fail "Fetch of $url failed: ".$status_line;
102             }
103              
104             sub page_unlike($$;$) {
105 8     8 1 329158 my($url, $regex, $comment) = @_;
106 8         32 my ($state, $content, $status_line) = _fetch($url);
107 8 100       78 $state
108             ? unlike_string($content, $regex, $comment)
109             : fail "Fetch of $url failed: ".$status_line;
110             }
111              
112             sub page_like_full($$;$) {
113 3     3 1 6887 my($url, $regex, $comment) = @_;
114 3         13 my ($state, $content, $status_line) = _fetch($url);
115 3 100       37 $state
116             ? like($content, $regex, $comment)
117             : fail "Fetch of $url failed: ".$status_line;
118             }
119              
120             sub page_unlike_full($$;$) {
121 0     0 1 0 my($url, $regex, $comment) = @_;
122 0         0 my ($state, $content, $status_line) = _fetch($url);
123 0 0       0 $state
124             ? unlike($content, $regex, $comment)
125             : fail "Fetch of $url failed: ".$status_line;
126             }
127              
128             sub _fetch {
129 48     48   140 my ($url, $comment) = @_;
130 48         181 local $Test::Builder::Level = 2;
131 48         143 my @results;
132              
133 48 100       186 if ($cache_results) {
134 8 100       28 if (defined $page_cache{$url}) {
    50          
135             # in cache: return it.
136 6         19 @results = (1, $page_cache{$url}, $status_cache{$url});
137             }
138             elsif ($last_url eq $url) {
139             # "cached" in Mech object
140             @results = (1,
141             $page_cache{$url} = $Mech->content,
142 0         0 $status_cache{$url} = $Mech->response->status_line);
143             }
144             else {
145             # not in cache - load and save the page (if any)
146 2         20 $Mech->get($url);
147             @results = ($Mech->success,
148             $page_cache{$url} = $Mech->content,
149 2         1080712 $status_cache{$url} = $Mech->response->status_line);
150             }
151             }
152             else {
153             # not caching. Just grab it.
154 40         569 $Mech->get($url);
155 40         13829259 @results = ($Mech->success, $Mech->content, $Mech->response->status_line);
156             }
157 48         10546 $last_url = $_[0];
158 48         184 $page_cache{$url} = $results[1];
159 48         140 $status_cache{$url} = $results[2];
160 48         291 @results;
161             }
162              
163             sub _trimmed_url {
164 3     3   197961 my $url = shift;
165 3 100       28 length($url) > $Test::WWW::display_length
166             ? substr($url,0,$Test::WWW::display_length) . "..."
167             : $url;
168             }
169              
170             sub user_agent {
171 7   100 7 1 266398 my $agent = shift || "Windows IE 6";
172 7         70 $Mech->agent_alias($agent);
173             }
174              
175             sub mech {
176 5     5 1 651153 my ($self) = @_;
177 5         70 return $Mech;
178             }
179              
180             sub last_test {
181 1     1 1 1424 my($self) = @_;
182 1         8 return ($Test->details)[-1];
183             }
184              
185             sub cache (;$) {
186 4     4 1 911 my $comment = shift;
187 4 100       29 $Test->note($comment) if defined $comment;
188 4         1118 $last_url = "";
189 4         11 $cache_results = 1;
190 4         40 1;
191             }
192              
193             sub no_cache (;$) {
194 5     5 1 356219 my $comment = shift;
195 5 100       121 $Test->note($comment) if defined $comment;
196 5         1476 $last_url = "";
197 5         35 $cache_results = 0;
198 5         46 1;
199             }
200              
201              
202             1;
203              
204             __END__