Fix perlio for Encode/t/perlio.t's SKIPPED TODO tests,
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / JP.t
index e9799da..4192a7c 100644 (file)
@@ -1,97 +1,89 @@
 BEGIN {
-#   chdir 't' if -d 't';
-#   @INC = (-d '../../lib' ? '../lib';
+    if ($ENV{'PERL_CORE'}){
+        chdir 't';
+        unshift @INC, '../lib';
+    }
     require Config; import Config;
     if ($Config{'extensions'} !~ /\bEncode\b/) {
       print "1..0 # Skip: Encode was not built\n";
       exit 0;
     }
+    if (ord("A") == 193) {
+       print "1..0 # Skip: EBCDIC\n";
+       exit 0;
+    }
     $| = 1;
 }
 use strict;
-use Test::More tests => 22;
+use Test::More tests => 37;
+#use Test::More qw(no_plan);
 use Encode;
 use File::Basename;
 use File::Spec;
 use File::Compare;
 require_ok "Encode::JP";
 
-my ($src, $uni, $dst, $txt);
+my ($src, $uni, $dst, $txt, $euc, $utf, $ref, $rnd);
 
-ok(defined(my $enc = find_encoding('euc-jp')));
-ok($enc->isa('Encode::XS'));
-is($enc->name,'euc-jp');
+ok(defined(my $enc = find_encoding('euc-jp')), 'find_encoding');
+ok($enc->isa('Encode::XS'), 'ISA');
+is($enc->name,'euc-jp',     '$enc->name');
 my $dir = dirname(__FILE__);
-my $euc = File::Spec->catfile($dir,"table.euc");
-my $utf = File::Spec->catfile($dir,"table.utf8");
-my $ref = File::Spec->catfile($dir,"table.ref");
-my $rnd = File::Spec->catfile($dir,"table.rnd");
-print "# Basic decode test\n";
-open($src,"<",$euc) || die "Cannot open $euc:$!";
-ok(defined($src) && fileno($src));
-$txt = join('',<$src>);
-open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
-ok(defined($dst) && fileno($dst));
-$uni = $enc->decode($txt,1);
-ok(defined($uni));
-is(length($txt),0);
-print $dst $uni;
-close($dst);
-close($src);
-ok(compare($utf,$ref) == 0);
 
-print "# Basic encode test\n";
-open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
-ok(defined($src) && fileno($src));
-$uni = join('',<$src>);
-open($dst,">",$rnd) || die "Cannot open $rnd:$!";
-ok(defined($dst) && fileno($dst));
-$txt = $enc->encode($uni,1);
-ok(defined($txt));
-is(length($uni),0);
-print $dst $txt;
-close($dst);
-close($src);
-ok(compare($euc,$rnd) == 0);
+for my $charset (qw(jisx0201 jisx0212 jisx0208)){
+    $euc = File::Spec->catfile($dir,"$charset.euc");
+    $utf = File::Spec->catfile($dir,"$$.utf8");
+    $ref = File::Spec->catfile($dir,"$charset.ref");
+    $rnd = File::Spec->catfile($dir,"$$.rnd");
+
+    open($src,"<",$euc) or die "Cannot open $euc:$!";
+    binmode($src);
+    $txt = join('',<$src>);
+    close($src);
+    
+    eval{ $uni = $enc->decode($txt, 1) }; 
+    $@ and print $@;
+    ok(defined($uni),  "decode $charset");
+    is(length($txt),0, "decode $charset completely");
 
-is($enc->name,'euc-jp');
+    open($dst,">:utf8",$utf) or die "Cannot open $utf:$!";
+    binmode($dst);
+    print $dst $uni;
+    close($dst); 
+    is(compare($utf, $ref), 0, "$utf eq $ref");
+    
+    open $src, "<:utf8", $ref or die "$ref : $!";
+    $uni = join('', <$src>);
+    close $src;
 
-print "# src :encoding test\n";
-open($src,"<encoding(euc-jp)",$euc) || die "Cannot open $euc:$!";
-ok(defined($src) && fileno($src));
-open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
-ok(defined($dst) || fileno($dst));
-my $out = select($dst);
-while (<$src>)
- {
-  print;
- }
-close($dst);
-close($src);
-ok(compare($utf,$ref) == 0);
-select($out);
+    for my $canon (qw(euc-jp shiftjis
+                     7bit-jis iso-2022-jp iso-2022-jp-1)){
+       my $test = \&is;
+       if   ($charset eq 'jisx0201'){
+           $canon eq 'iso-2022-jp'   and $test = \&isnt;
+           $canon eq 'iso-2022-jp-1' and $test = \&isnt;
+       }elsif($charset eq 'jisx0212'){
+           $canon eq 'shiftjis'    and   $test = \&isnt;
+           $canon eq 'iso-2022-jp' and   $test = \&isnt;
+       }
+       my $rt = ($test eq \&is) ? 'RT' : 'non-RT';
+       $test->($uni, decode($canon, encode($canon, $uni)), 
+             "$rt $charset $canon");
+       
+     }
 
-SKIP:
-{
- #skip "Multi-byte write is broken",3;
- print "# dst :encoding test\n";
- open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
- ok(defined($src) || fileno($src));
- open($dst,">encoding(euc-jp)",$rnd) || die "Cannot open $rnd:$!";
- ok(defined($dst) || fileno($dst));
- my $out = select($dst);
- while (<$src>)
-  {
-   print;
-  }
- close($dst);
- close($src);
- ok(compare($euc,$rnd) == 0);
- select($out);
+    eval{ $txt = $enc->encode($uni,1) };    
+    $@ and print $@;
+    ok(defined($txt),   "encode $charset");
+    is(length($uni), 0, "encode $charset completely");
+
+    open($dst,">", $rnd) or die "Cannot open $utf:$!";
+    binmode($dst);
+    print $dst $txt;
+    close($dst); 
+    is(compare($euc, $rnd), 0 => "$rnd eq $euc");
 }
 
-is($enc->name,'euc-jp');
 END {
-# unlink($utf,$rnd);
-
+ 1 while unlink($utf,$rnd);
 }