/[suikacvs]/markup/html/whatpm/t/HTML-tree.t
Suika

Contents of /markup/html/whatpm/t/HTML-tree.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (show annotations) (download) (as text)
Tue Oct 14 05:58:26 2008 UTC (17 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.38: +42 -92 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	14 Oct 2008 05:58:24 -0000
2008-10-14  Wakaba  <wakaba@suika.fam.cx>

	* HTML-tree.t: Test data file parser changed to the common one.

1 #!/usr/bin/perl
2 use strict;
3
4 my $DEBUG = $ENV{DEBUG};
5
6 use lib qw[/home/wakaba/work/manakai2/lib];
7
8 my $dir_name;
9 my $test_dir_name;
10 BEGIN {
11 $test_dir_name = 't/';
12 $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 BEGIN { plan tests => 3105 }
28
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
38 if ($DEBUG) {
39 my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
40 $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 my @FILES = grep {$_} split /\s+/, qq[
53 ${test_dir_name}tokenizer-test-2.dat
54 ${test_dir_name}tokenizer-test-3.dat
55 ${dir_name}tests1.dat
56 ${dir_name}tests2.dat
57 ${dir_name}tests3.dat
58 ${dir_name}tests4.dat
59 ${dir_name}tests5.dat
60 ${dir_name}tests6.dat
61 ${dir_name}tests7.dat
62 ${dir_name}tests8.dat
63 ${dir_name}tests9.dat
64 ${dir_name}tests10.dat
65 ${dir_name}tests11.dat
66 ${dir_name}tests12.dat
67 ${test_dir_name}tree-test-1.dat
68 ${test_dir_name}tree-test-2.dat
69 ${test_dir_name}tree-test-3.dat
70 ${test_dir_name}tree-test-void.dat
71 ${test_dir_name}tree-test-flow.dat
72 ${test_dir_name}tree-test-phrasing.dat
73 ${test_dir_name}tree-test-form.dat
74 ${test_dir_name}tree-test-foreign.dat
75 ];
76
77 require 't/testfiles.pl';
78 execute_test ($_, {
79 errors => {is_list => 1},
80 shoulds => {is_list => 1},
81 document => {is_prefixed => 1},
82 'document-fragment' => {is_prefixed => 1},
83 }, \&test) for @FILES;
84
85 use Whatpm::HTML;
86 use Whatpm::NanoDOM;
87 use Whatpm::Charset::UnicodeChecker;
88
89 sub test ($) {
90 my $test = shift;
91
92 if ($test->{'document-fragment'}) {
93 if (@{$test->{'document-fragment'}->[1]}) {
94 ## NOTE: Old format.
95 $test->{element} = $test->{'document-fragment'}->[1]->[0];
96 $test->{document} ||= $test->{'document-fragment'};
97 } else {
98 ## NOTE: New format.
99 $test->{element} = $test->{'document-fragment'}->[0];
100 }
101 }
102
103 my $doc = Whatpm::NanoDOM::Document->new;
104 my @errors;
105 my @shoulds;
106
107 $SIG{INT} = sub {
108 print scalar serialize ($doc);
109 exit;
110 };
111
112 my $onerror = sub {
113 my %opt = @_;
114 if ($opt{level} eq 's') {
115 push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
116 } else {
117 push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
118 }
119 };
120
121 my $chk = sub {
122 return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
123 }; # $chk
124
125 my $result;
126 unless (defined $test->{element}) {
127 Whatpm::HTML->parse_char_string
128 ($test->{data}->[0] => $doc, $onerror, $chk);
129 $result = serialize ($doc);
130 } else {
131 my $el = $doc->create_element_ns
132 ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
133 Whatpm::HTML->set_inner_html ($el, $test->{data}->[0], $onerror, $chk);
134 $result = serialize ($el);
135 }
136
137 warn "No #errors section" unless $test->{errors};
138
139 ok scalar @errors, scalar @{$test->{errors}->[0] or []},
140 'Parse error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' .
141 join (', ', @errors) . ';' . join (', ', @{$test->{errors}->[0] or []});
142 ok scalar @shoulds, scalar @{$test->{shoulds}->[0] or []},
143 'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' .
144 join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds}->[0] or []});
145
146 ok $result, $test->{document}->[0] . "\x0A",
147 'Document tree: ' . Data::Dumper::qquote ($test->{data}->[0]);
148 } # test
149
150 ## NOTE: Spec: <http://wiki.whatwg.org/wiki/Parser_tests>.
151 sub serialize ($) {
152 my $node = shift;
153 my $r = '';
154
155 my @node = map { [$_, ''] } @{$node->child_nodes};
156 while (@node) {
157 my $child = shift @node;
158 my $nt = $child->[0]->node_type;
159 if ($nt == $child->[0]->ELEMENT_NODE) {
160 $r .= $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
161
162 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
163 @{$child->[0]->attributes}) {
164 $r .= $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
165 $r .= $attr->[1] . '"' . "\x0A";
166 }
167
168 unshift @node,
169 map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
170 } elsif ($nt == $child->[0]->TEXT_NODE) {
171 $r .= $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
172 } elsif ($nt == $child->[0]->COMMENT_NODE) {
173 $r .= $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
174 } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
175 $r .= $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
176 my $pubid = $child->[0]->public_id;
177 $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
178 my $sysid = $child->[0]->system_id;
179 $r .= ' SYSTEM' if not length $pubid and length $sysid;
180 $r .= ' "' . $sysid . '"' if length $sysid;
181 $r .= ">\x0A";
182 } else {
183 $r .= $child->[1] . $child->[0]->node_type . "\x0A"; # error
184 }
185 }
186
187 return $r;
188 } # serialize
189
190 ## License: Public Domain.
191 ## $Date: 2008/10/04 17:16:02 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24