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