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
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}' );
38 local $SIG{__WARN__} = sub {
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' );
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' );
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' );
59 # it should reset them appropriately, too
60 import( 'IN', ':raw' );
61 is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
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' );
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' );
74 skip("no perlio, no :utf8", 4) unless $Config{'useperlio'};
81 is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
85 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
86 unshift @a, chr(0); # ... and a null byte in front just for fun
92 sysseek($_[0], 0, SEEK_CUR);
95 require bytes; # not use
99 open F, "<:utf8", "a";
103 ($c = sysread(F, $b, 1)) == 1 &&
105 ord($b) == ord($_) &&
106 systell(F) == ($a += bytes::length($b))
108 print '# ord($_) == ', ord($_), "\n";
109 print '# ord($b) == ', ord($b), "\n";
110 print '# length($b) == ', length($b), "\n";
111 print '# bytes::length($b) == ', bytes::length($b), "\n";
112 print '# systell(F) == ', systell(F), "\n";
113 print '# $a == ', $a, "\n";
114 print '# $c == ', $c, "\n";
121 "on :utf8 streams sysread() should work on characters, not bytes");
123 # syswrite() on should work on characters, not bytes
124 open G, ">:utf8", "b";
128 ($c = syswrite(G, $_, 1)) == 1 &&
129 systell(G) == ($a += bytes::length($_))
131 print '# ord($_) == ', ord($_), "\n";
132 print '# bytes::length($_) == ', bytes::length($_), "\n";
133 print '# systell(G) == ', systell(G), "\n";
134 print '# $a == ', $a, "\n";
135 print '# $c == ', $c, "\n";
143 "on :utf8 streams syswrite() should work on characters, not bytes");
145 open G, "<:utf8", "b";
149 ($c = sysread(G, $b, 1)) == 1 &&
151 ord($b) == ord($_) &&
152 systell(G) == ($a += bytes::length($_))
154 print '# ord($_) == ', ord($_), "\n";
155 print '# ord($b) == ', ord($b), "\n";
156 print '# length($b) == ', length($b), "\n";
157 print '# bytes::length($b) == ', bytes::length($b), "\n";
158 print '# systell(G) == ', systell(G), "\n";
159 print '# $a == ', $a, "\n";
160 print '# $c == ', $c, "\n";
167 "checking syswrite() output on :utf8 streams by reading it back in");
171 1 while unlink "utf8";
176 # the test cases beyond __DATA__ need to be executed separately
179 $ENV{LC_ALL} = 'nonexistent.euc';
180 eval { open::_get_locale_encoding() };
181 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
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';
188 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
191 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1