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