Upgrade to Encode 1.50, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / JP.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     if (ord("A") == 193) {
12         print "1..0 # Skip: EBCDIC\n";
13         exit 0;
14     }
15     $| = 1;
16 }
17 use strict;
18 use Test::More tests => 37;
19 #use Test::More qw(no_plan);
20 use Encode;
21 use File::Basename;
22 use File::Spec;
23 use File::Compare;
24 require_ok "Encode::JP";
25
26 my ($src, $uni, $dst, $txt, $euc, $utf, $ref, $rnd);
27
28 ok(defined(my $enc = find_encoding('euc-jp')), 'find_encoding');
29 ok($enc->isa('Encode::XS'), 'ISA');
30 is($enc->name,'euc-jp',     '$enc->name');
31 my $dir = dirname(__FILE__);
32
33 for my $charset (qw(jisx0201 jisx0212 jisx0208)){
34     $euc = File::Spec->catfile($dir,"$charset.euc");
35     $utf = File::Spec->catfile($dir,"$$.utf8");
36     $ref = File::Spec->catfile($dir,"$charset.ref");
37     $rnd = File::Spec->catfile($dir,"$$.rnd");
38
39     open($src,"<",$euc) or die "Cannot open $euc:$!";
40     binmode($src);
41     $txt = join('',<$src>);
42     close($src);
43     
44     eval{ $uni = $enc->decode($txt, 1) }; 
45     $@ and print $@;
46     ok(defined($uni),  "decode $charset");
47     is(length($txt),0, "decode $charset completely");
48
49     open($dst,">:utf8",$utf) or die "Cannot open $utf:$!";
50     binmode($dst);
51     print $dst $uni;
52     close($dst); 
53     is(compare($utf, $ref), 0, "$utf eq $ref");
54     
55     open $src, "<:utf8", $ref or die "$ref : $!";
56     $uni = join('', <$src>);
57     close $src;
58
59     for my $canon (qw(euc-jp shiftjis
60                       7bit-jis iso-2022-jp iso-2022-jp-1)){
61         my $test = \&is;
62         if   ($charset eq 'jisx0201'){
63             $canon eq 'iso-2022-jp'   and $test = \&isnt;
64             $canon eq 'iso-2022-jp-1' and $test = \&isnt;
65         }elsif($charset eq 'jisx0212'){
66             $canon eq 'shiftjis'    and   $test = \&isnt;
67             $canon eq 'iso-2022-jp' and   $test = \&isnt;
68         }
69         my $rt = ($test eq \&is) ? 'RT' : 'non-RT';
70         $test->($uni, decode($canon, encode($canon, $uni)), 
71               "$rt $charset $canon");
72         
73      }
74
75     eval{ $txt = $enc->encode($uni,1) };    
76     $@ and print $@;
77     ok(defined($txt),   "encode $charset");
78     is(length($uni), 0, "encode $charset completely");
79
80     open($dst,">", $rnd) or die "Cannot open $utf:$!";
81     binmode($dst);
82     print $dst $txt;
83     close($dst); 
84     is(compare($euc, $rnd), 0 => "$rnd eq $euc");
85 }
86
87 END {
88  1 while unlink($utf,$rnd);
89 }