1 |
wakaba |
1.1 |
#!/usr/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
|
4 |
wakaba |
1.23 |
my $DEBUG = $ENV{DEBUG}; |
5 |
wakaba |
1.33 |
|
6 |
wakaba |
1.32 |
use lib qw[/home/wakaba/work/manakai2/lib]; |
7 |
wakaba |
1.33 |
|
8 |
wakaba |
1.1 |
my $dir_name; |
9 |
wakaba |
1.2 |
my $test_dir_name; |
10 |
wakaba |
1.1 |
BEGIN { |
11 |
wakaba |
1.2 |
$test_dir_name = 't/'; |
12 |
wakaba |
1.1 |
$dir_name = 't/tree-construction/'; |
13 |
|
|
my $skip = "You don't have make command"; |
14 |
|
|
eval q{ |
15 |
|
|
system ("cd $test_dir_name; make tree-construction-files") == 0 or die |
16 |
|
|
unless -f $dir_name.'tests1.dat'; |
17 |
|
|
$skip = ''; |
18 |
|
|
}; |
19 |
|
|
if ($skip) { |
20 |
|
|
print "1..1\n"; |
21 |
|
|
print "ok 1 # $skip\n"; |
22 |
|
|
exit; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
use Test; |
27 |
wakaba |
1.30 |
BEGIN { plan tests => 3105 } |
28 |
wakaba |
1.1 |
|
29 |
|
|
use Data::Dumper; |
30 |
|
|
$Data::Dumper::Useqq = 1; |
31 |
|
|
sub Data::Dumper::qquote { |
32 |
|
|
my $s = shift; |
33 |
|
|
$s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge; |
34 |
|
|
return q<qq'> . $s . q<'>; |
35 |
|
|
} # Data::Dumper::qquote |
36 |
|
|
|
37 |
wakaba |
1.23 |
|
38 |
|
|
if ($DEBUG) { |
39 |
wakaba |
1.25 |
my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}}; |
40 |
wakaba |
1.23 |
$Whatpm::HTML::Debug::cp_pass = sub { |
41 |
|
|
my $id = shift; |
42 |
|
|
delete $not_found->{$id}; |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
END { |
46 |
|
|
for my $id (sort {$a <=> $b || $a cmp $b} keys %$not_found) { |
47 |
|
|
print "# checkpoint $id is not reached\n"; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
|
52 |
wakaba |
1.2 |
for my $file_name (grep {$_} split /\s+/, qq[ |
53 |
wakaba |
1.12 |
${test_dir_name}tokenizer-test-2.dat |
54 |
wakaba |
1.31 |
${test_dir_name}tokenizer-test-3.dat |
55 |
wakaba |
1.2 |
${dir_name}tests1.dat |
56 |
|
|
${dir_name}tests2.dat |
57 |
|
|
${dir_name}tests3.dat |
58 |
|
|
${dir_name}tests4.dat |
59 |
wakaba |
1.11 |
${dir_name}tests5.dat |
60 |
|
|
${dir_name}tests6.dat |
61 |
wakaba |
1.27 |
${dir_name}tests7.dat |
62 |
wakaba |
1.2 |
${test_dir_name}tree-test-1.dat |
63 |
wakaba |
1.14 |
${test_dir_name}tree-test-2.dat |
64 |
wakaba |
1.29 |
${test_dir_name}tree-test-3.dat |
65 |
wakaba |
1.34 |
${test_dir_name}tree-test-void.dat |
66 |
wakaba |
1.1 |
]) { |
67 |
wakaba |
1.2 |
open my $file, '<', $file_name |
68 |
|
|
or die "$0: $file_name: $!"; |
69 |
wakaba |
1.13 |
print "# $file_name\n"; |
70 |
wakaba |
1.1 |
|
71 |
|
|
my $test; |
72 |
|
|
my $mode = 'data'; |
73 |
wakaba |
1.12 |
my $escaped; |
74 |
wakaba |
1.1 |
while (<$file>) { |
75 |
|
|
s/\x0D\x0A/\x0A/; |
76 |
|
|
if (/^#data$/) { |
77 |
|
|
undef $test; |
78 |
|
|
$test->{data} = ''; |
79 |
|
|
$mode = 'data'; |
80 |
wakaba |
1.12 |
undef $escaped; |
81 |
|
|
} elsif (/^#data escaped$/) { |
82 |
|
|
undef $test; |
83 |
|
|
$test->{data} = ''; |
84 |
|
|
$mode = 'data'; |
85 |
|
|
$escaped = 1; |
86 |
wakaba |
1.1 |
} elsif (/^#errors$/) { |
87 |
|
|
$test->{errors} = []; |
88 |
|
|
$mode = 'errors'; |
89 |
wakaba |
1.13 |
$test->{data} =~ s/\x0D?\x0A\z//; |
90 |
|
|
$test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped; |
91 |
wakaba |
1.24 |
$test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped; |
92 |
wakaba |
1.12 |
undef $escaped; |
93 |
wakaba |
1.30 |
} elsif (/^#shoulds$/) { |
94 |
|
|
$test->{shoulds} = []; |
95 |
|
|
$mode = 'shoulds'; |
96 |
wakaba |
1.1 |
} elsif (/^#document$/) { |
97 |
|
|
$test->{document} = ''; |
98 |
|
|
$mode = 'document'; |
99 |
wakaba |
1.12 |
undef $escaped; |
100 |
|
|
} elsif (/^#document escaped$/) { |
101 |
|
|
$test->{document} = ''; |
102 |
|
|
$mode = 'document'; |
103 |
|
|
$escaped = 1; |
104 |
wakaba |
1.19 |
} elsif (/^#document-fragment$/) { |
105 |
|
|
$test->{element} = ''; |
106 |
|
|
$mode = 'element'; |
107 |
|
|
undef $escaped; |
108 |
wakaba |
1.5 |
} elsif (/^#document-fragment (\S+)$/) { |
109 |
|
|
$test->{document} = ''; |
110 |
|
|
$mode = 'document'; |
111 |
|
|
$test->{element} = $1; |
112 |
wakaba |
1.12 |
undef $escaped; |
113 |
|
|
} elsif (/^#document-fragment (\S+) escaped$/) { |
114 |
|
|
$test->{document} = ''; |
115 |
|
|
$mode = 'document'; |
116 |
|
|
$test->{element} = $1; |
117 |
|
|
$escaped = 1; |
118 |
wakaba |
1.2 |
} elsif (defined $test->{document} and /^$/) { |
119 |
wakaba |
1.13 |
$test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped; |
120 |
wakaba |
1.24 |
$test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped; |
121 |
wakaba |
1.2 |
test ($test); |
122 |
wakaba |
1.1 |
undef $test; |
123 |
|
|
} else { |
124 |
|
|
if ($mode eq 'data' or $mode eq 'document') { |
125 |
wakaba |
1.13 |
$test->{$mode} .= $_; |
126 |
wakaba |
1.19 |
} elsif ($mode eq 'element') { |
127 |
|
|
tr/\x0D\x0A//d; |
128 |
|
|
$test->{$mode} .= $_; |
129 |
wakaba |
1.1 |
} elsif ($mode eq 'errors') { |
130 |
|
|
tr/\x0D\x0A//d; |
131 |
|
|
push @{$test->{errors}}, $_; |
132 |
wakaba |
1.30 |
} elsif ($mode eq 'shoulds') { |
133 |
|
|
tr/\x0D\x0A//d; |
134 |
|
|
push @{$test->{shoulds}}, $_; |
135 |
wakaba |
1.1 |
} |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
test ($test) if $test->{errors}; |
139 |
|
|
} |
140 |
|
|
|
141 |
wakaba |
1.4 |
use Whatpm::HTML; |
142 |
|
|
use Whatpm::NanoDOM; |
143 |
wakaba |
1.33 |
use Whatpm::Charset::UnicodeChecker; |
144 |
wakaba |
1.1 |
|
145 |
|
|
sub test ($) { |
146 |
|
|
my $test = shift; |
147 |
|
|
|
148 |
wakaba |
1.4 |
my $doc = Whatpm::NanoDOM::Document->new; |
149 |
wakaba |
1.1 |
my @errors; |
150 |
wakaba |
1.30 |
my @shoulds; |
151 |
wakaba |
1.1 |
|
152 |
|
|
$SIG{INT} = sub { |
153 |
wakaba |
1.3 |
print scalar serialize ($doc); |
154 |
wakaba |
1.1 |
exit; |
155 |
|
|
}; |
156 |
wakaba |
1.3 |
|
157 |
wakaba |
1.5 |
my $onerror = sub { |
158 |
|
|
my %opt = @_; |
159 |
wakaba |
1.30 |
if ($opt{level} eq 's') { |
160 |
|
|
push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type}; |
161 |
|
|
} else { |
162 |
|
|
push @errors, join ':', $opt{line}, $opt{column}, $opt{type}; |
163 |
|
|
} |
164 |
wakaba |
1.5 |
}; |
165 |
wakaba |
1.33 |
|
166 |
|
|
my $chk = sub { |
167 |
|
|
return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5'); |
168 |
|
|
}; # $chk |
169 |
|
|
|
170 |
wakaba |
1.5 |
my $result; |
171 |
|
|
unless (defined $test->{element}) { |
172 |
wakaba |
1.33 |
Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk); |
173 |
wakaba |
1.5 |
$result = serialize ($doc); |
174 |
|
|
} else { |
175 |
|
|
my $el = $doc->create_element_ns |
176 |
|
|
('http://www.w3.org/1999/xhtml', [undef, $test->{element}]); |
177 |
wakaba |
1.33 |
Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk); |
178 |
wakaba |
1.5 |
$result = serialize ($el); |
179 |
|
|
} |
180 |
|
|
|
181 |
wakaba |
1.1 |
ok scalar @errors, scalar @{$test->{errors}}, |
182 |
wakaba |
1.21 |
'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' . |
183 |
wakaba |
1.1 |
join (', ', @errors) . ';' . join (', ', @{$test->{errors}}); |
184 |
wakaba |
1.30 |
ok scalar @shoulds, scalar @{$test->{shoulds} or []}, |
185 |
|
|
'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' . |
186 |
|
|
join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []}); |
187 |
wakaba |
1.1 |
|
188 |
wakaba |
1.21 |
ok $result, $test->{document}, |
189 |
|
|
'Document tree: ' . Data::Dumper::qquote ($test->{data}); |
190 |
wakaba |
1.1 |
} # test |
191 |
|
|
|
192 |
|
|
sub serialize ($) { |
193 |
|
|
my $node = shift; |
194 |
|
|
my $r = ''; |
195 |
|
|
|
196 |
|
|
my @node = map { [$_, ''] } @{$node->child_nodes}; |
197 |
|
|
while (@node) { |
198 |
|
|
my $child = shift @node; |
199 |
|
|
my $nt = $child->[0]->node_type; |
200 |
|
|
if ($nt == $child->[0]->ELEMENT_NODE) { |
201 |
|
|
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
202 |
|
|
|
203 |
wakaba |
1.2 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
204 |
wakaba |
1.1 |
@{$child->[0]->attributes}) { |
205 |
|
|
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
206 |
|
|
$r .= $attr->[1] . '"' . "\x0A"; |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
unshift @node, |
210 |
|
|
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
211 |
|
|
} elsif ($nt == $child->[0]->TEXT_NODE) { |
212 |
|
|
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
213 |
|
|
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
214 |
|
|
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
215 |
|
|
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
216 |
wakaba |
1.24 |
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name; |
217 |
|
|
my $pubid = $child->[0]->public_id; |
218 |
|
|
$r .= ' PUBLIC "' . $pubid . '"' if length $pubid; |
219 |
|
|
my $sysid = $child->[0]->system_id; |
220 |
|
|
$r .= ' SYSTEM' if not length $pubid and length $sysid; |
221 |
|
|
$r .= ' "' . $sysid . '"' if length $sysid; |
222 |
|
|
$r .= ">\x0A"; |
223 |
wakaba |
1.1 |
} else { |
224 |
|
|
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
return $r; |
229 |
|
|
} # serialize |
230 |
|
|
|
231 |
|
|
## License: Public Domain. |
232 |
wakaba |
1.34 |
## $Date: 2008/09/15 07:19:03 $ |