blib/lib/Test/HTML/T5.pm

filter: Statementstmt 100% 71 / 71 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 98.0% 98 / 100 CC 11 SCAR 12.7
CC
11
Cov
98%
_parse_and_complain
7.0
html_fragment_tidy_ok
3.0
html_tidy_ok
3.0
CRAP 3.6
« blib/lib/HTML/T5.pm ↑ Summary blib/lib/HTML/T5/Message.pm »
1 package Test::HTML::T5;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use HTML::T5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our @EXPORT_OK = qw(
15 html_tidy_ok
16 html_fragment_tidy_ok
17 );
18  
19 3 our @EXPORT = @EXPORT_OK;
20  
21 =head1 NAME
22  
23 Test::HTML::T5 - Test::More-style wrapper around HTML::T5
24  
25 =head1 VERSION
26  
27 Version 0.014
28  
29 =cut
30  
31 3 our $VERSION = '0.014';
32  
33 3 my $TB = Test::Builder->new;
34  
35 =head1 SYNOPSIS
36  
37 use Test::HTML::T5 tests => 4;
38  
39 my $table = build_display_table();
40 html_tidy_ok( $table, 'Built display table properly' );
41  
42 =head1 DESCRIPTION
43  
44 This module provides a few convenience methods for testing exception
45 based code. It is built with L<Test::Builder> and plays happily with
46 L<Test::More> and friends.
47  
48 If you are not already familiar with L<Test::More> now would be the time
49 to go take a look.
50  
51 =head1 EXPORT
52  
53 C<html_tidy_ok>
54  
55 =cut
56  
57 sub import
58 3 ▶{
Subroutine import: called CC=1 SCAR=0.0
59 3 my $self = shift;
60 3 my $pack = caller;
61  
62 3 $TB->exported_to($pack);
63 3 $TB->plan(@_);
64  
65 3 $self->export_to_level( 1, $self, @EXPORT );
66  
67 3 return;
68 }
69  
70 =head2 html_tidy_ok( [$tidy, ] $html, $name )
71  
72 Checks to see if C<$html> is a valid HTML document.
73  
74 If you pass an HTML::T5 object, C<html_tidy_ok()> will use that for its
75 settings.
76  
77 my $tidy = HTML::T5->new( {config_file => 'path/to/config'} );
78 $tidy->ignore( type => TIDY_WARNING, type => TIDY_INFO );
79 html_tidy_ok( $tidy, $content, "Web page is OK, ignoring warnings and info' );
80  
81 Otherwise, it will use the default rules.
82  
83 html_tidy_ok( $content, "Web page passes ALL tests" );
84  
85 =cut
86  
87 sub html_tidy_ok
88 10 ▶{
Subroutine html_tidy_ok: called CC=3 SCAR=11.0
Pod documented
89 10 ▶ my $tidy = ( ref( $_[0] ) eq 'HTML::T5' ) ? shift : HTML::T5->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTML::T5' ? : 4 6 10
90 10 my $html = shift;
91 10 my $name = shift;
92  
93 10 my $ok = defined $html;
94 10 ▶ if ( !$ok )
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) { } 1 9 10
95 {
96 1 $TB->ok( 0, $name );
97 1 $TB->diag('Error: html_tidy_ok() got undef');
98 }
99 else
100 {
101 9 $ok = _parse_and_complain( $tidy, $html, $name, 0 );
102 }
103  
104 10 return $ok;
105 }
106  
107 =head2 html_fragment_tidy_ok( [$tidy, ] $html, $name )
108  
109 Works the same as C<html_tidy_ok>, but first wraps it up an HTML document.
110 This is useful for when want to validate self-contained snippets of HTML,
111 such as from templates or an HTML feed from a third party, and check
112 that it is valid.
113  
114 =cut
115  
116 sub html_fragment_tidy_ok
117 3 ▶{
Subroutine html_fragment_tidy_ok: called CC=3 SCAR=11.0
Pod documented
118 3 ▶ my $tidy = ( ref( $_[0] ) eq 'HTML::T5' ) ? shift : HTML::T5->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTML::T5' ? : 0 3 3
119 3 my $html = shift;
120 3 my $name = shift;
121  
122 3 my $ok = defined $html;
123 3 ▶ if ( !$ok )
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) { } 1 2 3
124 {
125 1 $TB->ok( 0, $name );
126 1 $TB->diag('Error: html_fragment_tidy_ok() got undef');
127 }
128 else
129 {
130 2 $html = <<"HTML";
131 <!DOCTYPE html>
132 <html>
133 <head>
134 <title> </title>
135 </head>
136 <body>
137 $html
138 </body>
139 </html>
140 HTML
141  
142 2 $ok = _parse_and_complain( $tidy, $html, $name, 6 );
143 }
144  
145 3 return $ok;
146 }
147  
148 sub _parse_and_complain
149 11 ▶{
Subroutine _parse_and_complain: called CC=7 SCAR=19.5
150 11 local $Test::Builder::Level = $Test::Builder::Level + 1;
151  
152 11 my $tidy = shift;
153 11 my $html = shift;
154 11 my $name = shift;
155 11 my $offset = shift;
156  
157 11 $tidy->clear_messages();
158 11 $tidy->parse( undef, $html );
159  
160 11 my @messages = $tidy->messages;
161 11 my $nmessages = @messages;
162  
163 11 my $ok = !$nmessages;
164 11 $TB->ok( $ok, $name );
165 11 ▶ if ( !$ok )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 10 1 11
166 {
167 10 ▶ if ($offset)
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 2 8 10
168 {
169 2 $_->{_line} -= $offset for @messages;
170 }
171 10 my $msg = 'Errors:';
172 10 ▶ $msg .= " $name" if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 10 0 10
173 10 $TB->diag($msg);
174 10 $TB->diag( $_->as_string ) for @messages;
175 10 ▶ my $s = $nmessages == 1 ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
$nmessages == 1 ? : 4 6 10
176 10 $TB->diag("$nmessages message$s on the page");
177 }
178  
179 11 return $ok;
180 }
181  
182 =head1 BUGS
183  
184 All bugs and requests are now being handled through GitHub.
185  
186 http://github.com/shlomif/html-tidy5/issues
187  
188 DO NOT send bug reports to http://rt.cpan.org/.
189  
190 =head1 COPYRIGHT & LICENSE
191  
192 Copyright 2005-2018 Andy Lester.
193  
194 This program is free software; you can redistribute it and/or modify
195 it under the terms of the Artistic License v2.0.
196  
197 http://www.opensource.org/licenses/Artistic-2.0
198  
199 Please note that these modules are not products of or supported by the
200 employers of the various contributors to the code.
201  
202 =head1 AUTHOR
203  
204 Andy Lester, C<andy@petdance.com>
205  
206 =cut
207  
208 3 1;
« blib/lib/HTML/T5.pm ↑ Summary blib/lib/HTML/T5/Message.pm »