This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PerlIO::encoding:fallback.t: Don't use undefined sub
[perl5.git] / ext / PerlIO-encoding / t / fallback.t
... / ...
CommitLineData
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 use Test::More tests => 9;
17 import Encode qw(:fallback_all);
18}
19
20# $PerlIO::encoding = 0; # WARN_ON_ERR|PERLQQ;
21
22my $file = "fallback$$.txt";
23
24{
25 my $message = '';
26 local $SIG{__WARN__} = sub { $message = $_[0] };
27 $PerlIO::encoding::fallback = Encode::PERLQQ;
28 ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
29 my $str = "\x{20AC}";
30 print $fh $str,"0.02\n";
31 close($fh);
32 like($message, qr/does not map to iso-8859-1/o, "FB_WARN message");
33}
34
35open($fh,$file) || die "File cannot be re-opened";
36my $line = <$fh>;
37is($line,"\\x{20ac}0.02\n","perlqq escapes");
38close($fh);
39
40$PerlIO::encoding::fallback = Encode::HTMLCREF;
41
42ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
43my $str = "\x{20AC}";
44print $fh $str,"0.02\n";
45close($fh);
46
47open($fh,$file) || die "File cannot be re-opened";
48my $line = <$fh>;
49is($line,"&#8364;0.02\n","HTML escapes");
50close($fh);
51
52{
53 no utf8;
54 open($fh,">$file") || die "File cannot be re-opened";
55 binmode($fh);
56 print $fh "\xA30.02\n";
57 close($fh);
58}
59
60ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
61my $line = <$fh>;
62printf "# %x\n",ord($line);
63is($line,"\\xA30.02\n","Escaped non-mapped char");
64close($fh);
65
66$PerlIO::encoding::fallback = Encode::WARN_ON_ERROR;
67
68ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
69my $line = <$fh>;
70printf "# %x\n",ord($line);
71is($line,"\x{FFFD}0.02\n","Unicode replacement char");
72close($fh);
73
74END {
75 1 while unlink($file);
76}