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