This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Pod::LaTeX 0.57
[perl5.git] / lib / open.t
CommitLineData
e8c9ad1b 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
e69a2255 6 push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
bbd5c0f5 7 require Config; import Config;
e8c9ad1b 8}
9
d7a09b41 10use Test::More tests => 17;
e8c9ad1b 11
12# open::import expects 'open' as its first argument, but it clashes with open()
13sub import {
14 open::import( 'open', @_ );
15}
16
17# can't use require_ok() here, with a name like 'open'
217f68ed 18ok( require 'open.pm', 'requiring open' );
e8c9ad1b 19
20# this should fail
21eval { import() };
7f17c514 22like( $@, qr/needs explicit list of PerlIO layers/,
217f68ed 23 'import should fail without args' );
e8c9ad1b 24
25# the hint bits shouldn't be set yet
6b5da1a3 26is( $^H & $open::hint_bits, 0,
217f68ed 27 'hint bits should not be set in $^H before open import' );
e8c9ad1b 28
29# prevent it from loading I18N::Langinfo, so we can test encoding failures
e8c9ad1b 30my $warn;
31local $SIG{__WARN__} = sub {
32 $warn .= shift;
33};
34
7f17c514 35# and it shouldn't be able to find this layer
99ef548b
PM
36$warn = '';
37eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
38is( $warn, '',
7f17c514 39 'should not warn about unknown layer with bad layer provided' );
99ef548b
PM
40
41$warn = '';
42eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
7f17c514
RGS
43like( $warn, qr/Unknown PerlIO layer/,
44 'should warn about unknown layer with bad layer provided' );
e8c9ad1b 45
c129b0bd 46SKIP: {
6b5da1a3 47 skip("no perlio, no :utf8", 1) unless (find PerlIO::Layer 'perlio');
54cfe943 48 skip("no Encode for locale layer", 1) unless eval { require Encode };
c129b0bd
JH
49 # now load a real-looking locale
50 $ENV{LC_ALL} = ' .utf8';
51 import( 'IN', 'locale' );
d928c1f3 52 like( ${^OPEN}, qr/^(:utf8)?:utf8\0/,
c129b0bd
JH
53 'should set a valid locale layer' );
54}
e8c9ad1b 55
56# and see if it sets the magic variables appropriately
57import( 'IN', ':crlf' );
6b5da1a3 58ok( $^H & $open::hint_bits,
217f68ed 59 'hint bits should be set in $^H after open import' );
60is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
e8c9ad1b 61
62# it should reset them appropriately, too
63import( 'IN', ':raw' );
217f68ed 64is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
e8c9ad1b 65
1e616cf5 66# it dies if you don't set IN, OUT, or IO
e8c9ad1b 67eval { import( 'sideways', ':raw' ) };
7f17c514 68like( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' );
e8c9ad1b 69
70# but it handles them all so well together
1e616cf5
JH
71import( 'IO', ':raw :crlf' );
72is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
7f17c514 73 'should set multi types, multi layer' );
1e616cf5 74is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
e8c9ad1b 75
bbd5c0f5 76SKIP: {
6b5da1a3 77 skip("no perlio, no :utf8", 4) unless (find PerlIO::Layer 'perlio');
820c63ad 78
24c43532 79 eval <<EOE;
bbd5c0f5
JH
80 use open ':utf8';
81 open(O, ">utf8");
82 print O chr(0x100);
83 close O;
84 open(I, "<utf8");
e111333b 85 is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
bbd5c0f5 86 close I;
24c43532 87EOE
bbd5c0f5 88
820c63ad
JH
89 open F, ">a";
90 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
91 unshift @a, chr(0); # ... and a null byte in front just for fun
92 print F @a;
93 close F;
e111333b 94
820c63ad
JH
95 sub systell {
96 use Fcntl 'SEEK_CUR';
97 sysseek($_[0], 0, SEEK_CUR);
98 }
e111333b 99
820c63ad
JH
100 require bytes; # not use
101
102 my $ok;
103
104 open F, "<:utf8", "a";
105 $ok = $a = 0;
106 for (@a) {
107 unless (
108 ($c = sysread(F, $b, 1)) == 1 &&
109 length($b) == 1 &&
110 ord($b) == ord($_) &&
111 systell(F) == ($a += bytes::length($b))
112 ) {
113 print '# ord($_) == ', ord($_), "\n";
114 print '# ord($b) == ', ord($b), "\n";
115 print '# length($b) == ', length($b), "\n";
116 print '# bytes::length($b) == ', bytes::length($b), "\n";
117 print '# systell(F) == ', systell(F), "\n";
118 print '# $a == ', $a, "\n";
119 print '# $c == ', $c, "\n";
120 last;
121 }
122 $ok++;
e111333b 123 }
820c63ad
JH
124 close F;
125 ok($ok == @a,
126 "on :utf8 streams sysread() should work on characters, not bytes");
127
128 # syswrite() on should work on characters, not bytes
129 open G, ">:utf8", "b";
130 $ok = $a = 0;
131 for (@a) {
132 unless (
133 ($c = syswrite(G, $_, 1)) == 1 &&
134 systell(G) == ($a += bytes::length($_))
135 ) {
136 print '# ord($_) == ', ord($_), "\n";
137 print '# bytes::length($_) == ', bytes::length($_), "\n";
138 print '# systell(G) == ', systell(G), "\n";
139 print '# $a == ', $a, "\n";
140 print '# $c == ', $c, "\n";
141 print "not ";
142 last;
143 }
144 $ok++;
e111333b 145 }
820c63ad
JH
146 close G;
147 ok($ok == @a,
148 "on :utf8 streams syswrite() should work on characters, not bytes");
149
150 open G, "<:utf8", "b";
151 $ok = $a = 0;
152 for (@a) {
153 unless (
154 ($c = sysread(G, $b, 1)) == 1 &&
155 length($b) == 1 &&
156 ord($b) == ord($_) &&
157 systell(G) == ($a += bytes::length($_))
158 ) {
159 print '# ord($_) == ', ord($_), "\n";
160 print '# ord($b) == ', ord($b), "\n";
161 print '# length($b) == ', length($b), "\n";
162 print '# bytes::length($b) == ', bytes::length($b), "\n";
163 print '# systell(G) == ', systell(G), "\n";
164 print '# $a == ', $a, "\n";
165 print '# $c == ', $c, "\n";
166 last;
167 }
168 $ok++;
e111333b 169 }
820c63ad
JH
170 close G;
171 ok($ok == @a,
172 "checking syswrite() output on :utf8 streams by reading it back in");
e111333b 173}
e111333b 174
c4b28b7c
RGS
175SKIP: {
176 skip("no perlio", 1) unless (find PerlIO::Layer 'perlio');
d7a09b41
SR
177 use open IN => ':non-existent';
178 eval {
c9bca74a 179 require Symbol; # Anything that exists but we havn't loaded
d7a09b41 180 };
c9bca74a 181 like($@, qr/Can't locate Symbol|Recursive call/i,
d7a09b41
SR
182 "test for an endless loop in PerlIO_find_layer");
183}
184
bbd5c0f5
JH
185END {
186 1 while unlink "utf8";
e111333b
JH
187 1 while unlink "a";
188 1 while unlink "b";
bbd5c0f5 189}
1e616cf5
JH
190
191# the test cases beyond __DATA__ need to be executed separately
192
193__DATA__
e8c9ad1b 194$ENV{LC_ALL} = 'nonexistent.euc';
195eval { open::_get_locale_encoding() };
217f68ed 196like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
1e616cf5
JH
197%%%
198# the special :locale layer
b429a72e 199$ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
dbd62f41
JH
200# the :locale will probe the locale environment variables like LANG
201use open OUT => ':locale';
1e616cf5 202open(O, ">koi8");
23bcb45a 203print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
1e616cf5
JH
204close O;
205open(I, "<koi8");
23bcb45a 206printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
1e616cf5
JH
207close I;
208%%%