Move autouse from lib to ext
[p5sagit/p5-mst-13.2.git] / ext / autouse / t / autouse.t
1 #!./perl
2
3 BEGIN {
4     require Config;
5     if ($Config::Config{'extensions'} !~ m!\bList/Util\b!){
6         print "1..0 # Skip -- Perl configured without List::Util module\n";
7         exit 0;
8     }
9 }
10
11 use Test;
12 BEGIN { plan tests => 12; }
13
14 BEGIN {
15     require autouse;
16     eval {
17         "autouse"->import('List::Util' => 'List::Util::first(&@)');
18     };
19     ok( !$@ );
20
21     eval {
22         "autouse"->import('List::Util' => 'Foo::min');
23     };
24     ok( $@, qr/^autouse into different package attempted/ );
25
26     "autouse"->import('List::Util' => qw(max first(&@)));
27 }
28
29 my @a = (1,2,3,4,5.5);
30 ok( max(@a), 5.5);
31
32
33 # first() has a prototype of &@.  Make sure that's preserved.
34 ok( (first { $_ > 3 } @a), 4);
35
36
37 # Example from the docs.
38 use autouse 'Carp' => qw(carp croak);
39
40 {
41     my @warning;
42     local $SIG{__WARN__} = sub { push @warning, @_ };
43     carp "this carp was predeclared and autoused\n";
44     ok( scalar @warning, 1 );
45     ok( $warning[0], qr/^this carp was predeclared and autoused\n/ );
46
47     eval { croak "It is but a scratch!" };
48     ok( $@, qr/^It is but a scratch!/);
49 }
50
51
52 # Test that autouse's lazy module loading works.
53 use autouse 'Errno' => qw(EPERM);
54
55 my $mod_file = 'Errno.pm';   # just fine and portable for %INC
56 ok( !exists $INC{$mod_file} );
57 ok( EPERM ); # test if non-zero
58 ok( exists $INC{$mod_file} );
59
60 use autouse Env => "something";
61 eval { something() };
62 ok( $@, qr/^\Qautoused module Env has unique import() method/ );
63
64 # Check that UNIVERSAL.pm doesn't interfere with modules that don't use
65 # Exporter and have no import() of their own.
66 require UNIVERSAL;
67 autouse->import("Class::ISA" => 'self_and_super_versions');
68 my %versions = self_and_super_versions("Class::ISA");
69 ok( $versions{"Class::ISA"}, $Class::ISA::VERSION );