/[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.5 by wakaba, Wed May 2 13:44:35 2007 UTC revision 1.45 by wakaba, Tue Oct 14 13:24:53 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2  use strict;  use strict;
3    
4  my $dir_name;  my $DEBUG = $ENV{DEBUG};
5  my $test_dir_name;  
6  BEGIN {  use lib qw[/home/wakaba/work/manakai2/lib];
7    $test_dir_name = 't/';  my $test_dir_name = 't/';
8    $dir_name = 't/tree-construction/';  my $dir_name = 't/tree-construction/';
   my $skip = "You don't have make command";  
   eval q{  
          system ("cd $test_dir_name; make tree-construction-files") == 0 or die  
            unless -f $dir_name.'tests1.dat';  
          $skip = '';  
         };  
   if ($skip) {  
     print "1..1\n";  
     print "ok 1 # $skip\n";  
     exit;  
   }  
 }  
9    
10  use Test;  use Test;
11  BEGIN { plan tests => 422 }  BEGIN { plan tests => 5058 }
12    
13  use Data::Dumper;  use Data::Dumper;
14  $Data::Dumper::Useqq = 1;  $Data::Dumper::Useqq = 1;
# Line 30  sub Data::Dumper::qquote { Line 18  sub Data::Dumper::qquote {
18    return q<qq'> . $s . q<'>;    return q<qq'> . $s . q<'>;
19  } # Data::Dumper::qquote  } # Data::Dumper::qquote
20    
21  for my $file_name (grep {$_} split /\s+/, qq[  if ($DEBUG) {
22                        ${dir_name}tests1.dat    my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
23                        ${dir_name}tests2.dat    $Whatpm::HTML::Debug::cp_pass = sub {
24                        ${dir_name}tests3.dat      my $id = shift;
25                        ${dir_name}tests4.dat      delete $not_found->{$id};
26                        ${test_dir_name}tree-test-1.dat    };
27                       ]) {  
28    open my $file, '<', $file_name    END {
29      or die "$0: $file_name: $!";      for my $id (sort {$a <=> $b || $a cmp $b} keys %$not_found) {
30          print "# checkpoint $id is not reached\n";
   my $test;  
   my $mode = 'data';  
   while (<$file>) {  
     s/\x0D\x0A/\x0A/;  
     if (/^#data$/) {  
       undef $test;  
       $test->{data} = '';  
       $mode = 'data';  
     } elsif (/^#errors$/) {  
       $test->{errors} = [];  
       $mode = 'errors';  
       $test->{data} =~ s/\x0D?\x0A\z//;        
     } elsif (/^#document$/) {  
       $test->{document} = '';  
       $mode = 'document';  
     } elsif (/^#document-fragment (\S+)$/) {  
       $test->{document} = '';  
       $mode = 'document';  
       $test->{element} = $1;  
     } elsif (defined $test->{document} and /^$/) {  
       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}}, $_;  
       }  
31      }      }
32    }    }
   test ($test) if $test->{errors};  
