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