The test requires perlio.
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / JP.t
1 BEGIN {
2     chdir 't' if -d 't';
3     @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     $| = 1;
14 }
15 use strict;
16 use Test::More tests => 22;
17 use Encode;
18 use File::Basename;
19 use File::Spec;
20 use File::Compare;
21 require_ok "Encode::JP";
22
23 my ($src, $uni, $dst, $txt);
24
25 ok(defined(my $enc = find_encoding('euc-jp')));
26 ok($enc->isa('Encode::XS'));
27 is($enc->name,'euc-jp');
28 my $dir = dirname(__FILE__);
29 my $euc = File::Spec->catfile($dir,"table.euc");
30 my $utf = File::Spec->catfile($dir,"$$.utf8");
31 my $ref = File::Spec->catfile($dir,"table.ref");
32 my $rnd = File::Spec->catfile($dir,"$$.rnd");
33 print "# Basic decode test\n";
34 open($src,"<",$euc) || die "Cannot open $euc:$!";
35 ok(defined($src) && fileno($src));
36 $txt = join('',<$src>);
37 open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
38 ok(defined($dst) && fileno($dst));
39 $uni = $enc->decode($txt,1);
40 ok(defined($uni));
41 is(length($txt),0);
42 print $dst $uni;
43 close($dst);
44 close($src);
45 ok(compare($utf,$ref) == 0);
46
47 print "# Basic encode test\n";
48 open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
49 ok(defined($src) && fileno($src));
50 $uni = join('',<$src>);
51 open($dst,">",$rnd) || die "Cannot open $rnd:$!";
52 ok(defined($dst) && fileno($dst));
53 $txt = $enc->encode($uni,1);
54 ok(defined($txt));
55 is(length($uni),0);
56 print $dst $txt;
57 close($dst);
58 close($src);
59 ok(compare($euc,$rnd) == 0);
60
61 is($enc->name,'euc-jp');
62
63 print "# src :encoding test\n";
64 open($src,"<encoding(euc-jp)",$euc) || die "Cannot open $euc:$!";
65 ok(defined($src) && fileno($src));
66 open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
67 ok(defined($dst) || fileno($dst));
68 my $out = select($dst);
69 while (<$src>)
70  {
71   print;
72  }
73 close($dst);
74 close($src);
75 ok(compare($utf,$ref) == 0);
76 select($out);
77
78 SKIP:
79 {
80  #skip "Multi-byte write is broken",3;
81  print "# dst :encoding test\n";
82  open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
83  ok(defined($src) || fileno($src));
84  open($dst,">encoding(euc-jp)",$rnd) || die "Cannot open $rnd:$!";
85  ok(defined($dst) || fileno($dst));
86  my $out = select($dst);
87  while (<$src>)
88   {
89    print;
90   }
91  close($dst);
92  close($src);
93  ok(compare($euc,$rnd) == 0);
94  select($out);
95 }
96
97 is($enc->name,'euc-jp');
98 END {
99  1 while unlink($utf,$rnd);
100 }