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
1 #!./perl
2
3 BEGIN {
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
22 my $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
35 open($fh,$file) || die "File cannot be re-opened";
36 my $line = <$fh>;
37 is($line,"\\x{20ac}0.02\n","perlqq escapes");
38 close($fh);
39
40 $PerlIO::encoding::fallback = Encode::HTMLCREF;
41
42 ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
43 my $str = "\x{20AC}";
44 print $fh $str,"0.02\n";
45 close($fh);
46
47 open($fh,$file) || die "File cannot be re-opened";
48 my $line = <$fh>;
49 is($line,"&#8364;0.02\n","HTML escapes");
50 close($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
60 ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
61 my $line = <$fh>;
62 printf "# %x\n",ord($line);
63 is($line,"\\xA30.02\n","Escaped non-mapped char");
64 close($fh);
65
66 $PerlIO::encoding::fallback = Encode::WARN_ON_ERROR;
67
68 ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
69 my $line = <$fh>;
70 printf "# %x\n",ord($line);
71 is($line,"\x{FFFD}0.02\n","Unicode replacement char");
72 close($fh);
73
74 END {
75     1 while unlink($file);
76 }