| 1 |
#!perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use English qw( -no_match_vars ); |
| 22 |
use Fatal qw(open close); |
| 23 |
use File::Spec; |
| 24 |
use File::Temp; |
| 25 |
use Test::More; |
| 26 |
|
| 27 |
BEGIN { |
| 28 |
use lib 'html/tool/lib'; |
| 29 |
my $eval_result = eval { require Marpa::R2::HTML::Test::Util; 1 }; |
| 30 |
if ( !$eval_result ) { |
| 31 |
Test::More::plan tests => 1; |
| 32 |
Test::More::fail( |
| 33 |
"Could not load Marpa::R2::HTML::Test::Util; $EVAL_ERROR"); |
| 34 |
exit 0; |
| 35 |
} ## end if ( !$eval_result ) |
| 36 |
} ## end BEGIN |
| 37 |
|
| 38 |
BEGIN { Marpa::R2::HTML::Test::Util::load_or_skip_all('HTML::Parser'); } |
| 39 |
|
| 40 |
BEGIN { Test::More::plan tests => 10; } |
| 41 |
|
| 42 |
use lib 'tool/lib'; |
| 43 |
use Marpa::R2::Test; |
| 44 |
|
| 45 |
my $blib = $ENV{MARPA_TEST_BLIB} // 'blib'; |
| 46 |
my $script_dir = File::Spec->catdir( $blib, 'script' ); |
| 47 |
|
| 48 |
sub run_one_test { |
| 49 |
my ( $test_name, $html, $config_ref, $expected_ref ) = @_; |
| 50 |
my ( $html_fh, $html_file_name ) = File::Temp::tempfile( |
| 51 |
'temp_for_test_XXXXXX', |
| 52 |
UNLINK => 1, |
| 53 |
SUFFIX => '.html' |
| 54 |
); |
| 55 |
print {$html_fh} $html; |
| 56 |
close $html_fh; |
| 57 |
my ( $cfg_fh, $test_config_name ) = File::Temp::tempfile( |
| 58 |
'temp_for_test_XXXXXX', |
| 59 |
UNLINK => 1, |
| 60 |
SUFFIX => '.txt' |
| 61 |
); |
| 62 |
print {$cfg_fh} ${$config_ref}; |
| 63 |
close $cfg_fh; |
| 64 |
my $output = Marpa::R2::HTML::Test::Util::run_command( |
| 65 |
File::Spec->catfile( $script_dir, 'marpa_r2_html_fmt' ), |
| 66 |
'--no-added-tag', |
| 67 |
'--compile=' . $test_config_name, |
| 68 |
$html_file_name |
| 69 |
); |
| 70 |
|
| 71 |
unlink $test_config_name, $html_file_name; |
| 72 |
Marpa::R2::Test::is( $output, ${$expected_ref}, $test_name ); |
| 73 |
} ## end sub run_one_test |
| 74 |
|
| 75 |
my $default_config = do { |
| 76 |
my @g_config_dir = qw( g config ); |
| 77 |
my $file_name = File::Spec->catfile( qw(g config default.txt) ); |
| 78 |
open my $fh, q{<}, $file_name; |
| 79 |
my $file = join q{}, <$fh>; |
| 80 |
close $fh; |
| 81 |
\$file; |
| 82 |
}; |
| 83 |
|
| 84 |
my $test_name; |
| 85 |
my $test_html; |
| 86 |
my $test_config; |
| 87 |
my $expected_output; |
| 88 |
|
| 89 |
$test_name = 'Inline element containing inline flow'; |
| 90 |
$test_config = |
| 91 |
${$default_config} . '<acme> is a *inline included in %inline'; |
| 92 |
$test_html = '<acme>-during-<span>-more inline stuff-<p>-new block-' . "\n"; |
| 93 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 94 |
<html> |
| 95 |
<head> |
| 96 |
</head> |
| 97 |
<body> |
| 98 |
<p> |
| 99 |
<acme> |
| 100 |
-during-<span> |
| 101 |
-more inline stuff-</span></acme></p><p> |
| 102 |
-new block- |
| 103 |
</p></body> |
| 104 |
</html> |
| 105 |
END_OF_EXPECTED_OUTPUT |
| 106 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 107 |
|
| 108 |
$test_name = 'Inline element containing block flow'; |
| 109 |
$test_config = |
| 110 |
${$default_config} . '<acme> is a *inline included in %block'; |
| 111 |
# $test_html is same as in previous test |
| 112 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 113 |
<html> |
| 114 |
<head> |
| 115 |
</head> |
| 116 |
<body> |
| 117 |
<acme> |
| 118 |
-during-<span> |
| 119 |
-more inline stuff-</span></acme><p> |
| 120 |
-new block- |
| 121 |
</p></body> |
| 122 |
</html> |
| 123 |
END_OF_EXPECTED_OUTPUT |
| 124 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 125 |
|
| 126 |
$test_name = 'Block element containing mixed flow'; |
| 127 |
$test_config = |
| 128 |
${$default_config} . '<acme> is a *mixed included in %block'; |
| 129 |
# $test_html is same as in previous test |
| 130 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 131 |
<html> |
| 132 |
<head> |
| 133 |
</head> |
| 134 |
<body> |
| 135 |
<acme> |
| 136 |
-during-<span> |
| 137 |
-more inline stuff-</span><p> |
| 138 |
-new block- |
| 139 |
</p></acme></body> |
| 140 |
</html> |
| 141 |
END_OF_EXPECTED_OUTPUT |
| 142 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 143 |
|
| 144 |
$test_name = 'Block element containing block flow'; |
| 145 |
$test_config = |
| 146 |
${$default_config} . '<acme> is a *block included in %block'; |
| 147 |
# $test_html is same as in previous test |
| 148 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 149 |
<html> |
| 150 |
<head> |
| 151 |
</head> |
| 152 |
<body> |
| 153 |
<acme> |
| 154 |
<p> |
| 155 |
-during-<span> |
| 156 |
-more inline stuff-</span></p><p> |
| 157 |
-new block- |
| 158 |
</p></acme></body> |
| 159 |
</html> |
| 160 |
END_OF_EXPECTED_OUTPUT |
| 161 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 162 |
|
| 163 |
$test_name = 'Block element containing PCDATA'; |
| 164 |
$test_config = |
| 165 |
${$default_config} . '<acme> is a *pcdata included in %block'; |
| 166 |
# $test_html is same as in previous test |
| 167 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 168 |
<html> |
| 169 |
<head> |
| 170 |
</head> |
| 171 |
<body> |
| 172 |
<acme> |
| 173 |
-during-</acme><p> |
| 174 |
<span> |
| 175 |
-more inline stuff-</span></p><p> |
| 176 |
-new block- |
| 177 |
</p></body> |
| 178 |
</html> |
| 179 |
END_OF_EXPECTED_OUTPUT |
| 180 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 181 |
|
| 182 |
$test_name = 'Empty block element'; |
| 183 |
$test_config = |
| 184 |
${$default_config} . '<acme> is a *empty included in %block'; |
| 185 |
# $test_html is same as in previous test |
| 186 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 187 |
<html> |
| 188 |
<head> |
| 189 |
</head> |
| 190 |
<body> |
| 191 |
<acme><p> |
| 192 |
-during-<span> |
| 193 |
-more inline stuff-</span></p><p> |
| 194 |
-new block- |
| 195 |
</p></body> |
| 196 |
</html> |
| 197 |
END_OF_EXPECTED_OUTPUT |
| 198 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 199 |
|
| 200 |
$test_name = 'Body allows mixed flow'; |
| 201 |
$test_config = ${$default_config}; |
| 202 |
$test_config =~ s/^ \s* <body> [^\n]* $/<body> is *mixed/xms; |
| 203 |
$test_html = 'I cannot wait for a start tag<p>I can'; |
| 204 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 205 |
<html> |
| 206 |
<head> |
| 207 |
</head> |
| 208 |
<body> |
| 209 |
I cannot wait for a start tag<p> |
| 210 |
I can</p></body> |
| 211 |
</html> |
| 212 |
END_OF_EXPECTED_OUTPUT |
| 213 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 214 |
|
| 215 |
$test_name = 'Body allows block flow'; |
| 216 |
# This is the default |
| 217 |
$test_config = ${$default_config}; |
| 218 |
$test_config =~ s/^ \s* <body> [^\n]* $/<body> is *block/xms; |
| 219 |
# $test_html is same as in previous test |
| 220 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 221 |
<html> |
| 222 |
<head> |
| 223 |
</head> |
| 224 |
<body> |
| 225 |
<p> |
| 226 |
I cannot wait for a start tag</p><p> |
| 227 |
I can</p></body> |
| 228 |
</html> |
| 229 |
END_OF_EXPECTED_OUTPUT |
| 230 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 231 |
|
| 232 |
$test_name = 'Body is inline flow'; |
| 233 |
$test_config = ${$default_config}; |
| 234 |
$test_config =~ s/^ \s* <body> [^\n]* $/<body> is *inline/xms; |
| 235 |
# $test_html is same as in previous test |
| 236 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 237 |
<html> |
| 238 |
<head> |
| 239 |
</head> |
| 240 |
<body> |
| 241 |
I cannot wait for a start tag<!-- |
| 242 |
html_fmt: Next start tag is cruft |
| 243 |
--><p>I can</body> |
| 244 |
</html> |
| 245 |
END_OF_EXPECTED_OUTPUT |
| 246 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 247 |
|
| 248 |
$test_name = 'Body is empty'; |
| 249 |
$test_config = ${$default_config}; |
| 250 |
$test_config =~ s/^ \s* <body> [^\n]* $/<body> is *empty/xms; |
| 251 |
# I also need to eliminate all mentions of the </body> tag |
| 252 |
# now that <body> is an empty element |
| 253 |
$test_config =~ s{^ \s* [<][/]body[>] \s [^\n]* $}{}xms; |
| 254 |
$test_config =~ s{ [<][/]body[>] }{}gxms; |
| 255 |
# $test_html is same as in previous test |
| 256 |
$expected_output = <<'END_OF_EXPECTED_OUTPUT'; |
| 257 |
<html> |
| 258 |
<head> |
| 259 |
</head><body><!-- |
| 260 |
html_fmt: Next text is cruft |
| 261 |
-->I cannot wait for a start tag<!-- |
| 262 |
html_fmt: Next start tag is cruft |
| 263 |
--><p><!-- |
| 264 |
html_fmt: Next text is cruft |
| 265 |
-->I can</html> |
| 266 |
END_OF_EXPECTED_OUTPUT |
| 267 |
run_one_test( $test_name, $test_html, \$test_config, \$expected_output ); |
| 268 |
|
| 269 |
# vim: expandtab shiftwidth=4: |