This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
3944f7dff01a8888d2e5c82bc52510e3ecfb24ab
[perl5.git] / ext / PerlIO-encoding / t / fallback.t
1 #!./perl
2
3 BEGIN {
4     skip_all("No perlio") unless (find PerlIO::Layer 'perlio');
5     if (ord("A") == 193) {
6         print "1..0 # Skip: EBCDIC\n";
7         exit 0;
8     }
9     unless( eval { require Encode } ) { 
10         print "1..0 # Skip: No Encode\n";
11         exit 0;
12     }
13     use Test::More tests => 9;
14     import Encode qw(:fallback_all);
15 }
16
17 # $PerlIO::encoding = 0; # WARN_ON_ERR|PERLQQ;
18
19 my $file = "fallback$$.txt";
20
21 {
22     my $message = '';
23     local $SIG{__WARN__} = sub { $message = $_[0] };
24     $PerlIO::encoding::fallback = Encode::PERLQQ;
25     ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
26     my $str = "\x{20AC}";
27     print $fh $str,"0.02\n";
28     close($fh);
29     like($message, qr/does not map to iso-8859-1/o, "FB_WARN message");
30 }
31
32 open($fh,$file) || die "File cannot be re-opened";
33 my $line = <$fh>;
34 is($line,"\\x{20ac}0.02\n","perlqq escapes");
35 close($fh);
36
37 $PerlIO::encoding::fallback = Encode::HTMLCREF;
38
39 ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
40 my $str = "\x{20AC}";
41 print $fh $str,"0.02\n";
42 close($fh);
43
44 open($fh,$file) || die "File cannot be re-opened";
45 my $line = <$fh>;
46 is($line,"&#8364;0.02\n","HTML escapes");
47 close($fh);
48
49 {
50     no utf8;
51     open($fh,">$file") || die "File cannot be re-opened";
52     binmode($fh);
53     print $fh "\xA30.02\n";
54     close($fh);
55 }
56
57 ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
58 my $line = <$fh>;
59 printf "# %x\n",ord($line);
60 is($line,"\\xA30.02\n","Escaped non-mapped char");
61 close($fh);
62
63 $PerlIO::encoding::fallback = Encode::WARN_ON_ERROR;
64
65 ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
66 my $line = <$fh>;
67 printf "# %x\n",ord($line);
68 is($line,"\x{FFFD}0.02\n","Unicode replacement char");
69 close($fh);
70
71 END {
72     1 while unlink($file);
73 }