This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[perl5.git] / ext / PerlIO / t / fallback.t
CommitLineData
1982da40
NIS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
e69a2255 6 push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
1982da40
NIS
7 require "../t/test.pl";
8 skip_all("No perlio") unless (find PerlIO::Layer 'perlio');
982a4085
JH
9 if (ord("A") == 193) {
10 print "1..0 # Skip: EBCDIC\n";
11 exit 0;
12 }
709a84d3 13 plan (9);
1982da40
NIS
14}
15use Encode qw(:fallback_all);
16
17# $PerlIO::encoding = 0; # WARN_ON_ERR|PERLQQ;
18
19my $file = "fallback$$.txt";
20
709a84d3
DK
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}
1982da40
NIS
31
32open($fh,$file) || die "File cannot be re-opened";
33my $line = <$fh>;
34is($line,"\\x{20ac}0.02\n","perlqq escapes");
35close($fh);
36
37$PerlIO::encoding::fallback = Encode::HTMLCREF;
38
39ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
40my $str = "\x{20AC}";
41print $fh $str,"0.02\n";
42close($fh);
43
44open($fh,$file) || die "File cannot be re-opened";
45my $line = <$fh>;
46is($line,"&#8364;0.02\n","HTML escapes");
47close($fh);
48
e028e334
JH
49{
50 no utf8;
51 open($fh,">$file") || die "File cannot be re-opened";
52 print $fh "£0.02\n";
53 close($fh);
54}
1982da40
NIS
55
56ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
57my $line = <$fh>;
58printf "# %x\n",ord($line);
59is($line,"\\xA30.02\n","Escaped non-mapped char");
60close($fh);
61
62$PerlIO::encoding::fallback = Encode::WARN_ON_ERROR;
63
64ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
65my $line = <$fh>;
66printf "# %x\n",ord($line);
67is($line,"\x{FFFD}0.02\n","Unicode replacement char");
68close($fh);
69
70END {
cd5e0f48 71 1 while unlink($file);
1982da40
NIS
72}
73
74
75