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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by wakaba, Tue May 1 07:46:42 2007 UTC revision 1.37 by wakaba, Sat Oct 4 12:20:36 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2  use strict;  use strict;
3    
4    my $DEBUG = $ENV{DEBUG};
5    
6    use lib qw[/home/wakaba/work/manakai2/lib];
7    
8  my $dir_name;  my $dir_name;
9  my $test_dir_name;  my $test_dir_name;
10  BEGIN {  BEGIN {
# Line 20  BEGIN { Line 24  BEGIN {
24  }  }
25    
26  use Test;  use Test;
27  BEGIN { plan tests => 410 }  BEGIN { plan tests => 3105 }
28    
29  use Data::Dumper;  use Data::Dumper;
30  $Data::Dumper::Useqq = 1;  $Data::Dumper::Useqq = 1;
# Line 30  sub Data::Dumper::qquote { Line 34  sub Data::Dumper::qquote {
34    return q<qq'> . $s . q<'>;    return q<qq'> . $s . q<'>;
35  } # Data::Dumper::qquote  } # 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  for my $file_name (grep {$_} split /\s+/, qq[  for my $file_name (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                        ${dir_name}tests1.dat
56                        ${dir_name}tests2.dat                        ${dir_name}tests2.dat
57                        ${dir_name}tests3.dat                        ${dir_name}tests3.dat
58                        ${dir_name}tests4.dat                        ${dir_name}tests4.dat
59                          ${dir_name}tests5.dat
60                          ${dir_name}tests6.dat
61                          ${dir_name}tests7.dat
62                        ${test_dir_name}tree-test-1.dat                        ${test_dir_name}tree-test-1.dat
63                          ${test_dir_name}tree-test-2.dat
64                          ${test_dir_name}tree-test-3.dat
65                          ${test_dir_name}tree-test-void.dat
66                          ${test_dir_name}tree-test-flow.dat
67                          ${test_dir_name}tree-test-phrasing.dat
68                          ${test_dir_name}tree-test-form.dat
69                          ${test_dir_name}tree-test-foreign.dat
70                       ]) {                       ]) {
71    open my $file, '<', $file_name    open my $file, '<', $file_name
72      or die "$0: $file_name: $!";      or die "$0: $file_name: $!";
73      print "# $file_name\n";
74    
75    my $test;    my $test;
76    my $mode = 'data';    my $mode = 'data';
77      my $escaped;
78    while (<$file>) {    while (<$file>) {
79      s/\x0D\x0A/\x0A/;      s/\x0D\x0A/\x0A/;
80      if (/^#data$/) {      if (/^#data$/) {
81        undef $test;        undef $test;
82        $test->{data} = '';        $test->{data} = '';
83        $mode = 'data';        $mode = 'data';
84          undef $escaped;
85        } elsif (/^#data escaped$/) {
86          undef $test;
87          $test->{data} = '';
88          $mode = 'data';
89          $escaped = 1;
90      } elsif (/^#errors$/) {      } elsif (/^#errors$/) {
91        $test->{errors} = [];        $test->{errors} = [];
92        $mode = 'errors';        $mode = 'errors';
93        $test->{data} =~ s/\x0D?\x0A\z//;              $test->{data} =~ s/\x0D?\x0A\z//;      
94          $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
95          $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
96          undef $escaped;
97        } elsif (/^#shoulds$/) {
98          $test->{shoulds} = [];
99          $mode = 'shoulds';
100      } elsif (/^#document$/) {      } elsif (/^#document$/) {
101        $test->{document} = '';        $test->{document} = '';
102        $mode = 'document';        $mode = 'document';
103          undef $escaped;
104        } elsif (/^#document escaped$/) {
105          $test->{document} = '';
106          $mode = 'document';
107          $escaped = 1;
108        } elsif (/^#document-fragment$/) {
109          $test->{element} = '';
110          $mode = 'element';
111          undef $escaped;
112        } elsif (/^#document-fragment (\S+)$/) {
113          $test->{document} = '';
114          $mode = 'document';
115          $test->{element} = $1;
116          undef $escaped;
117        } elsif (/^#document-fragment (\S+) escaped$/) {
118          $test->{document} = '';
119          $mode = 'document';
120          $test->{element} = $1;
121          $escaped = 1;
122      } elsif (defined $test->{document} and /^$/) {      } elsif (defined $test->{document} and /^$/) {
123          $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
124          $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
125        test ($test);        test ($test);
126        undef $test;        undef $test;
127      } else {      } else {
128        if ($mode eq 'data' or $mode eq 'document') {        if ($mode eq 'data' or $mode eq 'document') {
129          $test->{$mode} .= $_;          $test->{$mode} .= $_;
130          } elsif ($mode eq 'element') {
131            tr/\x0D\x0A//d;
132            $test->{$mode} .= $_;
133        } elsif ($mode eq 'errors') {        } elsif ($mode eq 'errors') {
134          tr/\x0D\x0A//d;          tr/\x0D\x0A//d;
135          push @{$test->{errors}}, $_;          push @{$test->{errors}}, $_;
136          } elsif ($mode eq 'shoulds') {
137            tr/\x0D\x0A//d;
138            push @{$test->{shoulds}}, $_;
139        }        }
140      }      }
141    }    }
142    test ($test) if $test->{errors};    test ($test) if $test->{errors};
143  }  }
144    
145  use What::HTML;  use Whatpm::HTML;
146  use What::NanoDOM;  use Whatpm::NanoDOM;
147    use Whatpm::Charset::UnicodeChecker;
148    
149  sub test ($) {  sub test ($) {
150    my $test = shift;    my $test = shift;
151    
152    my $doc = What::NanoDOM::Document->new;    my $doc = Whatpm::NanoDOM::Document->new;
153    my @errors;    my @errors;
154      my @shoulds;
155        
156    $SIG{INT} = sub {    $SIG{INT} = sub {
157      print scalar serialize ($doc);      print scalar serialize ($doc);
158      exit;      exit;
159    };    };
160    
161    What::HTML->parse_string    my $onerror = sub {
162        ($test->{data} => $doc, sub {      my %opt = @_;
163           my $msg = shift;      if ($opt{level} eq 's') {
164           push @errors, $msg;        push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
165         });      } else {
166          push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
167        }
168      };
169    
170      my $chk = sub {
171        return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
172      }; # $chk
173    
174      my $result;
175      unless (defined $test->{element}) {
176        Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
177        $result = serialize ($doc);
178      } else {
179        my $el = $doc->create_element_ns
180          ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
181        Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
182        $result = serialize ($el);
183      }
184        
185    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}},
186      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
187      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
188      ok scalar @shoulds, scalar @{$test->{shoulds} or []},
189        'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
190        join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
191    
192    my $doc_s = serialize ($doc);    ok $result, $test->{document},
193    ok $doc_s, $test->{document}, 'Document tree: ' . $test->{data};        'Document tree: ' . Data::Dumper::qquote ($test->{data});
194  } # test  } # test
195    
196  sub serialize ($) {  sub serialize ($) {
# Line 122  sub serialize ($) { Line 217  sub serialize ($) {
217      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
218        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
219      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
220        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
221          my $pubid = $child->[0]->public_id;
222          $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
223          my $sysid = $child->[0]->system_id;
224          $r .= ' SYSTEM' if not length $pubid and length $sysid;
225          $r .= ' "' . $sysid . '"' if length $sysid;
226          $r .= ">\x0A";
227      } else {      } else {
228        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
229      }      }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.37

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24