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