This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Apparently at some point doing 3-arg open with
[perl5.git] / t / io / layers.t
CommitLineData
39f7a870
JH
1#!./perl
2
046e4a6a
JH
3my $PERLIO;
4
39f7a870
JH
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 require './test.pl';
491abfa0
JH
9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
11 exit 0;
12 }
51dc4578
JH
13 eval 'use Encode';
14 if ($@ =~ /dynamic loading not available/) {
15 print "1..0 # miniperl cannot load Encode\n";
16 exit 0;
17 }
3b0db4f9
JH
18 # Makes testing easier.
19 $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
8d3a61d9 20 if (exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/) {
3b0db4f9 21 # We are not prepared for anything else.
8d3a61d9
JH
22 print "1..0 # PERLIO='$ENV{PERLIO}' unknown\n";
23 exit 0;
24 }
046e4a6a 25 $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
39f7a870
JH
26}
27
39f7a870
JH
28use Config;
29
538204d5 30my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare|mint)$/ ? 1 : 0;
cebd85e6 31 $DOSISH = 1 if !$DOSISH and $^O =~ /^uwin/;
15b61c98
JH
32my $NONSTDIO = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio' ? 1 : 0;
33my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio} ? 1 : 0;
046e4a6a 34
8229d19f
JH
35my $NTEST = 43 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0);
36
37plan tests => $NTEST;
38
046e4a6a
JH
39print <<__EOH__;
40# PERLIO = $PERLIO
41# DOSISH = $DOSISH
42# NONSTDIO = $NONSTDIO
43# FASTSTDIO = $FASTSTDIO
44__EOH__
52f03692 45
491abfa0 46SKIP: {
8229d19f 47 skip("This perl does not have Encode", $NTEST)
491abfa0 48 unless " $Config{extensions} " =~ / Encode /;
39f7a870
JH
49
50 sub check {
51 my ($result, $expected, $id) = @_;
3b0db4f9
JH
52 # An interesting dance follows where we try to make the following
53 # IO layer stack setups to compare equal:
54 #
f0fd62e2 55 # PERLIO UNIX-like DOS-like
3b0db4f9 56 #
f0fd62e2
JH
57 # unset / "" unix perlio / stdio [1] unix crlf
58 # stdio unix perlio / stdio [1] stdio
59 # perlio unix perlio unix perlio
60 # mmap unix mmap unix mmap
3b0db4f9 61 #
f0fd62e2
JH
62 # [1] "stdio" if Configure found out how to do "fast stdio" (depends
63 # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
046e4a6a 64 #
52f03692 65 if ($NONSTDIO) {
79d9a4d7
JH
66 # Get rid of "unix".
67 shift @$result if $result->[0] eq "unix";
fb189484 68 # Change expectations.
046e4a6a
JH
69 if ($FASTSTDIO) {
70 $expected->[0] = $ENV{PERLIO};
71 } else {
72 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
73 }
e29b014f 74 } elsif (!$FASTSTDIO && !$DOSISH) {
046e4a6a
JH
75 splice(@$result, 0, 2, "stdio")
76 if @$result >= 2 &&
77 $result->[0] eq "unix" &&
78 $result->[1] eq "perlio";
79d9a4d7
JH
79 } elsif ($DOSISH) {
80 splice(@$result, 0, 2, "stdio")
046e4a6a
JH
81 if @$result >= 2 &&
82 $result->[0] eq "unix" &&
79d9a4d7 83 $result->[1] eq "crlf";
fb189484 84 }
8229d19f
JH
85 if ($DOSISH && grep { $_ eq 'crlf' } @$expected) {
86 # 5 tests potentially skipped because
87 # DOSISH systems already have a CRLF layer
88 # which will make new ones not stick.
89 @$expected = grep { $_ ne 'crlf' } @$expected;
90 }
79d9a4d7 91 my $n = scalar @$expected;
8229d19f 92 is($n, scalar @$expected, "$id - layers == $n");
39f7a870
JH
93 for (my $i = 0; $i < $n; $i++) {
94 my $j = $expected->[$i];
95 if (ref $j eq 'CODE') {
fb189484 96 ok($j->($result->[$i]), "$id - $i is ok");
39f7a870
JH
97 } else {
98 is($result->[$i], $j,
8d3a61d9
JH
99 sprintf("$id - $i is %s",
100 defined $j ? $j : "undef"));
39f7a870
JH
101 }
102 }
103 }
104
105 check([ PerlIO::get_layers(STDIN) ],
106 [ "stdio" ],
107 "STDIN");
108
109 open(F, ">:crlf", "afile");
110
111 check([ PerlIO::get_layers(F) ],
112 [ qw(stdio crlf) ],
113 "open :crlf");
114
115 binmode(F, ":encoding(sjis)"); # "sjis" will be canonized to "shiftjis"
116
117 check([ PerlIO::get_layers(F) ],
118 [ qw[stdio crlf encoding(shiftjis) utf8] ],
119 ":encoding(sjis)");
120
121 binmode(F, ":pop");
122
123 check([ PerlIO::get_layers(F) ],
124 [ qw(stdio crlf) ],
125 ":pop");
126
127 binmode(F, ":raw");
128
129 check([ PerlIO::get_layers(F) ],
130 [ "stdio" ],
131 ":raw");
132
133 binmode(F, ":utf8");
134
135 check([ PerlIO::get_layers(F) ],
136 [ qw(stdio utf8) ],
137 ":utf8");
138
139 binmode(F, ":bytes");
140
141 check([ PerlIO::get_layers(F) ],
142 [ "stdio" ],
143 ":bytes");
144
145 binmode(F, ":encoding(utf8)");
146
147 check([ PerlIO::get_layers(F) ],
148 [ qw[stdio encoding(utf8) utf8] ],
149 ":encoding(utf8)");
150
151 binmode(F, ":raw :crlf");
152
153 check([ PerlIO::get_layers(F) ],
154 [ qw(stdio crlf) ],
155 ":raw:crlf");
156
157 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
158
8229d19f
JH
159 # 7 tests potentially skipped.
160 unless ($DOSISH || !$FASTSTDIO) {
fb189484
JH
161 my @results = PerlIO::get_layers(F, details => 1);
162
79d9a4d7
JH
163 # Get rid of the args and the flags.
164 splice(@results, 1, 2) if $NONSTDIO;
fb189484
JH
165
166 check([ @results ],
167 [ "stdio", undef, sub { $_[0] > 0 },
168 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
169 ":raw:encoding(latin1)");
170 }
39f7a870
JH
171
172 binmode(F);
173
174 check([ PerlIO::get_layers(F) ],
175 [ "stdio" ],
176 "binmode");
177
178 close F;
179
180 {
181 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
491abfa0 182
39f7a870
JH
183 open F, "<afile";
184 open G, ">afile";
185
186 check([ PerlIO::get_layers(F, input => 1) ],
187 [ qw(stdio crlf) ],
188 "use open IN");
189
190 check([ PerlIO::get_layers(G, output => 1) ],
191 [ qw[stdio encoding(cp1252) utf8] ],
192 "use open OUT");
193
194 close F;
195 close G;
196 }
197
198 1 while unlink "afile";
199}