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