Nuke also the LANG because of glibc.
[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 => 12;
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', 'require' );
17
18 # this should fail
19 eval { import() };
20 like( $@, qr/needs explicit list of disciplines/, 'import fails without args' );
21
22 # the hint bits shouldn't be set yet
23 is( $^H & $open::hint_bits, 0, '$^H is okay before open import runs' );
24
25 # prevent it from loading I18N::Langinfo, so we can test encoding failures
26 local @INC;
27 undef @ENV{qw(LC_ALL LANG LANGUAGE)};
28 eval { import( 'IN', 'locale' ) };
29 like( $@, qr/Cannot figure out an encoding/, 'no encoding found' );
30
31 my $warn;
32 local $SIG{__WARN__} = sub {
33         $warn .= shift;
34 };
35
36 # and it shouldn't be able to find this discipline
37 eval{ import( 'IN', 'macguffin' ) };
38 like( $warn, qr/Unknown discipline layer/, 'warned about unknown discipline' );
39
40 # now load a real-looking locale
41 $ENV{LC_ALL} = ' .utf8';
42 import( 'IN', 'locale' );
43 is( ${^OPEN}, ':utf8\0', 'set locale layer' );
44
45 # and see if it sets the magic variables appropriately
46 import( 'IN', ':crlf' );
47 ok( $^H & $open::hint_bits, '$^H is set after open import runs' );
48 is( $^H{'open_IN'}, 'crlf', 'set crlf layer' );
49
50 # it should reset them appropriately, too
51 import( 'IN', ':raw' );
52 is( $^H{'open_IN'}, 'raw', 'set raw layer' );
53
54 # it dies if you don't set IN, OUT, or INOUT
55 eval { import( 'sideways', ':raw' ) };
56 like( $@, qr/Unknown discipline class/, 'croaked with unknown class' );
57
58 # but it handles them all so well together
59 import( 'INOUT', ':raw :crlf' );
60 is( ${^OPEN}, ':raw :crlf\0:raw :crlf', 'multi types, multi disciplines' );
61 is( $^H{'open_INOUT'}, 'crlf', 'last layer set in %^H' );
62
63 __END__
64 # this one won't run as $locale_encoding is already set
65 # perhaps qx{} it, if it's important to run
66 $ENV{LC_ALL} = 'nonexistent.euc';
67 eval { open::_get_locale_encoding() };
68 like( $@, qr/too ambiguous/, 'died with ambiguous locale encoding' );