/[suikacvs]/markup/html/whatpm/t/HTML-tokenizer.t
Suika

Contents of /markup/html/whatpm/t/HTML-tokenizer.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations) (download) (as text)
Mon Mar 3 10:20:19 2008 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.26: +18 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	3 Mar 2008 10:20:07 -0000
	* HTML-tokenizer.t: Support for test coverage caluclation.

2008-03-03  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	3 Mar 2008 10:19:33 -0000
	* HTML.pm.src: Checkpoints for debugging are added.

	* mkhtmlparser.pl: Support for |!!!cp| syntax.

2008-03-03  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4 wakaba 1.27 my $DEBUG = $ENV{DEBUG};
5    
6 wakaba 1.2 my $dir_name;
7 wakaba 1.4 my $test_dir_name;
8 wakaba 1.1 BEGIN {
9 wakaba 1.4 $test_dir_name = 't/';
10 wakaba 1.2 $dir_name = 't/tokenizer/';
11 wakaba 1.1 my $skip = "You don't have JSON module";
12     eval q{
13 wakaba 1.15 use JSON 1.07;
14 wakaba 1.1 $skip = "You don't have make command";
15 wakaba 1.3 system ("cd $test_dir_name; make tokenizer-files") == 0 or die
16 wakaba 1.2 unless -f $dir_name.'test1.test';
17 wakaba 1.1 $skip = '';
18     };
19     if ($skip) {
20     print "1..1\n";
21     print "ok 1 # $skip\n";
22     exit;
23     }
24     $JSON::UnMapping = 1;
25 wakaba 1.2 $JSON::UTF8 = 1;
26 wakaba 1.1 }
27    
28     use Test;
29 wakaba 1.24 BEGIN { plan tests => 420 }
30 wakaba 1.2
31 wakaba 1.1 use Data::Dumper;
32 wakaba 1.2 $Data::Dumper::Useqq = 1;
33     sub Data::Dumper::qquote {
34     my $s = shift;
35     $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
36     return q<qq'> . $s . q<'>;
37     } # Data::Dumper::qquote
38 wakaba 1.1
39 wakaba 1.27 if ($DEBUG) {
40     my $not_found = {%$Whatpm::HTML::Debug::cp};
41    
42     $Whatpm::HTML::Debug::cp_pass = sub {
43     my $id = shift;
44     delete $not_found->{$id};
45     };
46    
47     END {
48     for my $id (sort {$a <=> $b || $a cmp $b} keys %$not_found) {
49     print "# checkpoint $id is not passed\n";
50     }
51     }
52     }
53    
54 wakaba 1.5 use Whatpm::HTML;
55 wakaba 1.1
56 wakaba 1.4 for my $file_name (grep {$_} split /\s+/, qq[
57     ${dir_name}test1.test
58     ${dir_name}test2.test
59 wakaba 1.15 ${dir_name}test3.test
60     ${dir_name}test4.test
61 wakaba 1.4 ${dir_name}contentModelFlags.test
62 wakaba 1.8 ${dir_name}escapeFlag.test
63 wakaba 1.4 ${test_dir_name}tokenizer-test-1.test
64 wakaba 1.1 ]) {
65 wakaba 1.4 open my $file, '<', $file_name
66     or die "$0: $file_name: $!";
67 wakaba 1.1 local $/ = undef;
68     my $js = <$file>;
69     close $file;
70 wakaba 1.9
71     print "# $file_name\n";
72 wakaba 1.15 $js =~ s{\\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
73     \\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])}{
74     ## NOTE: JSON::Parser does not decode surrogate pair escapes
75     ## NOTE: In older version of JSON::Parser, utf8 string will be broken
76     ## by parsing. Use latest version!
77     ## NOTE: Encode.pm is broken; it converts e.g. U+10FFFF to U+FFFD.
78     my $c = 0x10000;
79     $c += ((((hex $1) & 0b1111111111) << 10) | ((hex $2) & 0b1111111111));
80     chr $c;
81     }gex;
82 wakaba 1.1 my $tests = jsonToObj ($js)->{tests};
83     TEST: for my $test (@$tests) {
84     my $s = $test->{input};
85    
86     my $j = 1;
87     while ($j < @{$test->{output}}) {
88     if (ref $test->{output}->[$j - 1] and
89     $test->{output}->[$j - 1]->[0] eq 'Character' and
90     ref $test->{output}->[$j] and
91     $test->{output}->[$j]->[0] eq 'Character') {
92     $test->{output}->[$j - 1]->[1]
93     .= $test->{output}->[$j]->[1];
94     splice @{$test->{output}}, $j, 1;
95     }
96     $j++;
97     }
98    
99 wakaba 1.2 my @cm = @{$test->{contentModelFlags} || ['PCDATA']};
100     my $last_start_tag = $test->{lastStartTag};
101 wakaba 1.1 for my $cm (@cm) {
102 wakaba 1.5 my $p = Whatpm::HTML->new;
103 wakaba 1.1 my $i = 0;
104 wakaba 1.9 my @token;
105 wakaba 1.26 $p->{set_next_char} = sub {
106 wakaba 1.1 my $self = shift;
107 wakaba 1.10
108 wakaba 1.26 pop @{$self->{prev_char}};
109     unshift @{$self->{prev_char}}, $self->{next_char};
110 wakaba 1.10
111 wakaba 1.26 $self->{next_char} = -1 and return if $i >= length $s;
112     $self->{next_char} = ord substr $s, $i++, 1;
113 wakaba 1.2
114 wakaba 1.26 if ($self->{next_char} == 0x000D) { # CR
115 wakaba 1.15 $i++ if substr ($s, $i, 1) eq "\x0A";
116 wakaba 1.26 $self->{next_char} = 0x000A; # LF # MUST
117     } elsif ($self->{next_char} > 0x10FFFF) {
118     $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
119 wakaba 1.9 push @token, 'ParseError';
120 wakaba 1.26 } elsif ($self->{next_char} == 0x0000) { # NULL
121     $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
122 wakaba 1.9 push @token, 'ParseError';
123 wakaba 1.2 }
124 wakaba 1.1 };
125 wakaba 1.26 $p->{prev_char} = [-1, -1, -1];
126     $p->{next_char} = -1;
127 wakaba 1.1
128     $p->{parse_error} = sub {
129     push @token, 'ParseError';
130     };
131    
132     $p->_initialize_tokenizer;
133 wakaba 1.18 $p->{content_model} = {
134     CDATA => Whatpm::HTML::CDATA_CONTENT_MODEL (),
135     RCDATA => Whatpm::HTML::RCDATA_CONTENT_MODEL (),
136     PCDATA => Whatpm::HTML::PCDATA_CONTENT_MODEL (),
137     PLAINTEXT => Whatpm::HTML::PLAINTEXT_CONTENT_MODEL (),
138     }->{$cm};
139 wakaba 1.2 $p->{last_emitted_start_tag_name} = $last_start_tag;
140 wakaba 1.1
141     while (1) {
142     my $token = $p->_get_next_token;
143 wakaba 1.19 last if $token->{type} == Whatpm::HTML::END_OF_FILE_TOKEN ();
144 wakaba 1.1
145     my $test_token = [
146     {
147 wakaba 1.19 Whatpm::HTML::DOCTYPE_TOKEN () => 'DOCTYPE',
148     Whatpm::HTML::START_TAG_TOKEN () => 'StartTag',
149     Whatpm::HTML::END_TAG_TOKEN () => 'EndTag',
150     Whatpm::HTML::COMMENT_TOKEN () => 'Comment',
151     Whatpm::HTML::CHARACTER_TOKEN () => 'Character',
152 wakaba 1.1 }->{$token->{type}} || $token->{type},
153     ];
154     $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};
155     $test_token->[1] = $token->{data} if defined $token->{data};
156 wakaba 1.19 if ($token->{type} == Whatpm::HTML::START_TAG_TOKEN ()) {
157 wakaba 1.11 $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}};
158 wakaba 1.19 } elsif ($token->{type} == Whatpm::HTML::DOCTYPE_TOKEN ()) {
159 wakaba 1.11 $test_token->[1] = $token->{name};
160     $test_token->[2] = $token->{public_identifier};
161     $test_token->[3] = $token->{system_identifier};
162 wakaba 1.25 $test_token->[4] = $token->{quirks} ? 0 : 1;
163 wakaba 1.11 }
164    
165 wakaba 1.1 if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and
166     $test_token->[0] eq 'Character') {
167     $token[-1]->[1] .= $test_token->[1];
168     } else {
169     push @token, $test_token;
170     }
171     }
172    
173     my $expected_dump = Dumper ($test->{output});
174     my $parser_dump = Dumper (\@token);
175 wakaba 1.2 ok $parser_dump, $expected_dump,
176 wakaba 1.17 $test->{description} . ': ' . Data::Dumper::qquote ($test->{input});
177 wakaba 1.1 }
178     }
179     }
180    
181 wakaba 1.19 ## License: Public Domain.
182 wakaba 1.27 ## $Date: 2008/03/03 09:17:10 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24