Upgrade to podlators 1.13.
[p5sagit/p5-mst-13.2.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;
e8c9ad1b 7}
8
1e616cf5 9use Test::More tests => 13;
e8c9ad1b 10
11# open::import expects 'open' as its first argument, but it clashes with open()
12sub import {
13 open::import( 'open', @_ );
14}
15
16# can't use require_ok() here, with a name like 'open'
217f68ed 17ok( require 'open.pm', 'requiring open' );
e8c9ad1b 18
19# this should fail
20eval { import() };
217f68ed 21like( $@, qr/needs explicit list of disciplines/,
22 'import should fail without args' );
e8c9ad1b 23
24# the hint bits shouldn't be set yet
217f68ed 25is( $^H & $open::hint_bits, 0,
26 'hint bits should not be set in $^H before open import' );
e8c9ad1b 27
28# prevent it from loading I18N::Langinfo, so we can test encoding failures
29local @INC;
217f68ed 30$ENV{LC_ALL} = $ENV{LANG} = '';
e8c9ad1b 31eval { import( 'IN', 'locale' ) };
217f68ed 32like( $@, qr/Cannot figure out an encoding/,
33 'no encoding should be found without $ENV{LANG} or $ENV{LC_ALL}' );
e8c9ad1b 34
35my $warn;
36local $SIG{__WARN__} = sub {
37 $warn .= shift;
38};
39
40# and it shouldn't be able to find this discipline
41eval{ import( 'IN', 'macguffin' ) };
217f68ed 42like( $warn, qr/Unknown discipline layer/,
43 'should warn about unknown discipline with bad discipline provided' );
e8c9ad1b 44
45# now load a real-looking locale
46$ENV{LC_ALL} = ' .utf8';
47import( 'IN', 'locale' );
1e616cf5 48is( ${^OPEN}, ":utf8\0",
217f68ed 49 'should set a valid locale layer' );
e8c9ad1b 50
51# and see if it sets the magic variables appropriately
52import( 'IN', ':crlf' );
217f68ed 53ok( $^H & $open::hint_bits,
54 'hint bits should be set in $^H after open import' );
55is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
e8c9ad1b 56
57# it should reset them appropriately, too
58import( 'IN', ':raw' );
217f68ed 59is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
e8c9ad1b 60
1e616cf5 61# it dies if you don't set IN, OUT, or IO
e8c9ad1b 62eval { import( 'sideways', ':raw' ) };
217f68ed 63like( $@, qr/Unknown discipline class/, 'should croak with unknown class' );
e8c9ad1b 64
65# but it handles them all so well together
1e616cf5 66import( 'IO', ':raw :crlf' );
67is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
217f68ed 68 'should set multi types, multi disciplines' );
1e616cf5 69is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
e8c9ad1b 70
bbd5c0f5 71SKIP: {
72 skip("no perlio, no :utf8", 1) unless $Config{'useperlio'};
1e616cf5 73# the special :utf8 layer
bbd5c0f5 74 use open ':utf8';
75 open(O, ">utf8");
76 print O chr(0x100);
77 close O;
78 open(I, "<utf8");
79 is(ord(<I>), 0x100, ":utf8");
80 close I;
81}
82
83END {
84 1 while unlink "utf8";
85}
1e616cf5 86
87# the test cases beyond __DATA__ need to be executed separately
88
89__DATA__
e8c9ad1b 90$ENV{LC_ALL} = 'nonexistent.euc';
91eval { open::_get_locale_encoding() };
217f68ed 92like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
1e616cf5 93%%%
94# the special :locale layer
95$ENV{LANG} = 'ru_RU.KOI8-R';
dbd62f41 96# the :locale will probe the locale environment variables like LANG
97use open OUT => ':locale';
1e616cf5 98open(O, ">koi8");
23bcb45a 99print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
1e616cf5 100close O;
101open(I, "<koi8");
23bcb45a 102printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
1e616cf5 103close I;
104%%%