/[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.1 by wakaba, Mon Apr 30 14:12:02 2007 UTC revision 1.34 by wakaba, Sat Oct 4 05:53:46 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;
10  BEGIN {  BEGIN {
11    my $test_dir_name = 't/';    $test_dir_name = 't/';
12    $dir_name = 't/tree-construction/';    $dir_name = 't/tree-construction/';
13    my $skip = "You don't have make command";    my $skip = "You don't have make command";
14    eval q{    eval q{
# Line 19  BEGIN { Line 24  BEGIN {
24  }  }
25    
26  use Test;  use Test;
27  BEGIN { plan tests => 67 }  BEGIN { plan tests => 3105 }
28    
29  use Data::Dumper;  use Data::Dumper;
30  $Data::Dumper::Useqq = 1;  $Data::Dumper::Useqq = 1;
# Line 29  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 (qw[  
38                        tests1.dat  if ($DEBUG) {
39                        tests2.dat    my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
40                        tests3.dat    $Whatpm::HTML::Debug::cp_pass = sub {
41                        tests4.dat      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[
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                          ${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                       ]) {                       ]) {
67    open my $file, '<', $dir_name.$file_name    open my $file, '<', $file_name
68      or die "$0: $dir_name$file_name: $!";      or die "$0: $file_name: $!";
69      print "# $file_name\n";
70    
71    my $test;    my $test;
72    my $mode = 'data';    my $mode = 'data';
73      my $escaped;
74    while (<$file>) {    while (<$file>) {
75      s/\x0D\x0A/\x0A/;      s/\x0D\x0A/\x0A/;
76      if (/^#data$/) {      if (/^#data$/) {
77        undef $test;        undef $test;
78        $test->{data} = '';        $test->{data} = '';
79        $mode = 'data';        $mode = 'data';
80          undef $escaped;
81        } elsif (/^#data escaped$/) {
82          undef $test;
83          $test->{data} = '';
84          $mode = 'data';
85          $escaped = 1;
86      } elsif (/^#errors$/) {      } elsif (/^#errors$/) {
87        $test->{errors} = [];        $test->{errors} = [];
88        $mode = 'errors';        $mode = 'errors';
89        $test->{data} =~ s/\x0D?\x0A\z//;              $test->{data} =~ s/\x0D?\x0A\z//;      
90          $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
91          $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
92          undef $escaped;
93        } elsif (/^#shoulds$/) {
94          $test->{shoulds} = [];
95          $mode = 'shoulds';
96      } elsif (/^#document$/) {      } elsif (/^#document$/) {
97        $test->{document} = '';        $test->{document} = '';
98        $mode = 'document';        $mode = 'document';
99      } elsif (/^$/) {        undef $escaped;
100        test ($test) if $test->{errors};      } elsif (/^#document escaped$/) {
101          $test->{document} = '';
102          $mode = 'document';
103          $escaped = 1;
104        } elsif (/^#document-fragment$/) {
105          $test->{element} = '';
106          $mode = 'element';
107          undef $escaped;
108        } elsif (/^#document-fragment (\S+)$/) {
109          $test->{document} = '';
110          $mode = 'document';
111          $test->{element} = $1;
112          undef $escaped;
113        } elsif (/^#document-fragment (\S+) escaped$/) {
114          $test->{document} = '';
115          $mode = 'document';
116          $test->{element} = $1;
117          $escaped = 1;
118        } elsif (defined $test->{document} and /^$/) {
119          $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
120          $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
121          test ($test);
122        undef $test;        undef $test;
123      } else {      } else {
124        if ($mode eq 'data' or $mode eq 'document') {        if ($mode eq 'data' or $mode eq 'document') {
125          $test->{$mode} .= $_;          $test->{$mode} .= $_;
126          } elsif ($mode eq 'element') {
127            tr/\x0D\x0A//d;
128            $test->{$mode} .= $_;
129        } elsif ($mode eq 'errors') {        } elsif ($mode eq 'errors') {
130          tr/\x0D\x0A//d;          tr/\x0D\x0A//d;
131          push @{$test->{errors}}, $_;          push @{$test->{errors}}, $_;
132          } elsif ($mode eq 'shoulds') {
133            tr/\x0D\x0A//d;
134            push @{$test->{shoulds}}, $_;
135        }        }
136      }      }
137    }    }
138    test ($test) if $test->{errors};    test ($test) if $test->{errors};
139  }  }
140    
141  use What::HTML;  use Whatpm::HTML;
142    use Whatpm::NanoDOM;
143    use Whatpm::Charset::UnicodeChecker;
144    
145  sub test ($) {  sub test ($) {
146    my $test = shift;    my $test = shift;
147    
148    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  
     }  
   };  
     
149    my @errors;    my @errors;
150    $p->{parse_error} = sub {    my @shoulds;
     my $msg = shift;  
     push @errors, $msg;  
   };  
151        
152    $SIG{INT} = sub {    $SIG{INT} = sub {
153      print scalar serialize ($p->{document});      print scalar serialize ($doc);
154      exit;      exit;
155    };    };
     
   $p->_initialize_tokenizer;  
   $p->_initialize_tree_constructor;  
   $p->_construct_tree;  
   $p->_terminate_tree_constructor;  
156    
157      my $onerror = sub {
158        my %opt = @_;
159        if ($opt{level} eq 's') {
160          push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
161        } else {
162          push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
163        }
164      };
165    
166      my $chk = sub {
167        return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
168      }; # $chk
169    
170      my $result;
171      unless (defined $test->{element}) {
172        Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
173        $result = serialize ($doc);
174      } else {
175        my $el = $doc->create_element_ns
176          ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
177        Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
178        $result = serialize ($el);
179      }
180        
181    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}},
182      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
183      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
184      ok scalar @shoulds, scalar @{$test->{shoulds} or []},
185        'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
186        join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
187    
188    my $doc = $p->{document};    ok $result, $test->{document},
189    my $doc_s = serialize ($doc);        'Document tree: ' . Data::Dumper::qquote ($test->{data});
   ok $doc_s, $test->{document}, 'Document tree: ' . $test->{data};  
190  } # test  } # test
191    
192  sub serialize ($) {  sub serialize ($) {
# Line 137  sub serialize ($) { Line 200  sub serialize ($) {
200      if ($nt == $child->[0]->ELEMENT_NODE) {      if ($nt == $child->[0]->ELEMENT_NODE) {
201        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
202    
203        for my $attr (sort {$a->[1] cmp $b->[1]} map { [$_->name, $_->value] }        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
204                      @{$child->[0]->attributes}) {                      @{$child->[0]->attributes}) {
205          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?
206          $r .= $attr->[1] . '"' . "\x0A";          $r .= $attr->[1] . '"' . "\x0A";
# Line 150  sub serialize ($) { Line 213  sub serialize ($) {
213      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
214        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
215      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
216        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
217          my $pubid = $child->[0]->public_id;
218          $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
219          my $sysid = $child->[0]->system_id;
220          $r .= ' SYSTEM' if not length $pubid and length $sysid;
221          $r .= ' "' . $sysid . '"' if length $sysid;
222          $r .= ">\x0A";
223      } else {      } else {
224        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
225      }      }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.34

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24