This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added test for RT#78844
[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
cd86ed9d 30my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare)$/ ? 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;
a45f1ba0 34my $UTF8_STDIN;
7a403565
NC
35if (${^UNICODE} & 1) {
36 if (${^UNICODE} & 64) {
37 # Conditional on the locale
a45f1ba0 38 $UTF8_STDIN = ${^UTF8LOCALE};
7a403565
NC
39 } else {
40 # Unconditional
a45f1ba0 41 $UTF8_STDIN = 1;
7a403565 42 }
1031ca5c 43} else {
a45f1ba0 44 $UTF8_STDIN = 0;
7a403565 45}
54f31609 46my $NTEST = 45 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0)
a45f1ba0 47 + $UTF8_STDIN;
8229d19f 48
7c0e976d
JH
49sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
50
8229d19f
JH
51plan tests => $NTEST;
52
046e4a6a 53print <<__EOH__;
7a403565
NC
54# PERLIO = $PERLIO
55# DOSISH = $DOSISH
56# NONSTDIO = $NONSTDIO
57# FASTSTDIO = $FASTSTDIO
58# UNICODE = ${^UNICODE}
59# UTF8LOCALE = ${^UTF8LOCALE}
a45f1ba0 60# UTF8_STDIN = $UTF8_STDIN
046e4a6a 61__EOH__
52f03692 62
491abfa0 63SKIP: {
43651d81 64 # FIXME - more of these could be tested without Encode or full perl
8229d19f 65 skip("This perl does not have Encode", $NTEST)
491abfa0 66 unless " $Config{extensions} " =~ / Encode /;
43651d81 67 skip("miniperl does not have Encode", $NTEST) if $ENV{PERL_CORE_MINITEST};
39f7a870
JH
68
69 sub check {
70 my ($result, $expected, $id) = @_;
3b0db4f9
JH
71 # An interesting dance follows where we try to make the following
72 # IO layer stack setups to compare equal:
73 #
f0fd62e2 74 # PERLIO UNIX-like DOS-like
3b0db4f9 75 #
f0fd62e2
JH
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
3b0db4f9 80 #
f0fd62e2
JH
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"
046e4a6a 83 #
52f03692 84 if ($NONSTDIO) {
79d9a4d7
JH
85 # Get rid of "unix".
86 shift @$result if $result->[0] eq "unix";
fb189484 87 # Change expectations.
046e4a6a
JH
88 if ($FASTSTDIO) {
89 $expected->[0] = $ENV{PERLIO};
90 } else {
91 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
92 }
e29b014f 93 } elsif (!$FASTSTDIO && !$DOSISH) {
046e4a6a
JH
94 splice(@$result, 0, 2, "stdio")
95 if @$result >= 2 &&
96 $result->[0] eq "unix" &&
97 $result->[1] eq "perlio";
79d9a4d7
JH
98 } elsif ($DOSISH) {
99 splice(@$result, 0, 2, "stdio")
046e4a6a
JH
100 if @$result >= 2 &&
101 $result->[0] eq "unix" &&
79d9a4d7 102 $result->[1] eq "crlf";
fb189484 103 }
8229d19f
JH
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 @$expected = grep { $_ ne 'crlf' } @$expected;
109 }
79d9a4d7 110 my $n = scalar @$expected;
e662ed2b 111 is(scalar @$result, $n, "$id - layers == $n");
39f7a870
JH
112 for (my $i = 0; $i < $n; $i++) {
113 my $j = $expected->[$i];
114 if (ref $j eq 'CODE') {
fb189484 115 ok($j->($result->[$i]), "$id - $i is ok");
39f7a870
JH
116 } else {
117 is($result->[$i], $j,
8d3a61d9
JH
118 sprintf("$id - $i is %s",
119 defined $j ? $j : "undef"));
39f7a870
JH
120 }
121 }
122 }
123
124 check([ PerlIO::get_layers(STDIN) ],
a45f1ba0 125 $UTF8_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
39f7a870
JH
126 "STDIN");
127
62a28c97
NC
128 my $afile = tempfile();
129 open(F, ">:crlf", $afile);
39f7a870
JH
130
131 check([ PerlIO::get_layers(F) ],
132 [ qw(stdio crlf) ],
133 "open :crlf");
134
78b7ef06 135 binmode(F, ":encoding(cp1047)");
39f7a870
JH
136
137 check([ PerlIO::get_layers(F) ],
78b7ef06
SP
138 [ qw[stdio crlf encoding(cp1047) utf8] ],
139 ":encoding(cp1047)");
39f7a870
JH
140
141 binmode(F, ":pop");
142
143 check([ PerlIO::get_layers(F) ],
144 [ qw(stdio crlf) ],
145 ":pop");
146
147 binmode(F, ":raw");
148
149 check([ PerlIO::get_layers(F) ],
150 [ "stdio" ],
151 ":raw");
152
153 binmode(F, ":utf8");
154
155 check([ PerlIO::get_layers(F) ],
156 [ qw(stdio utf8) ],
157 ":utf8");
158
159 binmode(F, ":bytes");
160
161 check([ PerlIO::get_layers(F) ],
162 [ "stdio" ],
163 ":bytes");
164
165 binmode(F, ":encoding(utf8)");
166
167 check([ PerlIO::get_layers(F) ],
168 [ qw[stdio encoding(utf8) utf8] ],
169 ":encoding(utf8)");
170
171 binmode(F, ":raw :crlf");
172
173 check([ PerlIO::get_layers(F) ],
174 [ qw(stdio crlf) ],
175 ":raw:crlf");
176
177 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
178
8229d19f
JH
179 # 7 tests potentially skipped.
180 unless ($DOSISH || !$FASTSTDIO) {
fb189484
JH
181 my @results = PerlIO::get_layers(F, details => 1);
182
79d9a4d7
JH
183 # Get rid of the args and the flags.
184 splice(@results, 1, 2) if $NONSTDIO;
fb189484
JH
185
186 check([ @results ],
187 [ "stdio", undef, sub { $_[0] > 0 },
188 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
189 ":raw:encoding(latin1)");
190 }
39f7a870
JH
191
192 binmode(F);
193
194 check([ PerlIO::get_layers(F) ],
195 [ "stdio" ],
196 "binmode");
197
54f31609
DG
198 # RT78844
199 {
200 local $TODO = "RT#78844";
201 local $@ = "foo";
202 binmode(F, ":encoding(utf8)");
203 is( $@, "foo", '$@ not clobbered by binmode and :encoding');
204 }
205
39f7a870
JH
206 close F;
207
208 {
209 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
491abfa0 210
62a28c97
NC
211 open F, '<', $afile;
212 open G, '>', $afile;
39f7a870
JH
213
214 check([ PerlIO::get_layers(F, input => 1) ],
215 [ qw(stdio crlf) ],
216 "use open IN");
217
218 check([ PerlIO::get_layers(G, output => 1) ],
219 [ qw[stdio encoding(cp1252) utf8] ],
220 "use open OUT");
221
222 close F;
223 close G;
224 }
225
9cfa90c0
NC
226 # Check that PL_sigwarn's reference count is correct, and that
227 # &PerlIO::Layer::NoWarnings isn't prematurely freed.
62a28c97
NC
228 fresh_perl_like (<<"EOT", qr/^CODE/);
229open(UTF, "<:raw:encoding(utf8)", '$afile') or die \$!;
9cfa90c0
NC
230print ref *PerlIO::Layer::NoWarnings{CODE};
231EOT
39f7a870 232}