/[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.4 - (show annotations) (download) (as text)
Tue May 1 06:22:12 2007 UTC (19 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +11 -9 lines
File MIME type: application/x-troff
++ whatpm/What/ChangeLog	1 May 2007 06:20:06 -0000
2007-05-01  Wakaba  <wakaba@suika.fam.cx>

	* NanoDOM.pm (last_child, previous_sibling): New attributes.
	(clone_node): Attribute nodes were not completely copied.

	* HTML.pm.src: Many bugs are fixed.

++ whatpm/t/ChangeLog	1 May 2007 06:21:52 -0000
2007-05-01  Wakaba  <wakaba@suika.fam.cx>

	* HTML-tree.t: New test file is added.  Sort key
	was incorrect.

	* HTML-tokenizer.t: New test file is added.

	* tokenizer-test-1.test, tree-test-1.dat: New tests.

1 #!/usr/bin/perl
2 use strict;
3
4 my $dir_name;
5 my $test_dir_name;
6 BEGIN {
7 $test_dir_name = 't/';
8 $dir_name = 't/tokenizer/';
9 my $skip = "You don't have JSON module";
10 eval q{
11 use JSON 1.00;
12 $skip = "You don't have make command";
13 system ("cd $test_dir_name; make tokenizer-files") == 0 or die
14 unless -f $dir_name.'test1.test';
15 $skip = '';
16 };
17 if ($skip) {
18 print "1..1\n";
19 print "ok 1 # $skip\n";
20 exit;
21 }
22 $JSON::UnMapping = 1;
23 $JSON::UTF8 = 1;
24 }
25
26 use Test;
27 BEGIN { plan tests => 70 }
28
29 use Data::Dumper;
30 $Data::Dumper::Useqq = 1;
31 sub Data::Dumper::qquote {
32 my $s = shift;
33 $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
34 return q<qq'> . $s . q<'>;
35 } # Data::Dumper::qquote
36
37 use What::HTML;
38
39 for my $file_name (grep {$_} split /\s+/, qq[
40 ${dir_name}test1.test
41 ${dir_name}test2.test
42 ${dir_name}contentModelFlags.test
43 ${test_dir_name}tokenizer-test-1.test
44 ]) {
45 open my $file, '<', $file_name
46 or die "$0: $file_name: $!";
47 local $/ = undef;
48 my $js = <$file>;
49 close $file;
50
51 my $tests = jsonToObj ($js)->{tests};
52 TEST: for my $test (@$tests) {
53 my $s = $test->{input};
54
55 my $j = 1;
56 while ($j < @{$test->{output}}) {
57 if (ref $test->{output}->[$j - 1] and
58 $test->{output}->[$j - 1]->[0] eq 'Character' and
59 ref $test->{output}->[$j] and
60 $test->{output}->[$j]->[0] eq 'Character') {
61 $test->{output}->[$j - 1]->[1]
62 .= $test->{output}->[$j]->[1];
63 splice @{$test->{output}}, $j, 1;
64 }
65 $j++;
66 }
67
68 my @cm = @{$test->{contentModelFlags} || ['PCDATA']};
69 my $last_start_tag = $test->{lastStartTag};
70 for my $cm (@cm) {
71 my $p = What::HTML->new;
72 my $i = 0;
73 $p->{set_next_input_character} = sub {
74 my $self = shift;
75 $self->{next_input_character} = -1 and return if $i >= length $s;
76 $self->{next_input_character} = ord substr $s, $i++, 1;
77
78 if ($self->{next_input_character} == 0x000D) { # CR
79 if ($i >= length $s) {
80 #
81 } else {
82 my $next_char = ord substr $s, $i++, 1;
83 if ($next_char == 0x000A) { # LF
84 #
85 } else {
86 push @{$self->{char}}, $next_char;
87 }
88 }
89 $self->{next_input_character} = 0x000A; # LF # MUST
90 } elsif ($self->{next_input_character} > 0x10FFFF) {
91 $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
92 } elsif ($self->{next_input_character} == 0x0000) { # NULL
93 $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
94 }
95 };
96
97 my @token;
98 $p->{parse_error} = sub {
99 push @token, 'ParseError';
100 };
101
102 $p->_initialize_tokenizer;
103 $p->{content_model_flag} = $cm;
104 $p->{last_emitted_start_tag_name} = $last_start_tag;
105
106 while (1) {
107 my $token = $p->_get_next_token;
108 last if $token->{type} eq 'end-of-file';
109
110 my $test_token = [
111 {
112 DOCTYPE => 'DOCTYPE',
113 'start tag' => 'StartTag',
114 'end tag' => 'EndTag',
115 comment => 'Comment',
116 character => 'Character',
117 }->{$token->{type}} || $token->{type},
118 ];
119 $test_token->[1] = $token->{name} if defined $token->{name};
120 $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};
121 $test_token->[1] = $token->{data} if defined $token->{data};
122 $test_token->[2] = $token->{error} ? 1 : 0 if $token->{type} eq 'DOCTYPE';
123 $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}}
124 if $token->{type} eq 'start tag';
125
126 if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and
127 $test_token->[0] eq 'Character') {
128 $token[-1]->[1] .= $test_token->[1];
129 } else {
130 push @token, $test_token;
131 }
132 }
133
134 my $expected_dump = Dumper ($test->{output});
135 my $parser_dump = Dumper (\@token);
136 ok $parser_dump, $expected_dump,
137 $test->{description} . ': ' . $test->{input};
138 }
139 }
140 }
141
142 ## $Date: 2007/04/30 14:12:02 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24