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