xref: /openbsd-src/gnu/usr.bin/perl/ext/PerlIO-encoding/t/fallback.t (revision b46d8ef224b95de1dddcd1f01c1ab482f0ab3778)
1#!./perl
2
3BEGIN {
4    unless (find PerlIO::Layer 'perlio') {
5	print "1..0 # No perlio\n";
6	exit 0;
7    }
8    if (ord("A") == 193) {
9	print "1..0 # Skip: EBCDIC\n";
10	exit 0;
11    }
12    unless ( eval { require Encode } ) {
13	print "1..0 # Skip: No Encode\n";
14	exit 0;
15    }
16    import Encode qw(:fallback_all);
17}
18
19use Test::More tests => 10;
20
21# $PerlIO::encoding = 0; # WARN_ON_ERR|PERLQQ;
22
23my $file = "fallback$$.txt";
24
25{
26    use warnings;
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}
36
37open($fh,'<',$file) || die "File cannot be re-opened";
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
49open($fh,'<',$file) || die "File cannot be re-opened";
50my $line = <$fh>;
51is($line,"&#8364;0.02\n","HTML escapes");
52close($fh);
53
54{
55    no utf8;
56    open($fh,'>',$file) || die "File cannot be re-opened";
57    binmode($fh);
58    print $fh "\xA30.02\n";
59    close($fh);
60}
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
68{
69    my $message = '';
70    local $SIG{__WARN__} = sub { $message = $_[0] };
71
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}
82
83END {
84    1 while unlink($file);
85}
86