Re: [PATCH lib/autouse.pm t/pragma/autouse.t] (was Re: [ID 20010528.001] use autouse...
[p5sagit/p5-mst-13.2.git] / t / pragma / autouse.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Test;
9 BEGIN { plan tests => 10; }
10
11 BEGIN {
12     require autouse;
13     eval {
14         "autouse"->import('List::Util' => 'List::Util::first(&@)');
15     };
16     ok( !$@ );
17
18     eval {
19         "autouse"->import('List::Util' => 'Foo::min');
20     };
21     ok( $@, qr/^autouse into different package attempted/ );
22
23     "autouse"->import('List::Util' => qw(max first(&@)));
24 }
25
26 my @a = (1,2,3,4,5.5);
27 ok( max(@a), 5.5);
28
29
30 # first() has a prototype of &@.  Make sure that's preserved.
31 ok( (first { $_ > 3 } @a), 4);
32
33
34 # Example from the docs.
35 use autouse 'Carp' => qw(carp croak);
36
37 {
38     my @warning;
39     local $SIG{__WARN__} = sub { push @warning, @_ };
40     carp "this carp was predeclared and autoused\n";
41     ok( scalar @warning, 1 );
42     ok( $warning[0], "this carp was predeclared and autoused\n" );
43
44     eval { croak "It is but a scratch!" };
45     ok( $@, qr/^It is but a scratch!/);
46 }
47
48
49 # Test that autouse's lazy module loading works.  We assume that nothing
50 # involved in this test uses Text::Soundex, which is pretty safe.
51 use File::Spec;
52 use autouse 'Text::Soundex' => qw(soundex);
53
54 my $mod_file = File::Spec->catfile(qw(Text Soundex.pm));
55 $mod_file = VMS::Filespec::unixify($mod_file) if $^O eq 'VMS';
56 ok( !exists $INC{$mod_file} );
57 ok( soundex('Basset'), 'B230' );
58 ok( exists $INC{$mod_file} );
59