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