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