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