Win32: encode/t/perlio.t needs some binmode
[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 }
0a95303c 15 $| = 1;
16}
0e567a6c 17use strict;
85982a32 18use Test::More tests => 37;
fab31126 19#use Test::More qw(no_plan);
0a95303c 20use Encode;
21use File::Basename;
22use File::Spec;
23use File::Compare;
0e567a6c 24require_ok "Encode::JP";
25
ee981de6 26my ($src, $uni, $dst, $txt, $euc, $utf, $ref, $rnd);
0a95303c 27
85982a32 28ok(defined(my $enc = find_encoding('euc-jp')), 'find_encoding');
29ok($enc->isa('Encode::XS'), 'ISA');
30is($enc->name,'euc-jp', '$enc->name');
0a95303c 31my $dir = dirname(__FILE__);
ee981de6 32
85982a32 33for my $charset (qw(jisx0201 jisx0212 jisx0208)){
34 $euc = File::Spec->catfile($dir,"$charset.euc");
ee981de6 35 $utf = File::Spec->catfile($dir,"$$.utf8");
85982a32 36 $ref = File::Spec->catfile($dir,"$charset.ref");
ee981de6 37 $rnd = File::Spec->catfile($dir,"$$.rnd");
85982a32 38
39 open($src,"<",$euc) or die "Cannot open $euc:$!";
ee981de6 40 binmode($src);
ee981de6 41 $txt = join('',<$src>);
ee981de6 42 close($src);
85982a32 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");
b2704119 48
85982a32 49 open($dst,">:utf8",$utf) or die "Cannot open $utf:$!";
b2704119 50 binmode($dst);
85982a32 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;
b2704119 58
85982a32 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 }
b2704119 74
85982a32 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:$!";
b2704119 81 binmode($dst);
85982a32 82 print $dst $txt;
83 close($dst);
84 is(compare($euc, $rnd), 0 => "$rnd eq $euc");
0a95303c 85}
86
0a95303c 87END {
f51e7ad5 88 1 while unlink($utf,$rnd);
0a95303c 89}