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