Re: @Config -"des" not quite working on VMS
[p5sagit/p5-mst-13.2.git] / lib / open.t
1 #!./perl
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6 }
7
8 use Test::More tests => 13;
9
10 # open::import expects 'open' as its first argument, but it clashes with open()
11 sub import {
12         open::import( 'open', @_ );
13 }
14
15 # can't use require_ok() here, with a name like 'open'
16 ok( require 'open.pm', 'requiring open' );
17
18 # this should fail
19 eval { import() };
20 like( $@, qr/needs explicit list of disciplines/, 
21         'import should fail without args' );
22
23 # the hint bits shouldn't be set yet
24 is( $^H & $open::hint_bits, 0, 
25         'hint bits should not be set in $^H before open import' );
26
27 # prevent it from loading I18N::Langinfo, so we can test encoding failures
28 local @INC;
29 $ENV{LC_ALL} = $ENV{LANG} = '';
30 eval { import( 'IN', 'locale' ) };
31 like( $@, qr/Cannot figure out an encoding/, 
32         'no encoding should be found without $ENV{LANG} or $ENV{LC_ALL}' );
33
34 my $warn;
35 local $SIG{__WARN__} = sub {
36         $warn .= shift;
37 };
38
39 # and it shouldn't be able to find this discipline
40 eval{ import( 'IN', 'macguffin' ) };
41 like( $warn, qr/Unknown discipline layer/, 
42         'should warn about unknown discipline with bad discipline provided' );
43
44 # now load a real-looking locale
45 $ENV{LC_ALL} = ' .utf8';
46 import( 'IN', 'locale' );
47 is( ${^OPEN}, ":utf8\0", 
48         'should set a valid locale layer' );
49
50 # and see if it sets the magic variables appropriately
51 import( 'IN', ':crlf' );
52 ok( $^H & $open::hint_bits, 
53         'hint bits should be set in $^H after open import' );
54 is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
55
56 # it should reset them appropriately, too
57 import( 'IN', ':raw' );
58 is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
59
60 # it dies if you don't set IN, OUT, or IO
61 eval { import( 'sideways', ':raw' ) };
62 like( $@, qr/Unknown discipline class/, 'should croak with unknown class' );
63
64 # but it handles them all so well together
65 import( 'IO', ':raw :crlf' );
66 is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
67         'should set multi types, multi disciplines' );
68 is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
69
70 # the special :utf8 layer
71 use open ':utf8';
72 open(O, ">utf8");
73 print O chr(0x100);
74 close O;
75 open(I, "<utf8");
76 is(ord(<I>), 0x100, ":utf8");
77 close I;
78
79 # the test cases beyond __DATA__ need to be executed separately
80
81 __DATA__
82 $ENV{LC_ALL} = 'nonexistent.euc';
83 eval { open::_get_locale_encoding() };
84 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
85 %%%
86 # the special :locale layer
87 $ENV{LANG} = 'ru_RU.KOI8-R';
88 # the :locale will probe the locale environment variables like LANG
89 use open OUT => ':locale';
90 open(O, ">koi8");
91 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
92 close O;
93 open(I, "<koi8");
94 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
95 close I;
96 %%%