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