| 1 | package Test::HTML::Lint; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use Test::Builder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 7 | ▶ | use HTML::Lint; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use vars qw( @ISA $VERSION @EXPORT ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 7 | @ISA = qw( HTML::Parser Exporter ); | |||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | Test::HTML::Lint - Test::More-style wrapper around HTML::Lint | ||||||||||
| 18 | |||||||||||
| 19 | =head1 VERSION | ||||||||||
| 20 | |||||||||||
| 21 | Version 2.27_03 | ||||||||||
| 22 | |||||||||||
| 23 | =cut | ||||||||||
| 24 | |||||||||||
| 25 | 7 | $VERSION = '2.27_03'; | |||||||||
| 26 | |||||||||||
| 27 | 7 | my $Tester = Test::Builder->new; | |||||||||
| 28 | |||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||
| 30 | |||||||||||
| 31 | use Test::HTML::Lint tests => 4; | ||||||||||
| 32 | |||||||||||
| 33 | my $table = build_display_table(); | ||||||||||
| 34 | html_ok( $table, 'Built display table properly' ); | ||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | This module provides a few convenience methods for testing exception | ||||||||||
| 39 | based code. It is built with L<Test::Builder> and plays happily with | ||||||||||
| 40 | L<Test::More> and friends. | ||||||||||
| 41 | |||||||||||
| 42 | If you are not already familiar with L<Test::More> now would be the time | ||||||||||
| 43 | to go take a look. | ||||||||||
| 44 | |||||||||||
| 45 | =head1 EXPORT | ||||||||||
| 46 | |||||||||||
| 47 | C<html_ok> | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | 7 | @EXPORT = qw( | |||||||||
| 52 | html_ok | ||||||||||
| 53 | html_fragment_ok | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | sub import { | ||||||||||
| 57 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 6 | my $pack = caller; | |||||||||
| 59 | |||||||||||
| 60 | 6 | $Tester->exported_to($pack); | |||||||||
| 61 | 6 | $Tester->plan(@_); | |||||||||
| 62 | |||||||||||
| 63 | 6 | $self->export_to_level(1, $self, @EXPORT); | |||||||||
| 64 | |||||||||||
| 65 | 6 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | =head2 html_ok( [$lint, ] $html, $name ) | ||||||||||
| 69 | |||||||||||
| 70 | Checks to see if C<$html> is a valid HTML document, including checks | ||||||||||
| 71 | for having C<< <html> >>, C<< <head> >>, C<< <title>> >> and | ||||||||||
| 72 | C<< <body> >> tags. | ||||||||||
| 73 | |||||||||||
| 74 | If you're checking something that is only a fragment of an HTML document, | ||||||||||
| 75 | use C<html_fragment_ok()>. | ||||||||||
| 76 | |||||||||||
| 77 | If you pass an HTML::Lint object, C<html_ok()> will use that for its | ||||||||||
| 78 | settings. | ||||||||||
| 79 | |||||||||||
| 80 | my $lint = new HTML::Lint( only_types => STRUCTURE ); | ||||||||||
| 81 | html_ok( $lint, $content, "Web page passes structural tests only" ); | ||||||||||
| 82 | |||||||||||
| 83 | Otherwise, it will use the default rules. | ||||||||||
| 84 | |||||||||||
| 85 | html_ok( $content, "Web page passes ALL tests" ); | ||||||||||
| 86 | |||||||||||
| 87 | Note that if you pass in your own HTML::Lint object, C<html_ok()> | ||||||||||
| 88 | will clear its errors before using it. | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | sub html_ok { | ||||||||||
| 93 | 6 | ▶ | my $lint; | ||||||||
|
Subroutine
html_ok: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 94 | |||||||||||
| 95 | 6 | ▶ | if ( ref($_[0]) eq 'HTML::Lint' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 1 | $lint = shift; | |||||||||
| 97 | 1 | $lint->newfile(); | |||||||||
| 98 | 1 | $lint->clear_errors(); | |||||||||
| 99 | } | ||||||||||
| 100 | else { | ||||||||||
| 101 | 5 | $lint = HTML::Lint->new; | |||||||||
| 102 | } | ||||||||||
| 103 | 6 | my $html = shift; | |||||||||
| 104 | 6 | my $name = shift; | |||||||||
| 105 | |||||||||||
| 106 | 6 | my $ok = defined $html; | |||||||||
| 107 | 6 | ▶ | if ( !$ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 1 | $Tester->ok( 0, $name ); | |||||||||
| 109 | } | ||||||||||
| 110 | else { | ||||||||||
| 111 | 5 | $lint->parse( $html ); | |||||||||
| 112 | 5 | $lint->eof(); | |||||||||
| 113 | 5 | my $nerr = scalar $lint->errors; | |||||||||
| 114 | 5 | $ok = !$nerr; | |||||||||
| 115 | 5 | $Tester->ok( $ok, $name ); | |||||||||
| 116 | 5 | ▶ | if ( !$ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 2 | my $msg = 'Errors:'; | |||||||||
| 118 | 2 | ▶ | $msg .= " $name" if $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 2 | $Tester->diag( $msg ); | |||||||||
| 120 | 2 | $Tester->diag( $_->as_string ) for $lint->errors; | |||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 6 | return $ok; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | =head2 html_fragment_ok( [$lint, ] $html, $name ) | ||||||||||
| 129 | |||||||||||
| 130 | Checks that C<$fragment> is valid HTML, but not necessarily a valid | ||||||||||
| 131 | HTML document. | ||||||||||
| 132 | |||||||||||
| 133 | For example, this is a valid fragment, but not a valid HTML document: | ||||||||||
| 134 | |||||||||||
| 135 | <body> | ||||||||||
| 136 | <p>Lorem ipsum</p> | ||||||||||
| 137 | </body> | ||||||||||
| 138 | |||||||||||
| 139 | because it doesn't contain C<< <html> >> and C<< <head> >> tags. If you | ||||||||||
| 140 | want to check that it is a valid document, use C<html_ok()>. | ||||||||||
| 141 | |||||||||||
| 142 | If you pass an HTML::Lint object, C<html_fragment_ok()> will use that for its | ||||||||||
| 143 | settings. | ||||||||||
| 144 | |||||||||||
| 145 | my $lint = new HTML::Lint( only_types => STRUCTURE ); | ||||||||||
| 146 | html_fragment_ok( $lint, $content, 'Web page passes structural tests only' ); | ||||||||||
| 147 | |||||||||||
| 148 | Otherwise, it will use the default rules. | ||||||||||
| 149 | |||||||||||
| 150 | html_fragment_ok( $content, 'Fragment passes ALL tests' ); | ||||||||||
| 151 | |||||||||||
| 152 | Note that if you pass in your own HTML::Lint object, C<html_fragment_ok()> | ||||||||||
| 153 | will clear its errors before using it. | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | sub html_fragment_ok { | ||||||||||
| 158 | 2 | ▶ | my $lint; | ||||||||
|
Subroutine
html_fragment_ok: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||
| 159 | |||||||||||
| 160 | 2 | ▶ | if ( ref($_[0]) eq 'HTML::Lint' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | $lint = shift; | |||||||||
| 162 | 0 | $lint->newfile(); | |||||||||
| 163 | 0 | $lint->clear_errors(); | |||||||||
| 164 | } | ||||||||||
| 165 | else { | ||||||||||
| 166 | 2 | $lint = HTML::Lint->new; | |||||||||
| 167 | } | ||||||||||
| 168 | 2 | my $html = shift; | |||||||||
| 169 | 2 | my $name = shift; | |||||||||
| 170 | |||||||||||
| 171 | 2 | my $ok = defined $html; | |||||||||
| 172 | 2 | ▶ | if ( !$ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | $Tester->ok( 0, $name ); | |||||||||
| 174 | } | ||||||||||
| 175 | else { | ||||||||||
| 176 | 2 | $lint->parse( $html ); | |||||||||
| 177 | 2 | $lint->eof(); | |||||||||
| 178 | |||||||||||
| 179 | # Ignore doc-level errors. | ||||||||||
| 180 | 2 | my @errors = grep { $_->errcode ne 'doc-tag-required' } $lint->errors; | |||||||||
| 181 | |||||||||||
| 182 | 2 | my $nerr = @errors; | |||||||||
| 183 | 2 | $ok = !$nerr; | |||||||||
| 184 | 2 | $Tester->ok( $ok, $name ); | |||||||||
| 185 | 2 | ▶ | if ( !$ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 1 | my $msg = 'Errors:'; | |||||||||
| 187 | 1 | ▶ | $msg .= " $name" if $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 1 | $Tester->diag( $msg ); | |||||||||
| 189 | 1 | $Tester->diag( $_->as_string ) for @errors; | |||||||||
| 190 | } | ||||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 2 | return $ok; | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | |||||||||||
| 197 | =head1 BUGS | ||||||||||
| 198 | |||||||||||
| 199 | All bugs and requests are now being handled through GitHub. | ||||||||||
| 200 | |||||||||||
| 201 | https://github.com/petdance/html-lint/issues | ||||||||||
| 202 | |||||||||||
| 203 | DO NOT send bug reports to http://rt.cpan.org/. | ||||||||||
| 204 | |||||||||||
| 205 | |||||||||||
| 206 | =head1 TO DO | ||||||||||
| 207 | |||||||||||
| 208 | There needs to be a C<html_table_ok()> to check that the HTML is a | ||||||||||
| 209 | self-contained, well-formed table, and then a comparable one for | ||||||||||
| 210 | C<html_page_ok()>. | ||||||||||
| 211 | |||||||||||
| 212 | If you think this module should do something that it doesn't do at the | ||||||||||
| 213 | moment please let me know. | ||||||||||
| 214 | |||||||||||
| 215 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 216 | |||||||||||
| 217 | Thanks to chromatic and Michael G Schwern for the excellent Test::Builder, | ||||||||||
| 218 | without which this module wouldn't be possible. | ||||||||||
| 219 | |||||||||||
| 220 | Thanks to Adrian Howard for writing Test::Exception, from which most of | ||||||||||
| 221 | this module is taken. | ||||||||||
| 222 | |||||||||||
| 223 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 224 | |||||||||||
| 225 | Copyright 2005-2017 Andy Lester. | ||||||||||
| 226 | |||||||||||
| 227 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 228 | it under the terms of the Artistic License v2.0. | ||||||||||
| 229 | |||||||||||
| 230 | http://www.opensource.org/licenses/Artistic-2.0 | ||||||||||
| 231 | |||||||||||
| 232 | Please note that these modules are not products of or supported by the | ||||||||||
| 233 | employers of the various contributors to the code. | ||||||||||
| 234 | |||||||||||
| 235 | =head1 AUTHOR | ||||||||||
| 236 | |||||||||||
| 237 | Andy Lester, C<andy@petdance.com> | ||||||||||
| 238 | |||||||||||
| 239 | =cut | ||||||||||
| 240 | |||||||||||
| 241 | 7 | 1; | |||||||||