This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge branch 'vlb' into blead
[perl5.git] / lib / open.t
CommitLineData
e8c9ad1b 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
bbd5c0f5 6 require Config; import Config;
879ced66 7 require './test.pl';
f2270484 8 require './charset_tools.pl';
e8c9ad1b 9}
10
1ed4b776 11plan 11;
e8c9ad1b 12
13# open::import expects 'open' as its first argument, but it clashes with open()
14sub import {
15 open::import( 'open', @_ );
16}
17
18# can't use require_ok() here, with a name like 'open'
217f68ed 19ok( require 'open.pm', 'requiring open' );
e8c9ad1b 20
21# this should fail
22eval { import() };
7f17c514 23like( $@, qr/needs explicit list of PerlIO layers/,
217f68ed 24 'import should fail without args' );
e8c9ad1b 25
e8c9ad1b 26# prevent it from loading I18N::Langinfo, so we can test encoding failures
e8c9ad1b 27my $warn;
28local $SIG{__WARN__} = sub {
29 $warn .= shift;
30};
31
7f17c514 32# and it shouldn't be able to find this layer
99ef548b
PM
33$warn = '';
34eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
35is( $warn, '',
7f17c514 36 'should not warn about unknown layer with bad layer provided' );
99ef548b
PM
37
38$warn = '';
39eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
7f17c514
RGS
40like( $warn, qr/Unknown PerlIO layer/,
41 'should warn about unknown layer with bad layer provided' );
e8c9ad1b 42
7c0e976d
JH
43# open :locale logic changed since open 1.04, new logic
44# difficult to test portably.
e8c9ad1b 45
7c0e976d 46# see if it sets the magic variables appropriately
e8c9ad1b 47import( 'IN', ':crlf' );
217f68ed 48is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
e8c9ad1b 49
50# it should reset them appropriately, too
51import( 'IN', ':raw' );
217f68ed 52is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
e8c9ad1b 53
1e616cf5 54# it dies if you don't set IN, OUT, or IO
e8c9ad1b 55eval { import( 'sideways', ':raw' ) };
7f17c514 56like( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' );
e8c9ad1b 57
58# but it handles them all so well together
1e616cf5
JH
59import( 'IO', ':raw :crlf' );
60is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
7f17c514 61 'should set multi types, multi layer' );
1e616cf5 62is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
e8c9ad1b 63
bbd5c0f5 64SKIP: {
e1c247fd
KW
65 skip("no perlio", 1) unless (find PerlIO::Layer 'perlio');
66 skip("no Encode", 1) unless $Config{extensions} =~ m{\bEncode\b};
a6431fd1
KW
67 skip("EBCDIC platform doesnt have 'use encoding' used by open ':locale'", 1)
68 if $::IS_EBCDIC;
e111333b 69
c0abe5aa
RGS
70 eval q[use Encode::Alias;use open ":std", ":locale"];
71 is($@, '', 'can use :std and :locale');
d7a09b41
SR
72}
73
f76f2ef3
TC
74{
75 local $ENV{PERL_UNICODE};
76 delete $ENV{PERL_UNICODE};
7a7edf4a
KW
77 local $TODO;
78 $TODO = "Encode not working on EBCDIC" if $::IS_EBCDIC;
f76f2ef3
TC
79 is runperl(
80 progs => [
81 'use open q\:encoding(UTF-8)\, q-:std-;',
82 'use open q\:encoding(UTF-8)\;',
83 'if(($_ = <STDIN>) eq qq-\x{100}\n-) { print qq-stdin ok\n- }',
84 'else { print qq-got -, join(q q q, map ord, split//), "\n" }',
f2270484
KW
85 'print STDOUT qq-\x{fe}\n-;',
86 'print STDERR qq-\x{fe}\n-;',
f76f2ef3 87 ],
f2270484 88 stdin => byte_utf8a_to_utf8n("\xc4\x80") . "\n",
f76f2ef3
TC
89 stderr => 1,
90 ),
f2270484
KW
91 "stdin ok\n"
92 . byte_utf8a_to_utf8n("\xc3\xbe")
93 . "\n"
94 . byte_utf8a_to_utf8n("\xc3\xbe")
95 . "\n",
f76f2ef3
TC
96 "use open without :std does not affect standard handles",
97 ;
98}
73f1eaca 99
bbd5c0f5
JH
100END {
101 1 while unlink "utf8";
e111333b
JH
102 1 while unlink "a";
103 1 while unlink "b";
bbd5c0f5 104}
1e616cf5
JH
105
106# the test cases beyond __DATA__ need to be executed separately
107
108__DATA__
e8c9ad1b 109$ENV{LC_ALL} = 'nonexistent.euc';
110eval { open::_get_locale_encoding() };
217f68ed 111like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
1e616cf5
JH
112%%%
113# the special :locale layer
b429a72e 114$ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
dbd62f41
JH
115# the :locale will probe the locale environment variables like LANG
116use open OUT => ':locale';
1e616cf5 117open(O, ">koi8");
23bcb45a 118print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
1e616cf5
JH
119close O;
120open(I, "<koi8");
23bcb45a 121printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
1e616cf5
JH
122close I;
123%%%