File: 99html5lib.t

package info (click to toggle)
libhtml-html5-parser-perl 0.992-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,444 kB
  • sloc: perl: 16,129; makefile: 2; sh: 1
file content (369 lines) | stat: -rw-r--r-- 7,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
## skip Test::Tabs
use Test::More;
use HTML::HTML5::Parser;

BEGIN {
	eval { require Moo; 1 } or plan skip_all => 'Need Moo!'
};

{
	package XML::LibXML::Document;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		my $return;
		
		my $element = $parser->dtd_element($self);
		my $public  = $parser->dtd_public_id($self) || '';
		my $system  = $parser->dtd_system_id($self) || '';
		
		if (defined $element)
		{
			$return = sprintf(
				"| <!DOCTYPE %s%s%s>\n",
				$element,
				(($public||$system) ? " \"$public\"" : ""),
				(($public||$system) ? " \"$system\"" : ""),
				);
		}
		
		$return .= $_->pythonDebug(q{| }, $parser) foreach $self->childNodes;
		return $return;
	}
}

{
	package XML::LibXML::DocumentFragment;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		$self->normalize;
		
		my $return;
		foreach ($self->childNodes)
		{
			$return .= $_->pythonDebug($indent . q{| }, $parser);
		}		
		return $return;
	}
}

{
	package XML::LibXML::Element;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		$self->normalize;
		
		my $nsbit  = '';
		$nsbit = 'svg ' if $self->namespaceURI =~ /svg/i;
		$nsbit = 'math ' if $self->namespaceURI =~ /math/i;
		my $return = sprintf("%s<%s%s>\n", $indent, $nsbit, $self->localname);
		
		my @attribs = 
			sort { $a->localname cmp $b->localname }
			grep { not $_->isa('XML::LibXML::Namespace') }
			$self->attributes;
		foreach (@attribs)
		{
			$return .= $_->pythonDebug($indent . q{  }, $parser);
		}
		
		if ($self->localname eq 'noscript')
		{
			my $innerHTML = join q{}, map { $_->toString } $self->childNodes;
			$return .= $indent . q{  "} . $innerHTML . "\"\n";
		}
		else
		{
			foreach ($self->childNodes)
			{
				$return .= $_->pythonDebug($indent . q{  }, $parser);
			}
		}
		
		return $return;
	}
}

{
	package XML::LibXML::Text;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		return sprintf("%s\"%s\"\n", $indent, $self->data);
	}
}

{
	package XML::LibXML::Comment;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		return sprintf("%s<!-- %s -->\n", $indent, $self->data);
	}
}

{
	package XML::LibXML::Attr;
	sub pythonDebug
	{
		my $self = shift;
		my ($indent, $parser) = @_;
		$indent = '' unless defined $indent;
		
		return sprintf("%s%s %s=\"%s\"\n", $indent, split(/:/, $self->nodeName), $self->value)
			if $self->namespaceURI && $self->nodeName=~/:/;
		return sprintf("%s%s=\"%s\"\n", $indent, $self->localname, $self->value);
	}
}

{
	package Local::HTML5Lib::Test;
	
	use Moo;
	
	has test_file         => (is => 'rw');
	has test_number       => (is => 'rw');
	has data              => (is => 'rw');
	has errors            => (is => 'rw');
	has document          => (is => 'rw');	
	has document_fragment => (is => 'rw');
	has parser            => (is => 'lazy', builder => '_build_parser');
	
	sub test_id
	{
		my $self = shift;
		if ($self->test_file->filename =~ m{ / ([^/]+) $ }x)
		{
			sprintf('%s:%s', $1, $self->test_number||1);
		}
	}
	
	sub dom
	{
		my ($self) = @_;
		
		if ($self->document_fragment)
		{
			return $self->parser->parse_balanced_chunk(
				$self->data,
				{within => $self->document_fragment},
			);
		}
		
		return eval {
			$self->parser->parse_string($self->data);
		} || do {
			my $e   = $@;
			my $xml = 'XML::LibXML::Document'->new('1.0', 'utf-8');
			$xml->setDocumentElement( $xml->createElementNS('http://www.w3.org/1999/xhtml', 'html') );
			$xml->documentElement->appendText("ERROR: $e");
			$xml;
		}
	}
	
	sub _build_parser
	{
		require HTML::HTML5::Parser;
		'HTML::HTML5::Parser'->new;
	}
	
	sub __uniscape
	{
		my $str = shift;
		eval {
			$str =~ s{ ([^\n\x20-\x7E]) }{ sprintf('\x{%04X}', ord($1)) }gex;
		};
		$str;
	}
	
	sub run
	{
		my ($self) = @_;
		my $expected = $self->document."\n";
		my $got      = $self->dom->pythonDebug(undef, $self->parser);
		utf8::decode($got);
		
		local $Test::Builder::Level = $Test::Builder::Level + 1;
		
		SKIP: {
			my $excuse = $::SKIP->{ $self->test_id };
			Test::More::skip($excuse, 1) if defined $excuse;
			
			if ($got eq $expected)
			{
				Test::More::pass("DATA: ".$self->data);
				return 1;
			}
			else
			{
				Test::More::fail("DATA: ".$self->data);
				Test::More::diag("ID: ".$self->test_id);
				Test::More::diag("GOT:\n" . __uniscape $got);
				Test::More::diag("EXPECTED:\n" . __uniscape $expected);
				return 0;
			}
		}
	}
}
	
