Upgrade to Encode 1.60, from Dan Kogai.
[p5sagit/p5-mst-13.2.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 => 11;
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
32 is(Encode::Guess->guess($ascii)->name, 'ascii');
33
34 eval { Encode::Guess->guess($latin1) } ;
35 like($@, qr/No appropriate encoding/io);
36
37 Encode::Guess->import(qw(latin1));
38
39 is(Encode::Guess->guess($latin1)->name, 'iso-8859-1');
40 is(Encode::Guess->guess($utf8on)->name, 'utf8');
41
42 eval { Encode::Guess->guess($utf8off) };
43 like($@, qr/ambiguous/io);
44
45 my $jisx0201 = File::Spec->catfile(dirname(__FILE__), 'jisx0201.utf');
46 my $jisx0208 = File::Spec->catfile(dirname(__FILE__), 'jisx0208.utf');
47 my $jisx0212 = File::Spec->catfile(dirname(__FILE__), 'jisx0212.utf');
48
49 open my $fh, $jisx0208 or die "$jisx0208: $!";
50 $utf8off = join('' => <$fh>);
51 close $fh;
52 $utf8on = decode('utf8', $utf8off);
53 my @jp = qw(7bit-jis shiftjis euc-jp);
54
55 Encode::Guess->import(@jp);
56
57 for my $jp (@jp){
58     my $test = encode($jp, $utf8on);
59     is(Encode::Guess->guess($test)->name, $jp, $jp);
60 }
61 is (decode('Guess', encode('euc-jp', $utf8on)), $utf8on, "decode('Guess')");
62 eval{ encode('Guess', $utf8on) };
63 like($@, qr/lazy/io, "no encode()");
64 __END__;