miniperl cannot handle layers.t.
[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 plan tests => 43;
29
30 use Config;
31
32 my $DOSISH    = $^O =~ /^(?:MSWin32|cygwin|os2|dos|NetWare|mint)$/ ? 1 : 0;
33 my $NONSTDIO  = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio'     ? 1 : 0;
34 my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio}      ? 1 : 0;
35
36 print <<__EOH__;
37 # PERLIO    = $PERLIO
38 # DOSISH    = $DOSISH
39 # NONSTDIO  = $NONSTDIO
40 # FASTSTDIO = $FASTSTDIO
41 __EOH__
42
43 SKIP: {
44     skip("This perl does not have Encode", 43)
45         unless " $Config{extensions} " =~ / Encode /;
46
47     sub check {
48         my ($result, $expected, $id) = @_;
49         # An interesting dance follows where we try to make the following
50         # IO layer stack setups to compare equal:
51         #
52         # PERLIO     UNIX-like                   DOS-like
53         #
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
58         #
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"
61         #
62         if ($NONSTDIO) {
63             # Get rid of "unix".
64             shift @$result if $result->[0] eq "unix";
65             # Change expectations.
66             if ($FASTSTDIO) {
67                 $expected->[0] = $ENV{PERLIO};
68             } else {
69                 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
70             }
71         } elsif (!$FASTSTDIO && !$DOSISH) {
72             splice(@$result, 0, 2, "stdio")
73                 if @$result >= 2 &&
74                    $result->[0] eq "unix" &&
75                    $result->[1] eq "perlio";
76         } elsif ($DOSISH) {
77             splice(@$result, 0, 2, "stdio")
78                 if @$result >= 2 &&
79                    $result->[0] eq "unix" &&
80                    $result->[1] eq "crlf";
81         }
82         my $n = scalar @$expected;
83         is($n, scalar @$expected, "$id - layers = $n");
84         for (my $i = 0; $i < $n; $i++) {
85             my $j = $expected->[$i];
86             if (ref $j eq 'CODE') {
87                 ok($j->($result->[$i]), "$id - $i is ok");
88             } else {
89                 is($result->[$i], $j,
90                    sprintf("$id - $i is %s",
91                            defined $j ? $j : "undef"));
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
124     binmode(F, ":pop") if $DOSISH; # Drop one extra :crlf.
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
151     SKIP: {
152         skip("too complex layer coreography", 7) if $DOSISH || !$FASTSTDIO;
153
154         my @results = PerlIO::get_layers(F, details => 1);
155
156         # Get rid of the args and the flags.
157         splice(@results, 1, 2) if $NONSTDIO;
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     }
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)");
175
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 }