/[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.7 by wakaba, Sat May 5 06:51:06 2007 UTC revision 1.35 by wakaba, Sat Oct 4 06:30:34 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 => 434 }  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                       ]) {                       ]) {
69    open my $file, '<', $file_name    open my $file, '<', $file_name
70      or die "$0: $file_name: $!";      or die "$0: $file_name: $!";
71      print "# $file_name\n";
72    
73    my $test;    my $test;
74    my $mode = 'data';    my $mode = 'data';
75      my $escaped;
76    while (<$file>) {    while (<$file>) {
77      s/\x0D\x0A/\x0A/;      s/\x0D\x0A/\x0A/;
78      if (/^#data$/) {      if (/^#data$/) {
79        undef $test;        undef $test;
80        $test->{data} = '';        $test->{data} = '';
81        $mode = 'data';        $mode = 'data';
82          undef $escaped;
83        } elsif (/^#data escaped$/) {
84          undef $test;
85          $test->{data} = '';
86          $mode = 'data';
87          $escaped = 1;
88      } elsif (/^#errors$/) {      } elsif (/^#errors$/) {
89        $test->{errors} = [];        $test->{errors} = [];
90        $mode = 'errors';        $mode = 'errors';
91        $test->{data} =~ s/\x0D?\x0A\z//;              $test->{data} =~ s/\x0D?\x0A\z//;      
92          $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
93          $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
94          undef $escaped;
95        } elsif (/^#shoulds$/) {
96          $test->{shoulds} = [];
97          $mode = 'shoulds';
98      } elsif (/^#document$/) {      } elsif (/^#document$/) {
99        $test->{document} = '';        $test->{document} = '';
100        $mode = 'document';        $mode = 'document';
101          undef $escaped;
102        } elsif (/^#document escaped$/) {
103          $test->{document} = '';
104          $mode = 'document';
105          $escaped = 1;
106        } elsif (/^#document-fragment$/) {
107          $test->{element} = '';
108          $mode = 'element';
109          undef $escaped;
110      } elsif (/^#document-fragment (\S+)$/) {      } elsif (/^#document-fragment (\S+)$/) {
111        $test->{document} = '';        $test->{document} = '';
112        $mode = 'document';        $mode = 'document';
113        $test->{element} = $1;        $test->{element} = $1;
114          undef $escaped;
115        } elsif (/^#document-fragment (\S+) escaped$/) {
116          $test->{document} = '';
117          $mode = 'document';
118          $test->{element} = $1;
119          $escaped = 1;
120      } elsif (defined $test->{document} and /^$/) {      } elsif (defined $test->{document} and /^$/) {
121          $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
122          $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
123        test ($test);        test ($test);
124        undef $test;        undef $test;
125      } else {      } else {
126        if ($mode eq 'data' or $mode eq 'document') {        if ($mode eq 'data' or $mode eq 'document') {
127          $test->{$mode} .= $_;          $test->{$mode} .= $_;
128          } elsif ($mode eq 'element') {
129            tr/\x0D\x0A//d;
130            $test->{$mode} .= $_;
131        } elsif ($mode eq 'errors') {        } elsif ($mode eq 'errors') {
132          tr/\x0D\x0A//d;          tr/\x0D\x0A//d;
133          push @{$test->{errors}}, $_;          push @{$test->{errors}}, $_;
134          } elsif ($mode eq 'shoulds') {
135            tr/\x0D\x0A//d;
136            push @{$test->{shoulds}}, $_;
137        }        }
138      }      }
139    }    }
# Line 76  for my $file_name (grep {$_} split /\s+/ Line 142  for my $file_name (grep {$_} split /\s+/
142    
143  use Whatpm::HTML;  use Whatpm::HTML;
144  use Whatpm::NanoDOM;  use Whatpm::NanoDOM;
145    use Whatpm::Charset::UnicodeChecker;
146    
147  sub test ($) {  sub test ($) {
148    my $test = shift;    my $test = shift;
149    
150    my $doc = Whatpm::NanoDOM::Document->new;    my $doc = Whatpm::NanoDOM::Document->new;
151    my @errors;    my @errors;
152      my @shoulds;
153        
154    $SIG{INT} = sub {    $SIG{INT} = sub {
155      print scalar serialize ($doc);      print scalar serialize ($doc);
# Line 90  sub test ($) { Line 158  sub test ($) {
158    
159    my $onerror = sub {    my $onerror = sub {
160      my %opt = @_;      my %opt = @_;
161      push @errors, join ':', $opt{line}, $opt{column}, $opt{type};      if ($opt{level} eq 's') {
162          push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
163        } else {
164          push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
165        }
166    };    };
167    
168      my $chk = sub {
169        return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
170      }; # $chk
171    
172    my $result;    my $result;
173    unless (defined $test->{element}) {    unless (defined $test->{element}) {
174      Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);      Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
175      $result = serialize ($doc);      $result = serialize ($doc);
176    } else {    } else {
177      my $el = $doc->create_element_ns      my $el = $doc->create_element_ns
178        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
179      Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);      Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
180      $result = serialize ($el);      $result = serialize ($el);
181    }    }
182            
183    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}},
184      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
185      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
186      ok scalar @shoulds, scalar @{$test->{shoulds} or []},
187        'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
188        join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
189    
190    ok $result, $test->{document}, 'Document tree: ' . $test->{data};    ok $result, $test->{document},
191          'Document tree: ' . Data::Dumper::qquote ($test->{data});
192  } # test  } # test
193    
194  sub serialize ($) {  sub serialize ($) {
# Line 134  sub serialize ($) { Line 215  sub serialize ($) {
215      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
216        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
217      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
218        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
219          my $pubid = $child->[0]->public_id;
220          $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
221          my $sysid = $child->[0]->system_id;
222          $r .= ' SYSTEM' if not length $pubid and length $sysid;
223          $r .= ' "' . $sysid . '"' if length $sysid;
224          $r .= ">\x0A";
225      } else {      } else {
226        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
227      }      }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.35

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24