/[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.17 by wakaba, Sat Jun 30 14:13:20 2007 UTC revision 1.40 by wakaba, Tue Oct 14 06:08:26 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 => 632 }  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  for my $file_name (grep {$_} split /\s+/, qq[  
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                        ${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                        ${dir_name}tests5.dat
60                        ${dir_name}tests6.dat                        ${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                        ${test_dir_name}tree-test-2.dat
69                       ]) {                        ${test_dir_name}tree-test-3.dat
70    open my $file, '<', $file_name                        ${test_dir_name}tree-test-void.dat
71      or die "$0: $file_name: $!";                        ${test_dir_name}tree-test-flow.dat
72    print "# $file_name\n";                        ${test_dir_name}tree-test-phrasing.dat
73                          ${test_dir_name}tree-test-form.dat
74    my $test;                        ${test_dir_name}tree-test-foreign.dat
75    my $mode = 'data';                       ];
76    my $escaped;  
77    while (<$file>) {  require 't/testfiles.pl';
78      s/\x0D\x0A/\x0A/;  execute_test ($_, {
79      if (/^#data$/) {    errors => {is_list => 1},
80        undef $test;    shoulds => {is_list => 1},
81        $test->{data} = '';    document => {is_prefixed => 1},
82        $mode = 'data';    'document-fragment' => {is_prefixed => 1},
83        undef $escaped;  }, \&test) for @FILES;
     } elsif (/^#data escaped$/) {  
       undef $test;  
       $test->{data} = '';  
       $mode = 'data';  
       $escaped = 1;  
     } elsif (/^#errors$/) {  
       $test->{errors} = [];  
       $mode = 'errors';  
       $test->{data} =~ s/\x0D?\x0A\z//;        
       $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;  
       undef $escaped;  
     } elsif (/^#document$/) {  
       $test->{document} = '';  
       $mode = 'document';  
       undef $escaped;  
     } elsif (/^#document escaped$/) {  
       $test->{document} = '';  
       $mode = 'document';  
       $escaped = 1;  
     } elsif (/^#document-fragment (\S+)$/) {  
       $test->{document} = '';  
       $mode = 'document';  
       $test->{element} = $1;  
       undef $escaped;  
     } elsif (/^#document-fragment (\S+) escaped$/) {  
       $test->{document} = '';  
       $mode = 'document';  
       $test->{element} = $1;  
       $escaped = 1;  
     } elsif (defined $test->{document} and /^$/) {  
       $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;  
       test ($test);  
       undef $test;  
     } else {  
       if ($mode eq 'data' or $mode eq 'document') {  
         $test->{$mode} .= $_;  
       } elsif ($mode eq 'errors') {  
         tr/\x0D\x0A//d;  
         push @{$test->{errors}}, $_;  
       }  
     }  
   }  
   test ($test) if $test->{errors};  
 }  
84    
85  use Whatpm::HTML;  use Whatpm::HTML;
86  use Whatpm::NanoDOM;  use Whatpm::NanoDOM;
87    use Whatpm::Charset::UnicodeChecker;
88    
89  sub test ($) {  sub test ($) {
90    my $test = shift;    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;    my $doc = Whatpm::NanoDOM::Document->new;
104    my @errors;    my @errors;
105      my @shoulds;
106        
107    $SIG{INT} = sub {    $SIG{INT} = sub {
108      print scalar serialize ($doc);      print scalar serialize ($doc);
# Line 116  sub test ($) { Line 111  sub test ($) {
111    
112    my $onerror = sub {    my $onerror = sub {
113      my %opt = @_;      my %opt = @_;
114      push @errors, join ':', $opt{line}, $opt{column}, $opt{type};      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;    my $result;
126    unless (defined $test->{element}) {    unless (defined $test->{element}) {
127      Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);      Whatpm::HTML->parse_char_string
128            ($test->{data}->[0] => $doc, $onerror, $chk);
129      $result = serialize ($doc);      $result = serialize ($doc);
130    } else {    } else {
131      my $el = $doc->create_element_ns      my $el = $doc->create_element_ns
132        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
133      Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);      Whatpm::HTML->set_inner_html ($el, $test->{data}->[0], $onerror, $chk);
134      $result = serialize ($el);      $result = serialize ($el);
135    }    }
136      
137      warn "No #errors section" unless $test->{errors};
138            
139    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}->[0] or []},
140      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' .
141      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      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}, 'Document tree: ' . $test->{data};    ok $result, $test->{document}->[0] . "\x0A",
147          'Document tree: ' . Data::Dumper::qquote ($test->{data}->[0]);
148  } # test  } # test
149    
150    ## NOTE: Spec: <http://wiki.whatwg.org/wiki/Parser_tests>.
151  sub serialize ($) {  sub serialize ($) {
152    my $node = shift;    my $node = shift;
153    my $r = '';    my $r = '';
# Line 145  sub serialize ($) { Line 157  sub serialize ($) {
157      my $child = shift @node;      my $child = shift @node;
158      my $nt = $child->[0]->node_type;      my $nt = $child->[0]->node_type;
159      if ($nt == $child->[0]->ELEMENT_NODE) {      if ($nt == $child->[0]->ELEMENT_NODE) {
160        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?        $r .= $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
161    
162        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
163                      @{$child->[0]->attributes}) {                      @{$child->[0]->attributes}) {
164          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?          $r .= $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?
165          $r .= $attr->[1] . '"' . "\x0A";          $r .= $attr->[1] . '"' . "\x0A";
166        }        }
167                
168        unshift @node,        unshift @node,
169          map { [$_, $child->[1] . '  '] } @{$child->[0]->child_nodes};          map { [$_, $child->[1] . '  '] } @{$child->[0]->child_nodes};
170      } elsif ($nt == $child->[0]->TEXT_NODE) {      } elsif ($nt == $child->[0]->TEXT_NODE) {
171        $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";        $r .= $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
172      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
173        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
174      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
175        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
176          my $pubid = $child->[0]->public_id;
177          my $sysid = $child->[0]->system_id;
178          if (length $pubid or length $sysid) {
179            $r .= ' "' . $pubid . '"';
180            $r .= ' "' . $sysid . '"';
181          }
182          $r .= ">\x0A";
183      } else {      } else {
184        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= $child->[1] . $child->[0]->node_type . "\x0A"; # error
185      }      }
186    }    }
187        

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.40

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24