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