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