This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cleanup utf8_heavy; allow dropping the In prefix from
[perl5.git] / lib / open.t
CommitLineData
e8c9ad1b 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use Test::More tests => 12;
9
10# open::import expects 'open' as its first argument, but it clashes with open()
11sub import {
12 open::import( 'open', @_ );
13}
14
15# can't use require_ok() here, with a name like 'open'
16ok( require 'open.pm', 'required okay!' );
17
18# this should fail
19eval { import() };
20like( $@, qr/needs explicit list of disciplines/, 'import fails without args' );
21
22# the hint bits shouldn't be set yet
23is( $^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
26local @INC;
27$ENV{LC_ALL} = '';
28eval { import( 'IN', 'locale' ) };
29like( $@, qr/Cannot figure out an encoding/, 'no encoding found' );
30
31my $warn;
32local $SIG{__WARN__} = sub {
33 $warn .= shift;
34};
35
36# and it shouldn't be able to find this discipline
37eval{ import( 'IN', 'macguffin' ) };
38like( $warn, qr/Unknown discipline layer/, 'warned about unknown discipline' );
39
40# now load a real-looking locale
41$ENV{LC_ALL} = ' .utf8';
42import( 'IN', 'locale' );
43is( ${^OPEN}, ':utf8\0', 'set locale layer okay!' );
44
45# and see if it sets the magic variables appropriately
46import( 'IN', ':crlf' );
47ok( $^H & $open::hint_bits, '$^H is set after open import runs' );
48is( $^H{'open_IN'}, 'crlf', 'set crlf layer okay!' );
49
50# it should reset them appropriately, too
51import( 'IN', ':raw' );
52is( $^H{'open_IN'}, 'raw', 'set raw layer okay!' );
53
54# it dies if you don't set IN, OUT, or INOUT
55eval { import( 'sideways', ':raw' ) };
56like( $@, qr/Unknown discipline class/, 'croaked with unknown class' );
57
58# but it handles them all so well together
59import( 'INOUT', ':raw :crlf' );
60is( ${^OPEN}, ':raw :crlf\0:raw :crlf', 'multi types, multi disciplines' );
61is( $^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';
67eval { open::_get_locale_encoding() };
68like( $@, qr/too ambiguous/, 'died with ambiguous locale encoding' );