Root is the lizard king.
[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", 4) unless $Config{'useperlio'};
75
76     eval <<EOE;
77     use open ':utf8';
78     open(O, ">utf8");
79     print O chr(0x100);
80     close O;
81     open(I, "<utf8");
82     is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
83     close I;
84 EOE
85
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;
91
92     sub systell {
93         use Fcntl 'SEEK_CUR';
94         sysseek($_[0], 0, SEEK_CUR);
95     }
96
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++;
120     }
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++;
142     }
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++;
166     }
167     close G;
168     ok($ok == @a,
169        "checking syswrite() output on :utf8 streams by reading it back in");
170 }
171
172 END {
173     1 while unlink "utf8";
174     1 while unlink "a";
175     1 while unlink "b";
176 }
177
178 # the test cases beyond __DATA__ need to be executed separately
179
180 __DATA__
181 $ENV{LC_ALL} = 'nonexistent.euc';
182 eval { open::_get_locale_encoding() };
183 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
184 %%%
185 # the special :locale layer
186 $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
187 # the :locale will probe the locale environment variables like LANG
188 use open OUT => ':locale';
189 open(O, ">koi8");
190 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
191 close O;
192 open(I, "<koi8");
193 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
194 close I;
195 %%%