Commit | Line | Data |
---|---|---|
e8c9ad1b | 1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
bbd5c0f5 | 6 | require Config; import Config; |
e8c9ad1b | 7 | } |
8 | ||
c0abe5aa | 9 | use Test::More tests => 23; |
e8c9ad1b | 10 | |
11 | # open::import expects 'open' as its first argument, but it clashes with open() | |
12 | sub import { | |
13 | open::import( 'open', @_ ); | |
14 | } | |
15 | ||
16 | # can't use require_ok() here, with a name like 'open' | |
217f68ed | 17 | ok( require 'open.pm', 'requiring open' ); |
e8c9ad1b | 18 | |
19 | # this should fail | |
20 | eval { import() }; | |
7f17c514 | 21 | like( $@, 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 | 25 | my $warn; |
26 | local $SIG{__WARN__} = sub { | |
27 | $warn .= shift; | |
28 | }; | |
29 | ||
7f17c514 | 30 | # and it shouldn't be able to find this layer |
99ef548b PM |
31 | $warn = ''; |
32 | eval q{ no warnings 'layer'; use open IN => ':macguffin' ; }; | |
33 | is( $warn, '', | |
7f17c514 | 34 | 'should not warn about unknown layer with bad layer provided' ); |
99ef548b PM |
35 | |
36 | $warn = ''; | |
37 | eval q{ use warnings 'layer'; use open IN => ':macguffin' ; }; | |
7f17c514 RGS |
38 | like( $warn, qr/Unknown PerlIO layer/, |
39 | 'should warn about unknown layer with bad layer provided' ); | |
e8c9ad1b | 40 | |
7c0e976d JH |
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 | 45 | import( 'IN', ':crlf' ); |
217f68ed | 46 | is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' ); |
e8c9ad1b | 47 | |
48 | # it should reset them appropriately, too | |
49 | import( 'IN', ':raw' ); | |
217f68ed | 50 | is( $^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 | 53 | eval { import( 'sideways', ':raw' ) }; |
7f17c514 | 54 | like( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' ); |
e8c9ad1b | 55 | |
56 | # but it handles them all so well together | |
1e616cf5 JH |
57 | import( 'IO', ':raw :crlf' ); |
58 | is( ${^OPEN}, ":raw :crlf\0:raw :crlf", | |
7f17c514 | 59 | 'should set multi types, multi layer' ); |
1e616cf5 | 60 | is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' ); |
e8c9ad1b | 61 | |
bbd5c0f5 | 62 | SKIP: { |
44cc5a7c | 63 | skip("no perlio, no :utf8", 12) unless (find PerlIO::Layer 'perlio'); |
820c63ad | 64 | |
24c43532 | 65 | eval <<EOE; |
bbd5c0f5 JH |
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 | 73 | EOE |
bbd5c0f5 | 74 | |
820c63ad JH |
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 JH |
81 | sub systell { |
82 | use Fcntl 'SEEK_CUR'; | |
83 | sysseek($_[0], 0, SEEK_CUR); | |
84 | } | |
e111333b | 85 | |
820c63ad JH |
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 JH |
110 | close F; |
111 | ok($ok == @a, | |
112 | "on :utf8 streams sysread() should work on characters, not bytes"); | |
113 | ||
4d70b921 NC |
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 NC |
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 NC |
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 NC |
182 | SKIP: { |
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 RGS |
186 | eval q[use Encode::Alias;use open ":std", ":locale"]; |
187 | is($@, '', 'can use :std and :locale'); | |
c0abe5aa | 188 | |
d7a09b41 SR |
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 SR |
194 | "test for an endless loop in PerlIO_find_layer"); |
195 | } | |
196 | ||
bbd5c0f5 JH |
197 | END { |
198 | 1 while unlink "utf8"; | |
e111333b JH |
199 | 1 while unlink "a"; |
200 | 1 while unlink "b"; | |
bbd5c0f5 | 201 | } |
1e616cf5 JH |
202 | |
203 | # the test cases beyond __DATA__ need to be executed separately | |
204 | ||
205 | __DATA__ | |
e8c9ad1b | 206 | $ENV{LC_ALL} = 'nonexistent.euc'; |
207 | eval { open::_get_locale_encoding() }; | |
217f68ed | 208 | like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' ); |
1e616cf5 JH |
209 | %%% |
210 | # the special :locale layer | |
b429a72e | 211 | $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R'; |
dbd62f41 JH |
212 | # the :locale will probe the locale environment variables like LANG |
213 | use open OUT => ':locale'; | |
1e616cf5 | 214 | open(O, ">koi8"); |
23bcb45a | 215 | print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1 |
1e616cf5 JH |
216 | close O; |
217 | open(I, "<koi8"); | |
23bcb45a | 218 | printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1 |
1e616cf5 JH |
219 | close I; |
220 | %%% |