{
	package Local::HTML5Lib::TestFile;
	
	use Moo;
	
	has filename   => (is => "rw");
	has tests      => (is => "rw");
	has last_score => (is => "rw");
	
	sub read_file
	{
		my ($class, $filename) = @_;
		
		my $self = $class->new(
			filename  => $filename,
			);
			
		my @tests;
		
		open my $fh, '<', $filename;
		push @tests, (my $current_test = { test_file=>$self });
		my $current_key;
		my @lines = <$fh>; # sometimes we need to peek at the next line;
		while (defined ($_ = shift @lines))
		{
			no warnings;
			
			if (!/\S/ and (!defined $lines[0] or $lines[0]=~ /^\#data/))
			{
				$current_test->{test_number} = @tests;
				chomp $current_test->{$current_key} if defined $current_key;
				$current_test = { test_file=>$self };
				$current_key  = undef;
				push @tests, $current_test;
				next;
			}
			
			if (/^\#(.+)/)
			{
				chomp $current_test->{$current_key} if defined $current_key;
				($current_key = $1) =~ s/-/_/g;
				next;
			}
		
			$current_test->{$current_key} .= $_;
		}

		chomp $current_test->{$current_key};
		
		$self->tests([ map {
			utf8::decode($_->{document});
			utf8::decode($_->{data});
			Local::HTML5Lib::Test->new(%$_);
			} @tests]);
		return $self;
	}

	sub run
	{
		local $Test::Builder::Level = $Test::Builder::Level + 1;

		my $self = shift;
		$self->{last_score} = 0;
		Test::More::subtest(
			sprintf("Test file: %s", $self->filename),
			sub {	$self->{last_score} += ($_->run ? 1 : 0) for @{ $self->tests } },
			);
	}
}

package main;

our $SKIP = {
	'tests26.dat:10'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad attribute name)',
	'webkit01.dat:14'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad element name)',
	'webkit01.dat:42'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad attribute name)',
	'webkit02.dat:4'
		=> 'I basically just disagree with this test.',
	'html5test-com.dat:1'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad element name)',
	'html5test-com.dat:2'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad attribute name)',
	'html5test-com.dat:4'
		=> 'requires HTML parser to construct a DOM tree which is illegal in libxml (bad attribute name)',
	};

my @fails;
my @passes;

unless (@ARGV)
{
	@ARGV = <t/html5lib-pass/*.dat>;
}

plan tests => scalar(@ARGV);

while (my $f = shift)
{
	my $F = Local::HTML5Lib::TestFile->read_file($f);
	if ($F->run)
	{
		push @passes, $F;
	}
	else
	{
		push @fails, $F;
	}
}

if (@fails)
{
	diag "FAILED:";
	diag sprintf("  %s [%d/%d]", $_->filename, $_->last_score, scalar(@{$_->tests}))
		for @fails;
}

if (@passes)
{
	diag "PASSED:";
	diag sprintf("  %s [%d/%d]", $_->filename, $_->last_score, scalar(@{$_->tests}))
		for @passes;
}

=head1 PURPOSE

Tests from html5lib's testdata/tree-construction.

=head1 SEE ALSO

L<http://code.google.com/p/html5lib/source/browse/testdata/tree-construction>.

=head1 AUTHOR

Toby Inkster, E<lt>tobyink@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENCE

Copyright (C) 2012 by Toby Inkster

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.