This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/test.pl: Add is_miniperl()
[perl5.git] / t / io / layers.t
1 #!./perl
2
3 my $PERLIO;
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8     require './test.pl';
9     unless (find PerlIO::Layer 'perlio') {
10         print "1..0 # Skip: not perlio\n";
11         exit 0;
12     }
13     eval 'use Encode';
14     if ($@ =~ /dynamic loading not available/) {
15         print "1..0 # miniperl cannot load Encode\n";
16         exit 0;
17     }
18     # Makes testing easier.
19     $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
20     if (exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/) {
21         # We are not prepared for anything else.
22         print "1..0 # PERLIO='$ENV{PERLIO}' unknown\n";
23         exit 0;
24     }
25     $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
26 }
27
28 use Config;
29
30 my $DOSISH    = $^O =~ /^(?:MSWin32|os2|dos|NetWare)$/ ? 1 : 0;
31    $DOSISH    = 1 if !$DOSISH and $^O =~ /^uwin/;
32 my $NONSTDIO  = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio'     ? 1 : 0;
33 my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio}      ? 1 : 0;
34 my $UTF8_STDIN;
35 if (${^UNICODE} & 1) {
36     if (${^UNICODE} & 64) {
37         # Conditional on the locale
38         $UTF8_STDIN = ${^UTF8LOCALE};
39     } else {
40         # Unconditional
41         $UTF8_STDIN = 1;
42     }
43 } else {
44     $UTF8_STDIN = 0;
45 }
46 my $NTEST = 55 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 7 : 0)
47     + $UTF8_STDIN;
48
49 sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
50
51 plan tests => $NTEST;
52
53 print <<__EOH__;
54 # PERLIO        = $PERLIO
55 # DOSISH        = $DOSISH
56 # NONSTDIO      = $NONSTDIO
57 # FASTSTDIO     = $FASTSTDIO
58 # UNICODE       = ${^UNICODE}
59 # UTF8LOCALE    = ${^UTF8LOCALE}
60 # UTF8_STDIN = $UTF8_STDIN
61 __EOH__
62
63 SKIP: {
64     # FIXME - more of these could be tested without Encode or full perl
65     skip("This perl does not have Encode", $NTEST)
66         unless " $Config{extensions} " =~ / Encode /;
67     skip("miniperl does not have Encode", $NTEST) if $ENV{PERL_CORE_MINITEST};
68
69     sub check {
70         my ($result, $expected, $id) = @_;
71         # An interesting dance follows where we try to make the following
72         # IO layer stack setups to compare equal:
73         #
74         # PERLIO     UNIX-like                   DOS-like
75         #
76         # unset / "" unix perlio / stdio [1]     unix crlf
77         # stdio      unix perlio / stdio [1]     stdio
78         # perlio     unix perlio                 unix perlio
79         # mmap       unix mmap                   unix mmap
80         #
81         # [1] "stdio" if Configure found out how to do "fast stdio" (depends
82         # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
83         #
84         if ($NONSTDIO) {
85             # Get rid of "unix".
86             shift @$result if $result->[0] eq "unix";
87             # Change expectations.
88             if ($FASTSTDIO) {
89                 $expected->[0] = $ENV{PERLIO};
90             } else {
91                 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
92             }
93         } elsif (!$FASTSTDIO && !$DOSISH) {
94             splice(@$result, 0, 2, "stdio")
95                 if @$result >= 2 &&
96                    $result->[0] eq "unix" &&
97                    $result->[1] eq "perlio";
98         } elsif ($DOSISH) {
99             splice(@$result, 0, 2, "stdio")
100                 if @$result >= 2 &&
101                    $result->[0] eq "unix" &&
102                    $result->[1] eq "crlf";
103         }
104         if ($DOSISH && grep { $_ eq 'crlf' } @$expected) {
105             # 5 tests potentially skipped because
106             # DOSISH systems already have a CRLF layer
107             # which will make new ones not stick.
108             splice @$expected, 1, 1 if $expected->[1] eq 'crlf';
109         }
110         my $n = scalar @$expected;
111         is(scalar @$result, $n, "$id - layers == $n");
112         for (my $i = 0; $i < $n; $i++) {
113             my $j = $expected->[$i];
114             if (ref $j eq 'CODE') {
115                 ok($j->($result->[$i]), "$id - $i is ok");
116             } else {
117                 is($result->[$i], $j,
118                    sprintf("$id - $i is %s",
119                            defined $j ? $j : "undef"));
120             }
121         }
122     }
123
124     check([ PerlIO::get_layers(STDIN) ],
125           $UTF8_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
126           "STDIN");
127
128     my $afile = tempfile();
129     open(F, ">:crlf", $afile);
130
131     check([ PerlIO::get_layers(F) ],
132           [ qw(stdio crlf) ],
133           "open :crlf");
134
135     binmode(F, ":crlf");
136
137     check([ PerlIO::get_layers(F) ],
138           [ qw(stdio crlf) ],
139           "binmode :crlf");
140
141     binmode(F, ":encoding(cp1047)"); 
142
143     check([ PerlIO::get_layers(F) ],
144           [ qw[stdio crlf encoding(cp1047) utf8] ],
145           ":encoding(cp1047)");
146
147     binmode(F, ":crlf");
148
149     check([ PerlIO::get_layers(F) ],
150           [ qw[stdio crlf encoding(cp1047) utf8 crlf utf8] ],
151           ":encoding(cp1047):crlf");
152     
153     binmode(F, ":pop:pop");
154
155     check([ PerlIO::get_layers(F) ],
156           [ qw(stdio crlf) ],
157           ":pop");
158
159     binmode(F, ":raw");
160
161     check([ PerlIO::get_layers(F) ],
162           [ "stdio" ],
163           ":raw");
164
165     binmode(F, ":utf8");
166
167     check([ PerlIO::get_layers(F) ],
168           [ qw(stdio utf8) ],
169           ":utf8");
170
171     binmode(F, ":bytes");
172
173     check([ PerlIO::get_layers(F) ],
174           [ "stdio" ],
175           ":bytes");
176
177     binmode(F, ":encoding(utf8)");
178
179     check([ PerlIO::get_layers(F) ],
180             [ qw[stdio encoding(utf8) utf8] ],
181             ":encoding(utf8)");
182
183     binmode(F, ":raw :crlf");
184
185     check([ PerlIO::get_layers(F) ],
186           [ qw(stdio crlf) ],
187           ":raw:crlf");
188
189     binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
190
191     # 7 tests potentially skipped.
192     unless ($DOSISH || !$FASTSTDIO) {
193         my @results = PerlIO::get_layers(F, details => 1);
194
195         # Get rid of the args and the flags.
196         splice(@results, 1, 2) if $NONSTDIO;
197
198         check([ @results ],
199               [ "stdio",    undef,        sub { $_[0] > 0 },
200                 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
201               ":raw:encoding(latin1)");
202     }
203
204     binmode(F);
205
206     check([ PerlIO::get_layers(F) ],
207           [ "stdio" ],
208           "binmode");
209
210     # RT78844
211     {
212         local $@ = "foo";
213         binmode(F, ":encoding(utf8)");
214         is( $@, "foo", '$@ not clobbered by binmode and :encoding');
215     }
216
217     close F;
218
219     {
220         use open(IN => ":crlf", OUT => ":encoding(cp1252)");
221
222         open F, '<', $afile;
223         open G, '>', $afile;
224
225         check([ PerlIO::get_layers(F, input  => 1) ],
226               [ qw(stdio crlf) ],
227               "use open IN");
228         
229         check([ PerlIO::get_layers(G, output => 1) ],
230               [ qw[stdio encoding(cp1252) utf8] ],
231               "use open OUT");
232
233         close F;
234         close G;
235     }
236
237     # Check that PL_sigwarn's reference count is correct, and that 
238     # &PerlIO::Layer::NoWarnings isn't prematurely freed.
239     fresh_perl_like (<<"EOT", qr/^CODE/);
240 open(UTF, "<:raw:encoding(utf8)", '$afile') or die \$!;
241 print ref *PerlIO::Layer::NoWarnings{CODE};
242 EOT
243 }