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