/[suikacvs]/markup/html/whatpm/t/ContentChecker.t
Suika

Contents of /markup/html/whatpm/t/ContentChecker.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations) (download) (as text)
Sat Jun 23 02:59:48 2007 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.11: +7 -6 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	23 Jun 2007 02:59:40 -0000
	* ContentChecker.t: Use NanoDOM-based manakai for XHTML.
	Note that a test fails that assumes XML attribute value
	normalization fails since XMLParserTemp.pm does
	not normalize white space in attribute value when
	creating an attribute node (and new NanoDOM-based
	manakai's Attr.value does not normalize white space
	for more Web compatibility).

2007-06-23  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4     use Test;
5 wakaba 1.11 BEGIN { plan tests => 873 }
6 wakaba 1.1
7 wakaba 1.5 my @FILES = qw[
8     t/content-model-1.dat
9     t/content-model-2.dat
10     t/content-model-3.dat
11     ];
12 wakaba 1.1
13     require Whatpm::ContentChecker;
14    
15     ## ISSUE: Currently we require manakai XML parser to test arbitrary XML tree.
16     use lib qw[/home/wakaba/work/manakai/lib];
17 wakaba 1.12 require Message::DOM::DOMImplementation;
18     require Message::DOM::XMLParserTemp;
19 wakaba 1.4 require Whatpm::HTML;
20     require Whatpm::NanoDOM;
21 wakaba 1.1
22 wakaba 1.12 my $dom = Message::DOM::DOMImplementation->____new;
23 wakaba 1.1
24     for my $file_name (@FILES) {
25     open my $file, '<', $file_name or die "$0: $file_name: $!";
26    
27     my $test;
28     my $mode = 'data';
29     while (<$file>) {
30     s/\x0D\x0A/\x0A/;
31     if (/^#data$/) {
32     undef $test;
33     $test->{data} = '';
34     $mode = 'data';
35 wakaba 1.4 $test->{parse_as} = 'xml';
36     } elsif (/^#data html$/) {
37     undef $test;
38     $test->{data} = '';
39     $mode = 'data';
40     $test->{parse_as} = 'html';
41 wakaba 1.1 } elsif (/^#errors$/) {
42     $test->{errors} = [];
43     $mode = 'errors';
44     $test->{data} =~ s/\x0D?\x0A\z//;
45     } elsif (defined $test->{errors} and /^$/) {
46     test ($test);
47     undef $test;
48     } else {
49     if ($mode eq 'data') {
50     $test->{$mode} .= $_;
51     } elsif ($mode eq 'errors') {
52     tr/\x0D\x0A//d;
53     push @{$test->{errors}}, $_;
54     }
55     }
56     }
57     } # @FILES
58    
59     sub test ($) {
60     my $test = shift;
61    
62 wakaba 1.4 my $doc;
63     if ($test->{parse_as} eq 'xml') {
64 wakaba 1.12 open my $fh, '<', \($test->{data});
65     $doc = Message::DOM::XMLParserTemp->parse_byte_stream
66     ($fh => $dom, sub { }, charset => 'utf-8');
67 wakaba 1.4 ## NOTE: There should be no well-formedness error; if there is,
68     ## then it is an error of the test case itself.
69     } else {
70     $doc = Whatpm::NanoDOM::Document->new;
71     Whatpm::HTML->parse_string ($test->{data} => $doc);
72     }
73 wakaba 1.1
74     my @error;
75 wakaba 1.10 Whatpm::ContentChecker->check_element
76 wakaba 1.1 ($doc->document_element, sub {
77     my %opt = @_;
78     push @error, get_node_path ($opt{node}) . ';' . $opt{type};
79     });
80    
81     ok join ("\n", sort {$a cmp $b} @error),
82     join ("\n", sort {$a cmp $b} @{$test->{errors}}), $test->{data};
83     } # test
84    
85     sub get_node_path ($) {
86     my $node = shift;
87     my @r;
88     while (defined $node) {
89     my $rs;
90     if ($node->node_type == 1) {
91     $rs = $node->manakai_local_name;
92 wakaba 1.4 $node = $node->parent_node;
93 wakaba 1.3 } elsif ($node->node_type == 2) {
94     $rs = '@' . $node->manakai_local_name;
95 wakaba 1.4 $node = $node->owner_element;
96 wakaba 1.1 } elsif ($node->node_type == 3) {
97     $rs = '"' . $node->data . '"';
98 wakaba 1.4 $node = $node->parent_node;
99 wakaba 1.1 } elsif ($node->node_type == 9) {
100     $rs = '';
101 wakaba 1.4 $node = $node->parent_node;
102 wakaba 1.1 } else {
103     $rs = '#' . $node->node_type;
104 wakaba 1.4 $node = $node->parent_node;
105 wakaba 1.1 }
106     unshift @r, $rs;
107     }
108     return join '/', @r;
109     } # get_node_path
110    
111     ## License: Public Domain.
112 wakaba 1.12 ## $Date: 2007/06/05 00:56:43 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24