6 require Config; import Config;
9 use Test::More tests => 16;
11 # open::import expects 'open' as its first argument, but it clashes with open()
13 open::import( 'open', @_ );
16 # can't use require_ok() here, with a name like 'open'
17 ok( require 'open.pm', 'requiring open' );
21 like( $@, qr/needs explicit list of disciplines/,
22 'import should fail without args' );
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' );
28 # prevent it from loading I18N::Langinfo, so we can test encoding failures
30 local $SIG{__WARN__} = sub {
34 # and it shouldn't be able to find this discipline
36 eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
38 'should not warn about unknown discipline with bad discipline provided' );
41 eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
42 like( $warn, qr/Unknown discipline layer/,
43 'should warn about unknown discipline with bad discipline provided' );
46 skip("no perlio, no :utf8", 1) unless $Config{useperlio};
47 # now load a real-looking locale
48 $ENV{LC_ALL} = ' .utf8';
49 import( 'IN', 'locale' );
50 like( ${^OPEN}, qr/^(:utf8)?:utf8\0/,
51 'should set a valid locale layer' );
54 # and see if it sets the magic variables appropriately
55 import( 'IN', ':crlf' );
56 ok( $^H & $open::hint_bits,
57 'hint bits should be set in $^H after open import' );
58 is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
60 # it should reset them appropriately, too
61 import( 'IN', ':raw' );
62 is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
64 # it dies if you don't set IN, OUT, or IO
65 eval { import( 'sideways', ':raw' ) };
66 like( $@, qr/Unknown discipline class/, 'should croak with unknown class' );
68 # but it handles them all so well together
69 import( 'IO', ':raw :crlf' );
70 is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
71 'should set multi types, multi disciplines' );
72 is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
75 skip("no perlio, no :utf8", 4) unless $Config{'useperlio'};
83 is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
88 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
89 unshift @a, chr(0); # ... and a null byte in front just for fun
95 sysseek($_[0], 0, SEEK_CUR);
98 require bytes; # not use
102 open F, "<:utf8", "a";
106 ($c = sysread(F, $b, 1)) == 1 &&
108 ord($b) == ord($_) &&
109 systell(F) == ($a += bytes::length($b))
111 print '# ord($_) == ', ord($_), "\n";
112 print '# ord($b) == ', ord($b), "\n";
113 print '# length($b) == ', length($b), "\n";
114 print '# bytes::length($b) == ', bytes::length($b), "\n";
115 print '# systell(F) == ', systell(F), "\n";
116 print '# $a == ', $a, "\n";
117 print '# $c == ', $c, "\n";
124 "on :utf8 streams sysread() should work on characters, not bytes");
126 # syswrite() on should work on characters, not bytes
127 open G, ">:utf8", "b";
131 ($c = syswrite(G, $_, 1)) == 1 &&
132 systell(G) == ($a += bytes::length($_))
134 print '# ord($_) == ', ord($_), "\n";
135 print '# bytes::length($_) == ', bytes::length($_), "\n";
136 print '# systell(G) == ', systell(G), "\n";
137 print '# $a == ', $a, "\n";
138 print '# $c == ', $c, "\n";
146 "on :utf8 streams syswrite() should work on characters, not bytes");
148 open G, "<:utf8", "b";
152 ($c = sysread(G, $b, 1)) == 1 &&
154 ord($b) == ord($_) &&
155 systell(G) == ($a += bytes::length($_))
157 print '# ord($_) == ', ord($_), "\n";
158 print '# ord($b) == ', ord($b), "\n";
159 print '# length($b) == ', length($b), "\n";
160 print '# bytes::length($b) == ', bytes::length($b), "\n";
161 print '# systell(G) == ', systell(G), "\n";
162 print '# $a == ', $a, "\n";
163 print '# $c == ', $c, "\n";
170 "checking syswrite() output on :utf8 streams by reading it back in");
174 1 while unlink "utf8";
179 # the test cases beyond __DATA__ need to be executed separately
182 $ENV{LC_ALL} = 'nonexistent.euc';
183 eval { open::_get_locale_encoding() };
184 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
186 # the special :locale layer
187 $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
188 # the :locale will probe the locale environment variables like LANG
189 use open OUT => ':locale';
191 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
194 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1