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