Preliminary fix of the CGI.pm problem of always assuming that everything is
[koha.git] / t / Charset.t
1 use strict;
2 use C4::Charset;
3
4 use vars qw( @tests );
5 use vars qw( $loaded );
6
7 BEGIN {
8    @tests = (
9    [
10       'Normal HTML without meta tag',
11       sub { C4::Charset::guesscharset($_[0]) },
12       undef,
13       <<EOF
14 <title>control case</title>
15 EOF
16    ], [
17       'Result of guesscharset with normal HTML with irrelevant meta tag',
18       sub { C4::Charset::guesscharset($_[0]) },
19       undef,
20       <<EOF
21 <meta http-equiv="Content-Language" content="zh-TW">
22 EOF
23    ], [
24       'Result of guesscharset with normal HTML with irrelevant meta tag',
25       sub { C4::Charset::guesstype($_[0]) },
26       undef,
27       <<EOF
28 <meta http-equiv="Content-Language" content="zh-TW">
29 EOF
30    ], [
31       'Result of guesscharset with normal HTML with relevant meta tag',
32       sub { C4::Charset::guesscharset($_[0]) },
33       'big5',
34       <<EOF
35 <meta http-equiv="Content-Type" content="text/html; charset=big5">
36 EOF
37    ], [
38       'Result of guesstype with normal HTML with relevant meta tag',
39       sub { C4::Charset::guesstype($_[0]) },
40       'text/html; charset=big5',
41       <<EOF
42 <meta http-equiv="Content-Type" content="text/html; charset=big5">
43 EOF
44    ], [
45       'Variant 1 using single quotes',
46       sub { C4::Charset::guesstype($_[0]) },
47       'text/html; charset=iso-2022-jp',
48       <<EOF
49 <meta http-equiv="Content-Type" content='text/html; charset=iso-2022-jp'>
50 EOF
51    ], [
52       'Variant 2 using single quotes',
53       sub { C4::Charset::guesstype($_[0]) },
54       'text/html; charset=utf-8',
55       <<EOF
56 <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
57 EOF
58    ], [
59       'Unquoted Content-Type',
60       sub { C4::Charset::guesstype($_[0]) },
61       'text/html; charset=big5',
62       <<EOF
63 <meta http-equiv=Content-Type content="text/html; charset=big5">
64 EOF
65    ], [
66       'XML syntax',
67       sub { C4::Charset::guesstype($_[0]) },
68       'text/html; charset=iso-8859-2',
69       <<EOF
70 <meta http-equiv=Content-Type content="text/html; charset=iso-8859-2" />
71 EOF
72    ], [
73       'Expected attributes in reverse order',
74       sub { C4::Charset::guesstype($_[0]) },
75       'text/html; charset=big5',
76       <<EOF
77 <meta content="text/html; charset=big5" http-equiv="Content-Type">
78 EOF
79    ], [
80       'Extra whitespace at end',
81       sub { C4::Charset::guesstype($_[0]) },
82       'text/html; charset=big5',
83       <<EOF
84 <meta http-equiv="Content-Type" content="text/html; charset=big5"   >
85 EOF
86    ], [
87       'Multiple lines',
88       sub { C4::Charset::guesstype($_[0]) },
89       'text/html; charset=big5',
90       <<EOF
91 <meta
92 http-equiv="Content-Type"
93 content="text/html; charset=big5"
94 >
95 EOF
96    ], [
97       'With surrounding HTML',
98       sub { C4::Charset::guesstype($_[0]) },
99       'text/html; charset=us-ascii',
100       <<EOF
101 <html>
102 <head>
103 <title>Test case with surrounding HTML</title>
104 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
105 </head>
106 <body>
107 The return value should not be contaiminated with any surround HTML
108 FIXME: Auth.pm returns in code that can contaminate the charset
109 FIXME: if we do not explicitly disallow whitespace in the charset
110 </body>
111 </html>
112 EOF
113    ],
114 );
115 }
116
117 BEGIN { $| = 1; printf "1..%d\n", scalar(@tests); }
118 END {print "not ok 1\n" unless $loaded;}
119 $loaded = 1;
120
121
122 # Run all tests in sequence
123 for (my $i = 1; $i <= scalar @tests; $i += 1) {
124    my $test = $tests[$i - 1];
125    my($title, $f, $expected, $input) = @$test;
126    die "not ok $i (malformed test case)\n"
127       unless @$test == 4 && ref $f eq 'CODE';
128
129    my $output = &$f($input);
130    if (
131          (!defined $output && !defined $expected)
132       || (defined $output && defined $expected && $output eq $expected)
133    ) {
134       print "ok $i ($title)\n";
135    } else {
136       print "not ok $i ($title: got ",
137             (defined $output? "\"$output\"": 'undef'),
138             ', expected ',
139             (defined $expected? "\"$expected\"": 'undef'),
140             ")\n";
141    }
142 }
143
144
145
146
147