This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perl
[perl5.git] / ext / Encode / t / guess.t
1 BEGIN {
2     if ($ENV{'PERL_CORE'}){
3         chdir 't';
4         unshift @INC, '../lib';
5     }
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     }
11     if (ord("A") == 193) {
12         print "1..0 # Skip: EBCDIC\n";
13         exit 0;
14     }
15     $| = 1;
16 }
17
18 use strict;
19 use File::Basename;
20 use File::Spec;
21 use Encode qw(decode encode find_encoding _utf8_off);
22
23 #use Test::More qw(no_plan);
24 use Test::More tests => 17;
25 use_ok("Encode::Guess");
26 {
27     no warnings;
28     $Encode::Guess::DEBUG = shift || 0;
29 }
30
31 my $ascii  = join('' => map {chr($_)}(0x21..0x7e));
32 my $latin1 = join('' => map {chr($_)}(0xa1..0xfe));
33 my $utf8on  = join('' => map {chr($_)}(0x3000..0x30fe));
34 my $utf8off = $utf8on; _utf8_off($utf8off);
35 my $utf16 = encode('UTF-16', $utf8on);
36 my $utf32 = encode('UTF-32', $utf8on);
37
38 is(guess_encoding($ascii)->name, 'ascii', 'ascii');
39 like(guess_encoding($latin1), qr/No appropriate encoding/io, 'no ascii');
40 is(guess_encoding($latin1, 'latin1')->name, 'iso-8859-1', 'iso-8859-1');
41 is(guess_encoding($utf8on)->name, 'utf8', 'utf8 w/ flag');
42 is(guess_encoding($utf8off)->name, 'utf8', 'utf8 w/o flag');
43 is(guess_encoding($utf16)->name, 'UTF-16', 'UTF-16');
44 is(guess_encoding($utf32)->name, 'UTF-32', 'UTF-32');
45
46 my $jisx0201 = File::Spec->catfile(dirname(__FILE__), 'jisx0201.utf');
47 my $jisx0208 = File::Spec->catfile(dirname(__FILE__), 'jisx0208.utf');
48 my $jisx0212 = File::Spec->catfile(dirname(__FILE__), 'jisx0212.utf');
49
50 open my $fh, $jisx0208 or die "$jisx0208: $!";
51 binmode($fh);
52 $utf8off = join('' => <$fh>);
53 close $fh;
54 $utf8on = decode('utf8', $utf8off);
55
56 my @jp = qw(7bit-jis shiftjis euc-jp);
57
58 Encode::Guess->set_suspects(@jp);
59
60 for my $jp (@jp){
61     my $test = encode($jp, $utf8on);
62     is(guess_encoding($test)->name, $jp, "JP:$jp");
63 }
64
65 is (decode('Guess', encode('euc-jp', $utf8on)), $utf8on, "decode('Guess')");
66 eval{ encode('Guess', $utf8on) };
67 like($@, qr/not defined/io, "no encode()");
68
69 my %CJKT = 
70     (
71      'euc-cn'    => File::Spec->catfile(dirname(__FILE__), 'gb2312.utf'),
72      'euc-jp'    => File::Spec->catfile(dirname(__FILE__), 'jisx0208.utf'),
73      'euc-kr'    => File::Spec->catfile(dirname(__FILE__), 'ksc5601.utf'),
74      'big5-eten' => File::Spec->catfile(dirname(__FILE__), 'big5-eten.utf'),
75 );
76
77 Encode::Guess->set_suspects(keys %CJKT);
78
79 for my $name (keys %CJKT){
80     open my $fh, $CJKT{$name} or die "$CJKT{$name}: $!";
81     binmode($fh);
82     $utf8off = join('' => <$fh>);
83     close $fh;
84
85     my $test = encode($name, decode('utf8', $utf8off));
86     is(guess_encoding($test)->name, $name, "CJKT:$name");
87 }
88
89 __END__;