more language improvements
[koha.git] / t / Charset.t
1 use strict;
2 use C4::Interface::CGI::Output;
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 { 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 { guesscharset($_[0]) },
19       undef,
20       <<EOF
21 <meta http-equiv="Content-Language" content="zh-TW">
22 EOF
23    ], [
24       'Result of guesstype with normal HTML with irrelevant meta tag',
25       sub { guesstype($_[0]) },
26       'text/html',
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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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       # FIXME - THIS IS NOT A WELL-WRITTEN TEST CASE!!!
98       'With surrounding HTML',
99       sub { guesstype($_[0]) },
100       'text/html; charset=us-ascii',
101       <<EOF
102 <html>
103 <head>
104 <title>Test case with surrounding HTML</title>
105 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
106 </head>
107 <body>
108 The return value should not be contaiminated with any surround HTML
109 FIXME: Auth.pm returns in code that can contaminate the charset
110 FIXME: if we do not explicitly disallow whitespace in the charset
111 </body>
112 </html>
113 EOF
114    ],
115 );
116 }
117
118 BEGIN { $| = 1; printf "1..%d\n", scalar(@tests); }
119 END {print "not ok 1\n" unless $loaded;}
120 $loaded = 1;
121
122
123 # Run all tests in sequence
124 for (my $i = 1; $i <= scalar @tests; $i += 1) {
125    my $test = $tests[$i - 1];
126    my($title, $f, $expected, $input) = @$test;
127    die "not ok $i (malformed test case)\n"
128       unless @$test == 4 && ref $f eq 'CODE';
129
130    my $output = &$f($input);
131    if (
132          (!defined $output && !defined $expected)
133       || (defined $output && defined $expected && $output eq $expected)
134    ) {
135       print "ok $i - $title\n";
136    } else {
137       print "not ok $i - $title: got ",
138             (defined $output? "\"$output\"": 'undef'),
139             ', expected ',
140             (defined $expected? "\"$expected\"": 'undef'),
141             "\n";
142    }
143 }
144
145
146
147
148