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