This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
croak() needs context.
[perl5.git] / ext / Encode / t / JP.t
CommitLineData
0a95303c 1BEGIN {
f51e7ad5
JH
2 chdir 't' if -d 't';
3 @INC = '../lib';
0a95303c
NIS
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 }
87e4f210
JH
9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: PerlIO was not built\n";
11 exit 0;
12 }
0a95303c
NIS
13 $| = 1;
14}
0e567a6c 15use strict;
0a95303c
NIS
16use Test::More tests => 22;
17use Encode;
18use File::Basename;
19use File::Spec;
20use File::Compare;
0e567a6c
JH
21require_ok "Encode::JP";
22
23my ($src, $uni, $dst, $txt);
0a95303c
NIS
24
25ok(defined(my $enc = find_encoding('euc-jp')));
26ok($enc->isa('Encode::XS'));
27is($enc->name,'euc-jp');
28my $dir = dirname(__FILE__);
29my $euc = File::Spec->catfile($dir,"table.euc");
f51e7ad5 30my $utf = File::Spec->catfile($dir,"$$.utf8");
0a95303c 31my $ref = File::Spec->catfile($dir,"table.ref");
f51e7ad5 32my $rnd = File::Spec->catfile($dir,"$$.rnd");
0a95303c 33print "# Basic decode test\n";
0e567a6c 34open($src,"<",$euc) || die "Cannot open $euc:$!";
0a95303c 35ok(defined($src) && fileno($src));
0e567a6c
JH
36$txt = join('',<$src>);
37open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
0a95303c 38ok(defined($dst) && fileno($dst));
0e567a6c 39$uni = $enc->decode($txt,1);
0a95303c
NIS
40ok(defined($uni));
41is(length($txt),0);
42print $dst $uni;
43close($dst);
44close($src);
45ok(compare($utf,$ref) == 0);
46
47print "# Basic encode test\n";
0e567a6c 48open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
0a95303c 49ok(defined($src) && fileno($src));
0e567a6c
JH
50$uni = join('',<$src>);
51open($dst,">",$rnd) || die "Cannot open $rnd:$!";
0a95303c 52ok(defined($dst) && fileno($dst));
0e567a6c 53$txt = $enc->encode($uni,1);
0a95303c
NIS
54ok(defined($txt));
55is(length($uni),0);
56print $dst $txt;
57close($dst);
58close($src);
59ok(compare($euc,$rnd) == 0);
60
61is($enc->name,'euc-jp');
62
63print "# src :encoding test\n";
0e567a6c 64open($src,"<encoding(euc-jp)",$euc) || die "Cannot open $euc:$!";
0a95303c 65ok(defined($src) && fileno($src));
0e567a6c 66open($dst,">:utf8",$utf) || die "Cannot open $utf:$!";
0a95303c 67ok(defined($dst) || fileno($dst));
0e567a6c 68my $out = select($dst);
0a95303c
NIS
69while (<$src>)
70 {
71 print;
72 }
73close($dst);
74close($src);
75ok(compare($utf,$ref) == 0);
76select($out);
77
78SKIP:
79{
25f7d9d3 80 #skip "Multi-byte write is broken",3;
0a95303c 81 print "# dst :encoding test\n";
0e567a6c 82 open($src,"<:utf8",$ref) || die "Cannot open $ref:$!";
0a95303c 83 ok(defined($src) || fileno($src));
0e567a6c 84 open($dst,">encoding(euc-jp)",$rnd) || die "Cannot open $rnd:$!";
0a95303c
NIS
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
97is($enc->name,'euc-jp');
98END {
f51e7ad5 99 1 while unlink($utf,$rnd);
0a95303c 100}