Upgrade to Encode 0.97, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / JP.t
CommitLineData
0a95303c 1BEGIN {
071db25d 2 if (! -d 'blib' and -d 't'){ chdir 't' };
ee981de6 3 chdir 't' if -d 't' and $ENV{PWD} !~ m,/Encode[^/]*$,o;
4 unshift @INC, '../lib';
0a95303c 5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bEncode\b/) {
7 print "1..0 # Skip: Encode was not built\n";
8 exit 0;
9 }
87e4f210 10 unless (find PerlIO::Layer 'perlio') {
11 print "1..0 # Skip: PerlIO was not built\n";
12 exit 0;
13 }
72f0eb71 14 if (ord("A") == 193) {
15 print "1..0 # Skip: EBCDIC\n";
16 exit 0;
17 }
0a95303c 18 $| = 1;
19}
0e567a6c 20use strict;
ee981de6 21use Test::More tests => 27;
fab31126 22#use Test::More qw(no_plan);
0a95303c 23use Encode;
24use File::Basename;
25use File::Spec;
26use File::Compare;
0e567a6c 27require_ok "Encode::JP";
28
ee981de6 29my ($src, $uni, $dst, $txt, $euc, $utf, $ref, $rnd);
0a95303c 30
31ok(defined(my $enc = find_encoding('euc-jp')));
32ok($enc->isa('Encode::XS'));
33is($enc->name,'euc-jp');
34my $dir = dirname(__FILE__);
ee981de6 35
36my @subcodings = qw(jisx0212 jisx0208);
37
38for my $subcoding (@subcodings){
39 $euc = File::Spec->catfile($dir,"$subcoding.euc");
40 $utf = File::Spec->catfile($dir,"$$.utf8");
41 $ref = File::Spec->catfile($dir,"$subcoding.ref");
42 $rnd = File::Spec->catfile($dir,"$$.rnd");
43 print "# Basic decode test\n";
44 open($src,"<",$euc) || die "Cannot open $euc:$!";
45 binmode($src);
46 ok(defined($src) && fileno($src));
47 $txt = join('',<$src>);
48 open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
49 binmode($dst);
50 ok(defined($dst) && fileno($dst));
51 eval{ $uni = $enc->decode($txt,1) };
52 $@ and print $@;
53 ok(defined($uni));
54 is(length($txt),0);
55 print $dst $uni;
56 close($dst);
57 close($src);
58 ok(compare($utf,$ref) == 0);
59}
0a95303c 60
61print "# Basic encode test\n";
0e567a6c 62open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
a6a3912c 63binmode($src);
0a95303c 64ok(defined($src) && fileno($src));
0e567a6c 65$uni = join('',<$src>);
66open($dst,">",$rnd) || die "Cannot open $rnd:$!";
a6a3912c 67binmode($dst);
0a95303c 68ok(defined($dst) && fileno($dst));
0e567a6c 69$txt = $enc->encode($uni,1);
0a95303c 70ok(defined($txt));
71is(length($uni),0);
72print $dst $txt;
73close($dst);
74close($src);
75ok(compare($euc,$rnd) == 0);
76
77is($enc->name,'euc-jp');
78
79print "# src :encoding test\n";
0e567a6c 80open($src,"<encoding(euc-jp)",$euc) || die "Cannot open $euc:$!";
a6a3912c 81binmode($src);
0a95303c 82ok(defined($src) && fileno($src));
0e567a6c 83open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
a6a3912c 84binmode($dst);
0a95303c 85ok(defined($dst) || fileno($dst));
0e567a6c 86my $out = select($dst);
0a95303c 87while (<$src>)
88 {
89 print;
90 }
91close($dst);
92close($src);
fab31126 93
161720b2 94TODO:
95{
96 local $TODO = 'needs debugging on VMS' if $^O eq 'VMS';
97 ok(compare($utf,$ref) == 0);
98}
0a95303c 99select($out);
100
101SKIP:
102{
25f7d9d3 103 #skip "Multi-byte write is broken",3;
0a95303c 104 print "# dst :encoding test\n";
0e567a6c 105 open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
a6a3912c 106 binmode($src);
0a95303c 107 ok(defined($src) || fileno($src));
0e567a6c 108 open($dst,">encoding(euc-jp)",$rnd) || die "Cannot open $rnd:$!";
a6a3912c 109 binmode($dst);
0a95303c 110 ok(defined($dst) || fileno($dst));
111 my $out = select($dst);
112 while (<$src>)
113 {
114 print;
115 }
116 close($dst);
117 close($src);
118 ok(compare($euc,$rnd) == 0);
119 select($out);
120}
121
122is($enc->name,'euc-jp');
fab31126 123
0a95303c 124END {
f51e7ad5 125 1 while unlink($utf,$rnd);
0a95303c 126}