1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#!perl
use 5.010001;
use warnings;
use strict;
use Test::More tests => 5;
use Test::Builder::Tester;
use Test::HTML::Tidy5;
subtest 'html_tidy_ok fails on undef' => sub {
plan tests => 1;
my $msg = 'Fails on undef';
test_out( "not ok 1 - $msg" );
test_fail( +2 );
test_diag( 'Error: html_tidy_ok() got undef' );
html_tidy_ok( undef, $msg );
test_test( $msg );
};
subtest 'html_tidy_ok without errors' => sub {
plan tests => 1;
my $html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<p>
This is a full document.
<img src="alpha.jpg" height="21" width="12" alt="alpha">
<input type="image">
</p>
</body>
</html>
HTML
test_out( 'not ok 1 - Called html_tidy_ok on full document' );
test_fail( +4 );
test_diag( "Errors: Called html_tidy_ok on full document" );
test_diag( '(4:9) Warning: blank \'title\' element' );
test_diag( '1 message on the page' );
html_tidy_ok( $html, 'Called html_tidy_ok on full document' );
test_test( 'html_tidy_ok on full document works' );
# > # Failed test 'Called html_tidy_ok on full document'
# > # at t/html_tidy_ok.t line 45.
# > # Errors: Called html_tidy_ok on full document
# > # (4:9) Warning: blank 'title' element
# > # 1 message on the page
};
subtest 'html_tidy_ok with failures' => sub {
plan tests => 1;
my $html = <<'HTML';
<p>
This is an incomplete document, and it has some errors besides that as well.
<img src="alpha.jpg" height="21" width="12">
<input type="image">
</p>
<p>
HTML
test_out( 'not ok 1 - Called html_tidy_ok on incomplete document' );
test_fail( +8 );
test_diag( 'Errors: Called html_tidy_ok on incomplete document' );
test_diag( '(1:1) Warning: missing <!DOCTYPE> declaration' );
test_diag( '(1:1) Warning: inserting implicit <body>' );
test_diag( '(1:1) Warning: inserting missing \'title\' element' );
test_diag( '(3:5) Warning: <img> lacks "alt" attribute' );
test_diag( '(6:1) Warning: trimming empty <p>' );
test_diag( '5 messages on the page' );
html_tidy_ok( $html, 'Called html_tidy_ok on incomplete document' );
test_test( 'html_tidy_ok works on incomplete document' );
};
subtest 'Test passing our own Tidy object' => sub {
plan tests => 3;
my $html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<p>
This is a complete document, with an empty paragraph.
<img src="alpha.jpg" height="21" width="12" alt="alpha">
<input type="image">
</p>
<p>
</body>
</html>
HTML
# Default html_tidy_ok() complains about empty paragraph.
test_out( 'not ok 1 - Empty paragraph' );
test_fail( +5 );
test_diag( 'Errors: Empty paragraph' );
test_diag( '(4:9) Warning: blank \'title\' element' );
test_diag( '(12:9) Warning: trimming empty <p>' );
test_diag( '2 messages on the page' );
html_tidy_ok( $html, 'Empty paragraph' );
test_test( 'html_tidy_ok works on empty paragraph' );
# Now make our own more relaxed Tidy object and it should pass.
my $tidy = HTML::Tidy5->new( { drop_empty_elements => 0 } );
isa_ok( $tidy, 'HTML::Tidy5' );
test_out( 'not ok 1 - Relaxed tidy' );
test_fail( +4 );
test_diag( 'Errors: Relaxed tidy' );
test_diag( '(4:9) Warning: blank \'title\' element' );
test_diag( '1 message on the page' );
html_tidy_ok( $tidy, $html, 'Relaxed tidy' );
test_test( 'html_tidy_ok with user-specified tidy works' );
};
subtest 'Reusing a tidy object' => sub {
plan tests => 7;
my $tidy = HTML::Tidy5->new();
isa_ok( $tidy, 'HTML::Tidy5' );
my $very_bad_html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
<title> One error </title>
</head>
<body>
<p>
This is just bad.
<img bingo="Bango">
<table>
</body>
</html>
HTML
# Very bad HTML.
test_out( 'not ok 1 - Very bad HTML' );
test_fail( +7 );
test_diag( 'Errors: Very bad HTML' );
test_diag( '(10:9) Warning: missing </table> before </body>' );
test_diag( '(9:13) Warning: <img> lacks "alt" attribute' );
test_diag( '(9:13) Warning: <img> lacks "src" attribute' );
test_diag( '(10:9) Warning: trimming empty <table>' );
test_diag( '4 messages on the page' );
html_tidy_ok( $tidy, $very_bad_html, 'Very bad HTML' );
test_test( 'html_tidy_ok works on very bad HTML' );
is( scalar $tidy->messages, 4, 'We have four messages' );
# Make sure the next use of html_tidy_ok() and the tidy doesn't have leftover messages.
my $kinda_bad_html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
<title> One error </title>
</head>
<body>
<p>
This is a complete document with an extra unclosed paragraph.
<img src="alpha.jpg" height="21" width="12" alt="alpha">
<input type="image">
</p>
<p>
</body>
</html>
HTML
# Kinda bad HTML only has one error.
test_out( 'not ok 1 - Empty paragraph' );
test_fail( +4 );
test_diag( 'Errors: Empty paragraph' );
test_diag( '(12:9) Warning: trimming empty <p>' );
test_diag( '1 message on the page' );
html_tidy_ok( $tidy, $kinda_bad_html, 'Empty paragraph' );
test_test( 'html_tidy_ok works on empty paragraph' );
is( scalar $tidy->messages, 1, 'We have one message' );
my $good_html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
<title> All good </title>
</head>
<body>
<p>
Good HTML
</p>
</body>
</html>
HTML
# The good HTML should have no warnings at all.
test_out( 'ok 1 - Good HTML' );
html_tidy_ok( $tidy, $good_html, 'Good HTML' );
test_test( 'Reusing tidy object with good HTML works' );
is( scalar $tidy->messages, 0, 'We have no messages' );
};
done_testing();
exit 0;
|