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.10 |
BEGIN { plan tests => 472 } |
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 |
|
|
${dir_name}tests1.dat |
35 |
|
|
${dir_name}tests2.dat |
36 |
|
|
${dir_name}tests3.dat |
37 |
|
|
${dir_name}tests4.dat |
38 |
|
|
${test_dir_name}tree-test-1.dat |
39 |
wakaba |
1.1 |
]) { |
40 |
wakaba |
1.2 |
open my $file, '<', $file_name |
41 |
|
|
or die "$0: $file_name: $!"; |
42 |
wakaba |
1.1 |
|
43 |
|
|
my $test; |
44 |
|
|
my $mode = 'data'; |
45 |
|
|
while (<$file>) { |
46 |
|
|
s/\x0D\x0A/\x0A/; |
47 |
|
|
if (/^#data$/) { |
48 |
|
|
undef $test; |
49 |
|
|
$test->{data} = ''; |
50 |
|
|
$mode = 'data'; |
51 |
|
|
} elsif (/^#errors$/) { |
52 |
|
|
$test->{errors} = []; |
53 |
|
|
$mode = 'errors'; |
54 |
|
|
$test->{data} =~ s/\x0D?\x0A\z//; |
55 |
|
|
} elsif (/^#document$/) { |
56 |
|
|
$test->{document} = ''; |
57 |
|
|
$mode = 'document'; |
58 |
wakaba |
1.5 |
} elsif (/^#document-fragment (\S+)$/) { |
59 |
|
|
$test->{document} = ''; |
60 |
|
|
$mode = 'document'; |
61 |
|
|
$test->{element} = $1; |
62 |
wakaba |
1.2 |
} elsif (defined $test->{document} and /^$/) { |
63 |
|
|
test ($test); |
64 |
wakaba |
1.1 |
undef $test; |
65 |
|
|
} else { |
66 |
|
|
if ($mode eq 'data' or $mode eq 'document') { |
67 |
|
|
$test->{$mode} .= $_; |
68 |
|
|
} elsif ($mode eq 'errors') { |
69 |
|
|
tr/\x0D\x0A//d; |
70 |
|
|
push @{$test->{errors}}, $_; |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
test ($test) if $test->{errors}; |
75 |
|
|
} |
76 |
|
|
|
77 |
wakaba |
1.4 |
use Whatpm::HTML; |
78 |
|
|
use Whatpm::NanoDOM; |
79 |
wakaba |
1.1 |
|
80 |
|
|
sub test ($) { |
81 |
|
|
my $test = shift; |
82 |
|
|
|
83 |
wakaba |
1.4 |
my $doc = Whatpm::NanoDOM::Document->new; |
84 |
wakaba |
1.1 |
my @errors; |
85 |
|
|
|
86 |
|
|
$SIG{INT} = sub { |
87 |
wakaba |
1.3 |
print scalar serialize ($doc); |
88 |
wakaba |
1.1 |
exit; |
89 |
|
|
}; |
90 |
wakaba |
1.3 |
|
91 |
wakaba |
1.5 |
my $onerror = sub { |
92 |
|
|
my %opt = @_; |
93 |
|
|
push @errors, join ':', $opt{line}, $opt{column}, $opt{type}; |
94 |
|
|
}; |
95 |
|
|
my $result; |
96 |
|
|
unless (defined $test->{element}) { |
97 |
|
|
Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror); |
98 |
|
|
$result = serialize ($doc); |
99 |
|
|
} else { |
100 |
|
|
my $el = $doc->create_element_ns |
101 |
|
|
('http://www.w3.org/1999/xhtml', [undef, $test->{element}]); |
102 |
|
|
Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror); |
103 |
|
|
$result = serialize ($el); |
104 |
|
|
} |
105 |
|
|
|
106 |
wakaba |
1.1 |
ok scalar @errors, scalar @{$test->{errors}}, |
107 |
|
|
'Parse error: ' . $test->{data} . '; ' . |
108 |
|
|
join (', ', @errors) . ';' . join (', ', @{$test->{errors}}); |
109 |
|
|
|
110 |
wakaba |
1.5 |
ok $result, $test->{document}, 'Document tree: ' . $test->{data}; |
111 |
wakaba |
1.1 |
} # test |
112 |
|
|
|
113 |
|
|
sub serialize ($) { |
114 |
|
|
my $node = shift; |
115 |
|
|
my $r = ''; |
116 |
|
|
|
117 |
|
|
my @node = map { [$_, ''] } @{$node->child_nodes}; |
118 |
|
|
while (@node) { |
119 |
|
|
my $child = shift @node; |
120 |
|
|
my $nt = $child->[0]->node_type; |
121 |
|
|
if ($nt == $child->[0]->ELEMENT_NODE) { |
122 |
|
|
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
123 |
|
|
|
124 |
wakaba |
1.2 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
125 |
wakaba |
1.1 |
@{$child->[0]->attributes}) { |
126 |
|
|
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
127 |
|
|
$r .= $attr->[1] . '"' . "\x0A"; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
unshift @node, |
131 |
|
|
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
132 |
|
|
} elsif ($nt == $child->[0]->TEXT_NODE) { |
133 |
|
|
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
134 |
|
|
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
135 |
|
|
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
136 |
|
|
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
137 |
|
|
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A"; |
138 |
|
|
} else { |
139 |
|
|
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
return $r; |
144 |
|
|
} # serialize |
145 |
|
|
|
146 |
|
|
## License: Public Domain. |
147 |
wakaba |
1.10 |
## $Date: 2007/05/26 08:12:34 $ |