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