Commit | Line | Data |
e8c9ad1b |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; |
bbd5c0f5 |
6 | require Config; import Config; |
e8c9ad1b |
7 | } |
8 | |
1e616cf5 |
9 | use Test::More tests => 13; |
e8c9ad1b |
10 | |
11 | # open::import expects 'open' as its first argument, but it clashes with open() |
12 | sub import { |
13 | open::import( 'open', @_ ); |
14 | } |
15 | |
16 | # can't use require_ok() here, with a name like 'open' |
217f68ed |
17 | ok( require 'open.pm', 'requiring open' ); |
e8c9ad1b |
18 | |
19 | # this should fail |
20 | eval { import() }; |
217f68ed |
21 | like( $@, qr/needs explicit list of disciplines/, |
22 | 'import should fail without args' ); |
e8c9ad1b |
23 | |
24 | # the hint bits shouldn't be set yet |
217f68ed |
25 | is( $^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 |
29 | local @INC; |
217f68ed |
30 | $ENV{LC_ALL} = $ENV{LANG} = ''; |
e8c9ad1b |
31 | eval { import( 'IN', 'locale' ) }; |
217f68ed |
32 | like( $@, qr/Cannot figure out an encoding/, |
33 | 'no encoding should be found without $ENV{LANG} or $ENV{LC_ALL}' ); |
e8c9ad1b |
34 | |
35 | my $warn; |
36 | local $SIG{__WARN__} = sub { |
37 | $warn .= shift; |
38 | }; |
39 | |
40 | # and it shouldn't be able to find this discipline |
41 | eval{ import( 'IN', 'macguffin' ) }; |
217f68ed |
42 | like( $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'; |
47 | import( 'IN', 'locale' ); |
1e616cf5 |
48 | is( ${^OPEN}, ":utf8\0", |
217f68ed |
49 | 'should set a valid locale layer' ); |
e8c9ad1b |
50 | |
51 | # and see if it sets the magic variables appropriately |
52 | import( 'IN', ':crlf' ); |
217f68ed |
53 | ok( $^H & $open::hint_bits, |
54 | 'hint bits should be set in $^H after open import' ); |
55 | is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' ); |
e8c9ad1b |
56 | |
57 | # it should reset them appropriately, too |
58 | import( 'IN', ':raw' ); |
217f68ed |
59 | is( $^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 |
62 | eval { import( 'sideways', ':raw' ) }; |
217f68ed |
63 | like( $@, qr/Unknown discipline class/, 'should croak with unknown class' ); |
e8c9ad1b |
64 | |
65 | # but it handles them all so well together |
1e616cf5 |
66 | import( 'IO', ':raw :crlf' ); |
67 | is( ${^OPEN}, ":raw :crlf\0:raw :crlf", |
217f68ed |
68 | 'should set multi types, multi disciplines' ); |
1e616cf5 |
69 | is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' ); |
e8c9ad1b |
70 | |
bbd5c0f5 |
71 | SKIP: { |
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 | |
83 | END { |
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'; |
91 | eval { open::_get_locale_encoding() }; |
217f68ed |
92 | like( $@, 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 |
97 | use open OUT => ':locale'; |
1e616cf5 |
98 | open(O, ">koi8"); |
23bcb45a |
99 | print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1 |
1e616cf5 |
100 | close O; |
101 | open(I, "<koi8"); |
23bcb45a |
102 | printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1 |
1e616cf5 |
103 | close I; |
104 | %%% |