xref: /openbsd/gnu/usr.bin/perl/lib/open.t (revision 09467b48)
1#!./perl
2
3BEGIN {
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
11plan 11;
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'
19ok( require 'open.pm', 'requiring open' );
20
21# this should fail
22eval { import() };
23like( $@, 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
27my $warn;
28local $SIG{__WARN__} = sub {
29	$warn .= shift;
30};
31
32# and it shouldn't be able to find this layer
33$warn = '';
34eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
35is( $warn, '',
36	'should not warn about unknown layer with bad layer provided' );
37
38$warn = '';
39eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
40like( $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
47import( 'IN', ':crlf' );
48is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
49
50# it should reset them appropriately, too
51import( 'IN', ':raw' );
52is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
53
54# it dies if you don't set IN, OUT, or IO
55eval { import( 'sideways', ':raw' ) };
56like( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' );
57
58# but it handles them all so well together
59import( 'IO', ':raw :crlf' );
60is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
61	'should set multi types, multi layer' );
62is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
63
64SKIP: {
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
100END {
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';
110eval { open::_get_locale_encoding() };
111like( $@, 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
116use open OUT => ':locale';
117open(O, ">koi8");
118print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
119close O;
120open(I, "<koi8");
121printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
122close I;
123%%%
124