This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Encode 1.01, from Dan Kogai.
[perl5.git] / ext / Encode / t / TW.t
1 BEGIN {
2     if ($ENV{'PERL_CORE'}){
3         chdir 't';
4         unshift @INC, '../lib';
5     }
6     require Config; import Config;
7     if ($Config{'extensions'} !~ /\bEncode\b/) {
8       print "1..0 # Skip: Encode was not built\n";
9       exit 0;
10     }
11     unless (find PerlIO::Layer 'perlio') {
12         print "1..0 # Skip: PerlIO was not built\n";
13         exit 0;
14     }
15     if (ord("A") == 193) {
16         print "1..0 # Skip: EBCDIC\n";
17         exit 0;
18     }
19     $| = 1;
20 }
21
22 use strict;
23 use Test::More tests => 17;
24 use Encode;
25
26 use_ok('Encode::TW');
27
28 # Since JP.t already test basic file IO, we will just focus on
29 # internal encode / decode test here. Unfortunately, to test
30 # against all the UniHan characters will take a huge disk space,
31 # not to mention the time it will take, and the fact that Perl
32 # did not bundle UniHan.txt anyway.
33
34 # So, here we just test a typical snippet spanning multiple Unicode
35 # blocks, and hope it can point out obvious errors.
36
37 run_tests('Basic Big5 range', {
38     'utf'       => (
39 24093.39640.38525.20043.33495.35028.20846.65292.
40 26389.30343.32771.26352.20271.24248.65108.
41 25885.25552.35998.20110.23391.38508.20846.65292.
42 24799.24218.23493.21566.20197.38477.65108
43     ),
44
45     'big5'      => (join('',
46 '«Ò°ª¶§¤§­]¸Ç¤¼¡A®Ó¬Ó¦Ò¤ê§B±e¡Q',
47 'Äá´£­s¤_©s³µ¤¼¡A±©©°±G§^¥H­°¡Q',
48     )),
49
50     'big5-hkscs'=> (join('',
51 '«Ò°ª¶§¤§­]¸Ç¤¼¡A®Ó¬Ó¦Ò¤ê§B±e¡Q',
52 'Äá´£­s¤_©s³µ¤¼¡A±©©°±G§^¥H­°¡Q',
53     )),
54
55     'cp950'     => (join('',
56 '«Ò°ª¶§¤§­]¸Ç¤¼¡A®Ó¬Ó¦Ò¤ê§B±e¡Q',
57 'Äá´£­s¤_©s³µ¤¼¡A±©©°±G§^¥H­°¡Q',
58     )),
59 });
60
61 run_tests('Hong Kong Extensions', {
62     'utf'       => (
63 24863.35613.25152.26377.20351.29992.32.80.101.114.108.32.
64 22021.26379.21451.65292.32102.25105.21707.22021.
65 25903.25345.12289.24847.35211.21644.40723.21237.
66 22914.26524.32232.30908.26377.20219.20309.37679.28431.
67 65292.35531.21578.35380.25105.21707.12290
68     ),
69
70     'big5-hkscs'        => join('',
71 '·PÁ©Ҧ³¨Ï¥Î Perl \9dïªB¤Í¡Aµ¹§Ú\92]\9dï¤ä«ù¡B·N¨£©M¹ªÀy',
72 '¦pªG½s½X¦³¥ô¦ó¿ùº|¡A½Ð§i¶D§Ú\92]¡C'
73     ),
74 });
75
76 sub run_tests {
77     my ($title, $tests) = @_;
78     my $utf = delete $tests->{'utf'};
79
80     # $enc = encoding, $str = content
81     foreach my $enc (sort keys %{$tests}) {
82         my $str = $tests->{$enc};
83
84         is(Encode::decode($enc, $str), $utf, "[$enc] decode - $title");
85         is(Encode::encode($enc, $utf), $str, "[$enc] encode - $title");
86
87         my $str2 = $str;
88         my $utf8 = Encode::encode('utf-8', $utf);
89
90         Encode::from_to($str2, $enc, 'utf-8');
91         is($str2, $utf8, "[$enc] from_to => utf8 - $title");
92
93         Encode::from_to($utf8, 'utf-8', $enc); # convert $utf8 as $enc
94         is($utf8, $str,  "[$enc] utf8 => from_to - $title");
95     }
96 }