/[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.2 by wakaba, Tue May 1 06:22:12 2007 UTC revision 1.38 by wakaba, Sat Oct 4 17:16:02 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 => 402 }  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                          ${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                        ${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    open my $file, '<', $file_name    open my $file, '<', $file_name
77      or die "$0: $file_name: $!";      or die "$0: $file_name: $!";
78      print "# $file_name\n";
79    
80    my $test;    my $test;
81    my $mode = 'data';    my $mode = 'data';
82      my $escaped;
83    while (<$file>) {    while (<$file>) {
84      s/\x0D\x0A/\x0A/;      s/\x0D\x0A/\x0A/;
85      if (/^#data$/) {      if (/^#data$/) {
86        undef $test;        undef $test;
87        $test->{data} = '';        $test->{data} = '';
88        $mode = 'data';        $mode = 'data';
89          undef $escaped;
90        } elsif (/^#data escaped$/) {
91          undef $test;
92          $test->{data} = '';
93          $mode = 'data';
94          $escaped = 1;
95      } elsif (/^#errors$/) {      } elsif (/^#errors$/) {
96        $test->{errors} = [];        $test->{errors} = [];
97        $mode = 'errors';        $mode = 'errors';
98        $test->{data} =~ s/\x0D?\x0A\z//;              $test->{data} =~ s/\x0D?\x0A\z//;      
99          $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
100          $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
101          undef $escaped;
102        } elsif (/^#shoulds$/) {
103          $test->{shoulds} = [];
104          $mode = 'shoulds';
105      } elsif (/^#document$/) {      } elsif (/^#document$/) {
106        $test->{document} = '';        $test->{document} = '';
107        $mode = 'document';        $mode = 'document';
108          undef $escaped;
109        } elsif (/^#document escaped$/) {
110          $test->{document} = '';
111          $mode = 'document';
112          $escaped = 1;
113        } elsif (/^#document-fragment$/) {
114          $test->{element} = '';
115          $mode = 'element';
116          undef $escaped;
117        } elsif (/^#document-fragment (\S+)$/) {
118          $test->{document} = '';
119          $mode = 'document';
120          $test->{element} = $1;
121          undef $escaped;
122        } elsif (/^#document-fragment (\S+) escaped$/) {
123          $test->{document} = '';
124          $mode = 'document';
125          $test->{element} = $1;
126          $escaped = 1;
127      } elsif (defined $test->{document} and /^$/) {      } elsif (defined $test->{document} and /^$/) {
128          $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
129          $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
130        test ($test);        test ($test);
131        undef $test;        undef $test;
132      } else {      } else {
133        if ($mode eq 'data' or $mode eq 'document') {        if ($mode eq 'data' or $mode eq 'document') {
134          $test->{$mode} .= $_;          $test->{$mode} .= $_;
135          } elsif ($mode eq 'element') {
136            tr/\x0D\x0A//d;
137            $test->{$mode} .= $_;
138        } elsif ($mode eq 'errors') {        } elsif ($mode eq 'errors') {
139          tr/\x0D\x0A//d;          tr/\x0D\x0A//d;
140          push @{$test->{errors}}, $_;          push @{$test->{errors}}, $_;
141          } elsif ($mode eq 'shoulds') {
142            tr/\x0D\x0A//d;
143            push @{$test->{shoulds}}, $_;
144        }        }
145      }      }
146    }    }
147    test ($test) if $test->{errors};    test ($test) if $test->{errors};
148  }  }
149    
150  use What::HTML;  use Whatpm::HTML;
151    use Whatpm::NanoDOM;
152    use Whatpm::Charset::UnicodeChecker;
153    
154  sub test ($) {  sub test ($) {
155    my $test = shift;    my $test = shift;
156    
157    my $s = $test->{data};    my $doc = Whatpm::NanoDOM::Document->new;
   
   my $p = What::HTML->new;  
   my $i = 0;  
   $p->{set_next_input_character} = sub {  
     my $self = shift;  
     $self->{next_input_character} = -1 and return if $i >= length $s;  
     $self->{next_input_character} = ord substr $s, $i++, 1;  
       
     if ($self->{next_input_character} == 0x000D) { # CR  
       if ($i >= length $s) {  
         #  
       } else {  
         my $next_char = ord substr $s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
       $self->{next_input_character} = 0x000A; # LF # MUST  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     }  
   };  
     
158    my @errors;    my @errors;
159    $p->{parse_error} = sub {    my @shoulds;
     my $msg = shift;  
     push @errors, $msg;  
   };  
160        
161    $SIG{INT} = sub {    $SIG{INT} = sub {
162      print scalar serialize ($p->{document});      print scalar serialize ($doc);
163      exit;      exit;
164    };    };
     
   $p->_initialize_tokenizer;  
   $p->_initialize_tree_constructor;  
   $p->_construct_tree;  
   $p->_terminate_tree_constructor;  
165    
166      my $onerror = sub {
167        my %opt = @_;
168        if ($opt{level} eq 's') {
169          push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
170        } else {
171          push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
172        }
173      };
174    
175      my $chk = sub {
176        return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
177      }; # $chk
178    
179      my $result;
180      unless (defined $test->{element}) {
181        Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
182        $result = serialize ($doc);
183      } else {
184        my $el = $doc->create_element_ns
185          ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
186        Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
187        $result = serialize ($el);
188      }
189        
190    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}},
191      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
192      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
193      ok scalar @shoulds, scalar @{$test->{shoulds} or []},
194        'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
195        join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
196    
197    my $doc = $p->{document};    ok $result, $test->{document},
198    my $doc_s = serialize ($doc);        'Document tree: ' . Data::Dumper::qquote ($test->{data});
   ok $doc_s, $test->{document}, 'Document tree: ' . $test->{data};  
199  } # test  } # test
200    
201  sub serialize ($) {  sub serialize ($) {
# Line 152  sub serialize ($) { Line 222  sub serialize ($) {
222      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
223        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
224      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
225        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
226          my $pubid = $child->[0]->public_id;
227          $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
228          my $sysid = $child->[0]->system_id;
229          $r .= ' SYSTEM' if not length $pubid and length $sysid;
230          $r .= ' "' . $sysid . '"' if length $sysid;
231          $r .= ">\x0A";
232      } else {      } else {
233        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
234      }      }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.38

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24