This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Scalar-List-Utils from version 1.32 to 1.33
[perl5.git] / dist / autouse / t / autouse.t
CommitLineData
82d4508f
MS
1#!./perl
2
3BEGIN {
78cd8b71 4 require Config;
34c716a1 5 if ($Config::Config{'extensions'} !~ m!\bList/Util\b!){
78cd8b71
NC
6 print "1..0 # Skip -- Perl configured without List::Util module\n";
7 exit 0;
8 }
82d4508f
MS
9}
10
799fd3b9 11use Test::More tests => 15;
82d4508f 12
a6c2ede0 13BEGIN {
82d4508f
MS
14 require autouse;
15 eval {
5a02ccb1
MS
16 "autouse"->import('List::Util' => 'List::Util::first(&@)');
17 };
18 ok( !$@ );
19
20 eval {
21 "autouse"->import('List::Util' => 'Foo::min');
82d4508f
MS
22 };
23 ok( $@, qr/^autouse into different package attempted/ );
24
25 "autouse"->import('List::Util' => qw(max first(&@)));
a6c2ede0
JH
26}
27
82d4508f 28my @a = (1,2,3,4,5.5);
8618896a 29is( max(@a), 5.5);
a6c2ede0 30
a6c2ede0 31
82d4508f 32# first() has a prototype of &@. Make sure that's preserved.
8618896a 33is( (first { $_ > 3 } @a), 4);
82d4508f
MS
34
35
36# Example from the docs.
37use autouse 'Carp' => qw(carp croak);
38
39{
40 my @warning;
41 local $SIG{__WARN__} = sub { push @warning, @_ };
42 carp "this carp was predeclared and autoused\n";
8618896a
NC
43 is( scalar @warning, 1 );
44 like( $warning[0], qr/^this carp was predeclared and autoused\n/ );
82d4508f
MS
45
46 eval { croak "It is but a scratch!" };
8618896a 47 like( $@, qr/^It is but a scratch!/);
82d4508f
MS
48}
49
a6c2ede0 50
43bd726c
JH
51# Test that autouse's lazy module loading works.
52use autouse 'Errno' => qw(EPERM);
a6c2ede0 53
43bd726c 54my $mod_file = 'Errno.pm'; # just fine and portable for %INC
82d4508f 55ok( !exists $INC{$mod_file} );
fd9a0c67 56ok( EPERM ); # test if non-zero
82d4508f 57ok( exists $INC{$mod_file} );
a6c2ede0 58
03699e8e
MS
59use autouse Env => "something";
60eval { something() };
8618896a 61like( $@, qr/^\Qautoused module Env has unique import() method/ );
03699e8e
MS
62
63# Check that UNIVERSAL.pm doesn't interfere with modules that don't use
64# Exporter and have no import() of their own.
65require UNIVERSAL;
00f261ef
SM
66require File::Spec;
67unshift @INC, File::Spec->catdir('t', 'lib'), 'lib';
68autouse->import("MyTestModule" => 'test_function');
69my $ret = test_function();
8618896a 70is( $ret, 'works' );
00f261ef 71
799fd3b9
FC
72# Test that autouse is exempt from all methods of triggering the subroutine
73# redefinition warning.
74SKIP: {
75 skip "Fails in 5.15.5 and below (perl bug)", 2 if $] < 5.0150051;
f965e9d4 76 use warnings; local $^W = 1; no warnings 'once';
799fd3b9
FC
77 my $w;
78 local $SIG{__WARN__} = sub { $w .= shift };
79 use autouse MyTestModule2 => 'test_function2';
80 *MyTestModule2::test_function2 = \&test_function2;
81 require MyTestModule2;
82 is $w, undef,
83 'no redefinition warning when clobbering autouse stub with new sub';
84 undef $w;
85 import MyTestModule2 'test_function2';
86 is $w, undef,
87 'no redefinition warning when clobbering autouse stub via *a=\&b';
88}
89SKIP: {
90 skip "Fails from 5.10 to 5.15.5 (perl bug)", 1
91 if $] < 5.0150051 and $] > 5.0099;
92 use Config;
93 skip "no B", 1 unless $Config{extensions} =~ /\bB\b/;
f965e9d4 94 use warnings; local $^W = 1; no warnings 'once';
799fd3b9
FC
95 my $w;
96 local $SIG{__WARN__} = sub { $w .= shift };
97 use autouse B => "sv_undef";
98 *B::sv_undef = \&sv_undef;
99 require B;
100 is $w, undef,
101 'no redefinition warning when clobbering autouse stub with new XSUB';
102}