blib/lib/Test/HTML/W3C.pm

filter: Statementstmt 26.4% 18 / 68 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.5% 4 / 14 Podpod 100% 7 / 7 totaltotal 24.5% 29 / 118 CC 15 SCAR 23.1
CC
15
Cov
19%
import_extra
20.0
is_valid_markup
12.0
is_valid_file
12.0
CRAP 10.1
↑ Summary
1 package Test::HTML::W3C;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = "0.04";
6  
7 =head1 NAME
8  
9 Test::HTML::W3C - Perform W3C HTML validation testing
10  
11 =head1 SYNOPSIS
12  
13 use Test::HTML::W3C tests => $test_count;
14 # or
15 use Test::HTML::W3C 'show_detail';
16 # or when using both
17 use Test::HTML::W3C tests => $test_count, 'show_detail';
18  
19 is_valid_markup($my_html_scalar);
20  
21 is_valid_file("/path/to/my/file.html");
22  
23 is_valid("http://example.com");
24  
25 # Get the underlying WebService:;Validator::W3C::HTML object
26 my $validator = validator();
27  
28 =head1 DESCRIPTION
29  
30 The purpose of this module is to provide a wrapper around the W3C
31 that works with the L<Test::More> testing framework.
32  
33 =head1 ABUSE
34  
35 Please keep in mind that the W3C validation pages and services are
36 a shared resource. If you plan to do many many tests, please consider
37 using your own installation of the validation programs, and then use
38 your local install by modifying the local validtor:
39  
40 my $v = validator();
41 $v->validator_uri($my_own_validator);
42  
43 See the documentation for WebService:;Validator::W3C::HTML and the W3C's
44 site at http://validator.w3.org/ for details
45  
46 =over 4
47  
48 =cut
49  
50 1 ▶use WebService::Validator::HTML::W3C;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use base qw(Test::Builder::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 @EXPORT = qw(
53 plan
54 diag_html
55 is_valid_markup
56 is_valid_file
57 is_valid
58 validator
59 );
60  
61 1 my $v = WebService::Validator::HTML::W3C->new();
62 1 my $not_checked = 1;
63 1 my $show_detail = 0;
64  
65 0 ▶sub import_extra {
Subroutine import_extra: not called CC=4 SCAR=30.0
Pod documented
66 0 my ($class, $list) = @_;
67 0 my @other = ();
68 0 my $idx = 0;
69 0 ▶ while( $idx <= $#{$list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $#{$list;}) 0 0 0
70 0 my $item = $list->[$idx];
71  
72 0 ▶ if( defined $item and $item eq 'show_detail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item and $item eq 'show_detail') { } 0 0 0
Condition0%0/3
defined $item and $item eq "show_detail"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item and $item eq "show_detail"
defined $item $item eq "show_detail"
Truth table0%0/3
A and B
A= defined $itemB= $item eq "show_detail"
ABresult
0X0
100
111
73 0 $show_detail = 1;
74 0 $v = WebService::Validator::HTML::W3C->new(detailed => 1);
75 } else {
76 0 push @other, $item;
77 }
78 0 $idx++;
79 }
80 0 @$list = @other;
81 }
82  
83 =item validator();
84  
85 B<Description:> Returns the underlying WebService::Validator::HTML::W3C object
86  
87 B<Parameters:> None.
88  
89 B<Returns:> $validator
90  
91 =cut
92  
93 0 ▶sub validator {
Subroutine validator: not called CC=1 SCAR=6.9
Pod documented
94 0 return $v;
95 }
96  
97  
98 =item plan();
99  
100 B<Description:> Access to the underlying C<plan> method provided by
101 L<Test::Builder>.
102  
103 B<Parameters:> As per L<Test::Builder>
104  
105 =cut
106  
107 0 ▶sub plan {
Subroutine plan: not called CC=1 SCAR=6.9
Pod documented
108 0 __PACKAGE__->builder->plan(@_);
109 }
110  
111 0 ▶sub _check_plan {
Subroutine _check_plan: not called CC=2 SCAR=17.9
112 0 $not_checked = 0;
113 0 ▶ if (! __PACKAGE__->builder->has_plan()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not __PACKAGE__->builder->has_plan) 0 0 0
114 0 plan("no_plan");
115 }
116 }
117  
118 =item is_valid_markup($markup[, $name]);
119  
120 B<Description:> is_valid_markup tests whether the text in the provided scalar
121 value correctly validates according to the W3C specifications. This is useful
122 if you have markup stored in a scalar that you wish to test that you might get
123 from using LWP or WWW::Mechanize for example...
124  
125 B<Parameters:> $markup, a scalar containing the data to test, $name, an
126 optional descriptive test name.
127  
128 B<Returns:> None.
129  
130 =cut
131  
132 0 ▶sub is_valid_markup {
Subroutine is_valid_markup: not called CC=3 SCAR=24.8
Pod documented
133 0 ▶ _check_plan() if $not_checked;
Branch: 1 branch
BranchTrueFalseTotal
if $not_checked 0 0 0
134 0 my ($markup, $message) = @_;
135 0 ▶ if ($v->validate_markup($markup)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->validate_markup($markup)) { } 0 0 0
136 0 _result($v, $message);
137 } else {
138 0 _validator_err($v, "markup");
139 }
140 }
141  
142 =item is_valid_file($path[, $name]);
143  
144 B<Description:> is_valid_file works the same way as is_valid_markup, except that
145 you can specify the text to validate with the path to a filename. This is useful
146 if you have pregenerated all your HTML files locally, and now wish to test them.
147  
148 B<Parameters:> $path, a scalar, $name, an optional descriptive test name.
149  
150 B<Returns:> None.
151  
152 =cut
153  
154 0 ▶sub is_valid_file {
Subroutine is_valid_file: not called CC=3 SCAR=24.8
Pod documented
155 0 my ($file, $message) = @_;
156 0 ▶ _check_plan() if $not_checked;
Branch: 1 branch
BranchTrueFalseTotal
if $not_checked 0 0 0
157 0 ▶ if ($v->validate_file($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->validate_file($file)) { } 0 0 0
158 0 _result($v, $message);
159 } else {
160 0 _validator_err($v, "file");
161 }
162 }
163  
164  
165 =item is_valid($url[, $name]);
166  
167 B<Description:> is_valid, again, works very similarly to the is_valid_file and
168 is_valid_file, except you specify a document that is already online with its
169 URL. This can be useful if you wish to periodically test a website or webpage
170 that dynamically changes over time for example, like a blog or a wiki, without
171 first saving the html to a file using your browswer, or a utility such as wget.
172  
173 B<Parameters:> $url, a scalar, $name, an optional descriptive test name.
174  
175 B<Returns:> None.
176  
177 =cut
178  
179 0 ▶sub is_valid {
Subroutine is_valid: not called CC=3 SCAR=24.8
Pod documented
180 0 my ($uri, $message) = @_;
181 0 ▶ _check_plan() if $not_checked;
Branch: 1 branch
BranchTrueFalseTotal
if $not_checked 0 0 0
182 0 ▶ if ($v->validate($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->validate($uri)) { } 0 0 0
183 0 _result($v, $message);
184 } else {
185 0 _validator_err($v, "URI");
186 }
187 }
188  
189 0 ▶sub _validator_err {
Subroutine _validator_err: not called CC=1 SCAR=6.9
190 0 my ($validator, $type) = @_;
191 0 __PACKAGE__->builder->ok(0, "Failed to validate $type.");
192 0 __PACKAGE__->builder->diag($v->validator_error());
193 }
194  
195 0 ▶sub _result {
Subroutine _result: not called CC=3 SCAR=24.8
196 0 my ($validator, $message) = @_;
197 0 ▶ if ($validator->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->is_valid) { } 0 0 0
198 0 __PACKAGE__->builder->ok(1, $message);
199 } else {
200 0 my $num = $validator->num_errors();
201 0 ▶ my $plurality = ($num == 1) ? "error" : "errors";
Branch: 1 branch
BranchTrueFalseTotal
$num == 1 ? : 0 0 0
202 0 __PACKAGE__->builder->ok(0, $message . " ($num $plurality).");
203 }
204 }
205  
206  
207 =item diag_html($url);
208  
209 B<Description:> If you want to display the actual errors reported by
210 the service for a particular test, you can use the diag_html function.
211 Please note that you must have imported 'show_detail' for this to
212 work properly.
213  
214 use Test::HTML::W3C 'show_detail';
215  
216 is_valid_markup("<html></html">, "My simple test") or diag_html();
217  
218 B<Parameters:> $url, a scalar.
219  
220 B<Returns:> None.
221  
222 =cut
223  
224 0 ▶sub diag_html {
Subroutine diag_html: not called CC=3 SCAR=24.8
Pod documented
225 0 my $tb = __PACKAGE__->builder();
226 0 ▶ if ($show_detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_detail) { } 0 0 0
227 0 my @errs = $v->errors();
228 0 my $e;
229 0 foreach my $error ( @{$v->errors()} ) {
230 0 $e .= sprintf("%s at line %d\n", $error->msg, $error->line);
231 }
232 0 $tb->diag($e);
233 } else {
234 0 $tb->diag("You need to import 'show_detail' in order to call diag_html\n");
235 }
236 }
237  
238  
239 1 1;
240  
241 __END__
↑ Summary