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