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