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