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