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