178064Sume#!./perl
262627Skris
355163SshinBEGIN {
455163Sshin	chdir 't' if -d 't';
555163Sshin	@INC = '../lib';
655163Sshin	require Config; import Config;
755163Sshin	require './test.pl';
855163Sshin	require './charset_tools.pl';
955163Sshin}
1055163Sshin
1155163Sshinplan 11;
1255163Sshin
1355163Sshin# open::import expects 'open' as its first argument, but it clashes with open()
1455163Sshinsub import {
1555163Sshin	open::import( 'open', @_ );
1655163Sshin}
1755163Sshin
1855163Sshin# can't use require_ok() here, with a name like 'open'
1955163Sshinok( require 'open.pm', 'requiring open' );
2055163Sshin
2155163Sshin# this should fail
2255163Sshineval { import() };
2355163Sshinlike( $@, qr/needs explicit list of PerlIO layers/,
2455163Sshin	'import should fail without args' );
2555163Sshin
2655163Sshin# prevent it from loading I18N::Langinfo, so we can test encoding failures
2755163Sshinmy $warn;
2855163Sshinlocal $SIG{__WARN__} = sub {
2955163Sshin	$warn .= shift;
3055163Sshin};
3155163Sshin
3255163Sshin# and it shouldn't be able to find this layer
3355163Sshin$warn = '';
3455163Sshineval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
3555163Sshinis( $warn, '',
3655163Sshin	'should not warn about unknown layer with bad layer provided' );
3755163Sshin
3855163Sshin$warn = '';
3955163Sshineval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
4055163Sshinlike( $warn, qr/Unknown PerlIO layer/,
4155163Sshin	'should warn about unknown layer with bad layer provided' );
4255163Sshin
4355163Sshin# open :locale logic changed since open 1.04, new logic
4455163Sshin# difficult to test portably.
4555163Sshin
4655163Sshin# see if it sets the magic variables appropriately
4755163Sshinimport( 'IN', ':crlf' );
4855163Sshinis( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
4955163Sshin
5055163Sshin# it should reset them appropriately, too
5155163Sshinimport( 'IN', ':raw' );
5255163Sshinis( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
5355163Sshin
5455163Sshin# it dies if you don't set IN, OUT, or IO
5555163Sshineval { import( 'sideways', ':raw' ) };
5655163Sshinlike( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' );
5755163Sshin
5855163Sshin# but it handles them all so well together
5955163Sshinimport( 'IO', ':raw :crlf' );
6055163Sshinis( ${^OPEN}, ":raw :crlf\0:raw :crlf",
6155163Sshin	'should set multi types, multi layer' );
6255163Sshinis( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
6355163Sshin
6455163SshinSKIP: {
6555163Sshin    skip("no perlio", 1) unless (find PerlIO::Layer 'perlio');
6655163Sshin    skip("no Encode", 1) unless $Config{extensions} =~ m{\bEncode\b};
6755163Sshin    skip("EBCDIC platform doesnt have 'use encoding' used by open ':locale'", 1)
6855163Sshin                                                                if $::IS_EBCDIC;
6955163Sshin
7055163Sshin    eval q[use Encode::Alias;use open ":std", ":locale"];
7162627Skris    is($@, '', 'can use :std and :locale');
7255163Sshin}
7355163Sshin
7455163Sshin{
7555163Sshin    local $ENV{PERL_UNICODE};
7655163Sshin    delete $ENV{PERL_UNICODE};
7755163Sshin    local $TODO;
7855163Sshin    $TODO = "Encode not working on EBCDIC" if $::IS_EBCDIC;
7955163Sshin    is runperl(
8062627Skris         progs => [
8162627Skris            'use open q\:encoding(UTF-8)\, q-:std-;',
8255163Sshin            'use open q\:encoding(UTF-8)\;',
8355163Sshin            'if(($_ = <STDIN>) eq qq-\x{100}\n-) { print qq-stdin ok\n- }',
8455163Sshin            'else { print qq-got -, join(q q q, map ord, split//), "\n" }',
8555163Sshin            'print STDOUT qq-\x{fe}\n-;',
8655163Sshin            'print STDERR qq-\x{fe}\n-;',
8755163Sshin         ],
8855163Sshin         stdin => byte_utf8a_to_utf8n("\xc4\x80") . "\n",
8955163Sshin         stderr => 1,
9055163Sshin       ),
9155163Sshin       "stdin ok\n"
9255163Sshin        . byte_utf8a_to_utf8n("\xc3\xbe")
9355163Sshin        . "\n"
9455163Sshin        . byte_utf8a_to_utf8n("\xc3\xbe")
9555163Sshin        . "\n",
9655163Sshin       "use open without :std does not affect standard handles",
9755163Sshin    ;
9855163Sshin}
9955163Sshin
10055163SshinEND {
10155163Sshin    1 while unlink "utf8";
10278064Sume    1 while unlink "a";
10378064Sume    1 while unlink "b";
10455163Sshin}
10555163Sshin
10655163Sshin# the test cases beyond __DATA__ need to be executed separately
10755163Sshin
10855163Sshin__DATA__
10955163Sshin$ENV{LC_ALL} = 'nonexistent.euc';
11055163Sshineval { open::_get_locale_encoding() };
11155163Sshinlike( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
11255163Sshin%%%
11355163Sshin# the special :locale layer
11455163Sshin$ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
11555163Sshin# the :locale will probe the locale environment variables like LANG
11655163Sshinuse open OUT => ':locale';
11755163Sshinopen(O, ">koi8");
11855163Sshinprint O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
11978064Sumeclose O;
12055163Sshinopen(I, "<koi8");
12155163Sshinprintf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
12255163Sshinclose I;
12355163Sshin%%%
12455163Sshin