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