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