1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
|
4 |
## Test for Whatpm::CSS::MediaQueryParser and Whatpm::CSS::MediaQuerySerializer |
5 |
|
6 |
use Test; |
7 |
|
8 |
BEGIN { plan tests => 28 } |
9 |
|
10 |
require Whatpm::CSS::MediaQueryParser; |
11 |
require Whatpm::CSS::MediaQuerySerializer; |
12 |
|
13 |
for my $file_name (map {"t/$_"} qw( |
14 |
mq-1.dat |
15 |
)) { |
16 |
print "# $file_name\n"; |
17 |
open my $file, '<:utf8', $file_name or die "$0: $file_name: $!"; |
18 |
|
19 |
my $all_test = {test => []}; |
20 |
my $test; |
21 |
my $mode = 'data'; |
22 |
my $doc_id; |
23 |
my $selectors; |
24 |
while (<$file>) { |
25 |
s/\x0D\x0A/\x0A/; |
26 |
if (/^#data$/) { |
27 |
undef $test; |
28 |
$test->{data} = ''; |
29 |
push @{$all_test->{test}}, $test; |
30 |
$mode = 'data'; |
31 |
} elsif (/#mediatext$/) { |
32 |
$test->{mediatext} = ''; |
33 |
$mode = 'mediatext'; |
34 |
} elsif (/^#errors$/) { |
35 |
$test->{errors} = []; |
36 |
$mode = 'errors'; |
37 |
$test->{data} =~ s/\x0D?\x0A\z//; |
38 |
} elsif (defined $test->{data} and /^$/) { |
39 |
undef $test; |
40 |
} else { |
41 |
if ({data => 1, mediatext => 1}->{$mode}) { |
42 |
$test->{$mode} .= $_; |
43 |
} elsif ($mode eq 'errors') { |
44 |
tr/\x0D\x0A//d; |
45 |
push @{$test->{errors}}, $_; |
46 |
} else { |
47 |
die "Line $.: $_"; |
48 |
} |
49 |
} |
50 |
} |
51 |
|
52 |
my $p = Whatpm::CSS::MediaQueryParser->new; |
53 |
for my $test (@{$all_test->{test}}) { |
54 |
my @actual_error; |
55 |
$p->{onerror} = sub { |
56 |
my (%opt) = @_; |
57 |
my $uri = ${$opt{uri}}; |
58 |
$uri =~ s[^thismessage:/][]; |
59 |
push @actual_error, join ';', |
60 |
$uri, $opt{token}->{line}, $opt{token}->{column}, |
61 |
$opt{level}, |
62 |
$opt{type} . (defined $opt{value} ? ';'.$opt{value} : ''); |
63 |
}; |
64 |
|
65 |
my $mq = $p->parse_char_string ($test->{data}); |
66 |
|
67 |
ok ((join "\n", @actual_error), (join "\n", @{$test->{errors} or []}), |
68 |
"#result ($test->{data})"); |
69 |
|
70 |
if (defined $test->{mediatext}) { |
71 |
my $mt = Whatpm::CSS::MediaQuerySerializer->serialize_media_query ($mq); |
72 |
ok $mt."\n", $test->{mediatext}, "#mediatext ($test->{data})"; |
73 |
} |
74 |
} |
75 |
} |