/[suikacvs]/markup/html/whatpm/t/CSS-Tokenizer.t
Suika

Contents of /markup/html/whatpm/t/CSS-Tokenizer.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download) (as text)
Sat Sep 8 05:57:05 2007 UTC (18 years, 8 months ago) by wakaba
Branch: MAIN
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	8 Sep 2007 05:56:49 -0000
2007-09-08  Wakaba  <wakaba@suika.fam.cx>

	* css-token-1.test, CSS-Tokenizer.t: New files.

1 #!/usr/bin/perl
2 use strict;
3
4 my $test_dir_name = 't/';
5 my $dir_name = 't/tokenizer/';
6
7 use JSON 1.07;
8 $JSON::UnMapping = 1;
9 $JSON::UTF8 = 1;
10
11 use Test;
12 BEGIN { plan tests => 347 }
13
14 use Data::Dumper;
15 $Data::Dumper::Useqq = 1;
16 sub Data::Dumper::qquote {
17 my $s = shift;
18 $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
19 return q<qq'> . $s . q<'>;
20 } # Data::Dumper::qquote
21
22 use Whatpm::CSS::Tokenizer;
23
24 for my $file_name (grep {$_} split /\s+/, qq[
25 ${test_dir_name}css-token-1.test
26 ]) {
27 open my $file, '<', $file_name
28 or die "$0: $file_name: $!";
29 local $/ = undef;
30 my $js = <$file>;
31 close $file;
32
33 print "# $file_name\n";
34 $js =~ s{\\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
35 \\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])}{
36 ## NOTE: JSON::Parser does not decode surrogate pair escapes
37 ## NOTE: In older version of JSON::Parser, utf8 string will be broken
38 ## by parsing. Use latest version!
39 ## NOTE: Encode.pm is broken; it converts e.g. U+10FFFF to U+FFFD.
40 my $c = 0x10000;
41 $c += ((((hex $1) & 0b1111111111) << 10) | ((hex $2) & 0b1111111111));
42 chr $c;
43 }gex;
44 my $tests = jsonToObj ($js)->{tests};
45 TEST: for my $test (@$tests) {
46 my $s = $test->{input};
47
48 my $p = Whatpm::CSS::Tokenizer->new;
49
50 my $pos = 0;
51 my $length = length $s;
52 $p->{get_char} = sub {
53 if ($pos < $length) {
54 return ord substr $s, $pos++, 1;
55 } else {
56 return -1;
57 }
58 };
59 $p->init;
60
61 my @token;
62 while (1) {
63 my $token = $p->get_next_token;
64 last if $token->{type} == Whatpm::CSS::Tokenizer::EOF_TOKEN ();
65
66 my $test_token;
67 $test_token->[0] = $Whatpm::CSS::Tokenizer::TokenName[$token->{type}] ||
68 $token->{type};
69 push @$test_token, $token->{number} if defined $token->{number};
70 push @$test_token, $token->{value} if defined $token->{value};
71 push @token, $test_token;
72 }
73
74 my $expected_dump = Dumper ($test->{output});
75 my $parser_dump = Dumper (\@token);
76 ok $parser_dump, $expected_dump,
77 $test->{description} . ': ' . Data::Dumper::qquote ($test->{input});
78 }
79 }
80
81 ## License: Public Domain.
82 ## $Date: 2007/08/25 03:04:24 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24