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