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