33  }  }
34    
35  use Whatpm::HTML;  use Whatpm::HTML;
36  use Whatpm::NanoDOM;  use Whatpm::NanoDOM;
37    use Whatpm::Charset::UnicodeChecker;
38    use Whatpm::HTML::Dumper qw/dumptree/;
39    
40  sub test ($) {  sub test ($) {
41    my $test = shift;    my $test = shift;
42    
43      if ($test->{'document-fragment'}) {
44        if (@{$test->{'document-fragment'}->[1]}) {
45          ## NOTE: Old format.
46          $test->{element} = $test->{'document-fragment'}->[1]->[0];
47          $test->{document} ||= $test->{'document-fragment'};
48        } else {
49          ## NOTE: New format.
50          $test->{element} = $test->{'document-fragment'}->[0];
51        }
52      }
53    
54    my $doc = Whatpm::NanoDOM::Document->new;    my $doc = Whatpm::NanoDOM::Document->new;
55    my @errors;    my @errors;
56      my @shoulds;
57        
58    $SIG{INT} = sub {    $SIG{INT} = sub {
59      print scalar serialize ($doc);      print scalar dumptree ($doc);
60      exit;      exit;
61    };    };
62    
63    my $onerror = sub {    my $onerror = sub {
64      my %opt = @_;      my %opt = @_;
65      push @errors, join ':', $opt{line}, $opt{column}, $opt{type};      if ($opt{level} eq 's') {
66          push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
67        } else {
68          push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
69        }
70    };    };
71    
72      my $chk = sub {
73        return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
74      }; # $chk
75    
76    my $result;    my $result;
77    unless (defined $test->{element}) {    unless (defined $test->{element}) {
78      Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);      Whatpm::HTML->parse_char_string
79      $result = serialize ($doc);          ($test->{data}->[0] => $doc, $onerror, $chk);
80        $result = dumptree ($doc);
81    } else {    } else {
82      my $el = $doc->create_element_ns      my $el = $doc->create_element_ns
83        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);        ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
84      Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);      Whatpm::HTML->set_inner_html ($el, $test->{data}->[0], $onerror, $chk);
85      $result = serialize ($el);      $result = dumptree ($el);
86    }    }
87      
88      warn "No #errors section ($test->{data}->[0])" unless $test->{errors};
89            
90    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}->[0] or []},
91      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' .
92      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}->[0] or []});
93      ok scalar @shoulds, scalar @{$test->{shoulds}->[0] or []},
94    ok $result, $test->{document}, 'Document tree: ' . $test->{data};      'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' .
95        join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds}->[0] or []});
96    
97      $test->{document}->[0] .= "\x0A" if length $test->{document}->[0];
98      ok $result, $test->{document}->[0],
99          'Document tree: ' . Data::Dumper::qquote ($test->{data}->[0]);
100  } # test  } # test
101    
102  sub serialize ($) {  my @FILES = grep {$_} split /\s+/, qq[
103    my $node = shift;                        ${test_dir_name}tokenizer-test-2.dat
104    my $r = '';                        ${test_dir_name}tokenizer-test-3.dat
105                          ${dir_name}tests1.dat
106    my @node = map { [$_, ''] } @{$node->child_nodes};                        ${dir_name}tests2.dat
107    while (@node) {                        ${dir_name}tests3.dat
108      my $child = shift @node;                        ${dir_name}tests4.dat
109      my $nt = $child->[0]->node_type;                        ${dir_name}tests5.dat
110      if ($nt == $child->[0]->ELEMENT_NODE) {                        ${dir_name}tests6.dat
111        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?                        ${dir_name}tests7.dat
112                          ${dir_name}tests8.dat
113        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }                        ${dir_name}tests9.dat
114                      @{$child->[0]->attributes}) {                        ${dir_name}tests10.dat
115          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?                        ${dir_name}tests11.dat
116          $r .= $attr->[1] . '"' . "\x0A";                        ${dir_name}tests12.dat
117        }                        ${test_dir_name}tree-test-1.dat
118                                ${test_dir_name}tree-test-2.dat
119        unshift @node,                        ${test_dir_name}tree-test-3.dat
120          map { [$_, $child->[1] . '  '] } @{$child->[0]->child_nodes};                        ${test_dir_name}tree-test-void.dat
121      } elsif ($nt == $child->[0]->TEXT_NODE) {                        ${test_dir_name}tree-test-flow.dat
122        $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";                        ${test_dir_name}tree-test-phrasing.dat
123      } elsif ($nt == $child->[0]->COMMENT_NODE) {                        ${test_dir_name}tree-test-form.dat
124        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";                        ${test_dir_name}tree-test-foreign.dat
125      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {                       ];
126        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";  
127      } else {  require 't/testfiles.pl';
128        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error  execute_test ($_, {
129      }    errors => {is_list => 1},
130    }    shoulds => {is_list => 1},
131        document => {is_prefixed => 1},
132    return $r;    'document-fragment' => {is_prefixed => 1},
133  } # serialize  }, \&test) for @FILES;
134    
135  ## License: Public Domain.  ## License: Public Domain.
136  ## $Date$  ## $Date$

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.45

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24