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