This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix legacy VMS feature checking status.
[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 {
aa86db36 16 "autouse"->import('Scalar::Util' => 'Scalar::Util::set_prototype(&$)');
5a02ccb1 17 };
0c6e98c7 18 ok( !$@, "Function from package with custom 'import()' correctly imported" );
5a02ccb1
MS
19
20 eval {
aa86db36 21 "autouse"->import('Scalar::Util' => 'Foo::min');
82d4508f
MS
22 };
23 ok( $@, qr/^autouse into different package attempted/ );
24
aa86db36 25 "autouse"->import('Scalar::Util' => qw(isdual set_prototype(&$)));
a6c2ede0
JH
26}
27
0c6e98c7
JK
28ok( isdual($!),
29 "Function imported via 'autouse' performs as expected");
a6c2ede0 30
a6c2ede0 31
aa86db36
SH
32# set_prototype() has a prototype of &$. Make sure that's preserved.
33sub sum { return $_[0] + $_[1] };
0c6e98c7
JK
34is( (set_prototype \&sum, '$$'), \&sum,
35 "Subroutine prototype preserved after import via 'autouse'");
82d4508f
MS
36
37
38# Example from the docs.
39use autouse 'Carp' => qw(carp croak);
40
41{
42 my @warning;
43 local $SIG{__WARN__} = sub { push @warning, @_ };
44 carp "this carp was predeclared and autoused\n";
0c6e98c7
JK
45 is( scalar @warning, 1, "Expected number of warnings received" );
46 like( $warning[0], qr/^this carp was predeclared and autoused\n/,
47 "Warning received as expected" );
82d4508f
MS
48
49 eval { croak "It is but a scratch!" };
0c6e98c7
JK
50 like( $@, qr/^It is but a scratch!/,
51 "Failure message received as expected" );
82d4508f
MS
52}
53
a6c2ede0 54
43bd726c
JH
55# Test that autouse's lazy module loading works.
56use autouse 'Errno' => qw(EPERM);
a6c2ede0 57
43bd726c 58my $mod_file = 'Errno.pm'; # just fine and portable for %INC
0c6e98c7
JK
59ok( !exists $INC{$mod_file}, "Module not yet loaded" );
60ok( EPERM, "Access a constant from that module" ); # test if non-zero
61ok( exists $INC{$mod_file}, "Module has been lazily loaded" );
a6c2ede0 62
03699e8e
MS
63use autouse Env => "something";
64eval { something() };
0c6e98c7
JK
65like( $@, qr/^\Qautoused module Env has unique import() method/,
66 "Module with unique import() method detected and error reported" );
03699e8e
MS
67
68# Check that UNIVERSAL.pm doesn't interfere with modules that don't use
69# Exporter and have no import() of their own.
70require UNIVERSAL;
00f261ef
SM
71require File::Spec;
72unshift @INC, File::Spec->catdir('t', 'lib'), 'lib';
73autouse->import("MyTestModule" => 'test_function');
74my $ret = test_function();
0c6e98c7 75is( $ret, 'works', "No interference from UNIVERSAL.pm" );
00f261ef 76
799fd3b9
FC
77# Test that autouse is exempt from all methods of triggering the subroutine
78# redefinition warning.
79SKIP: {
80 skip "Fails in 5.15.5 and below (perl bug)", 2 if $] < 5.0150051;
f965e9d4 81 use warnings; local $^W = 1; no warnings 'once';
799fd3b9
FC
82 my $w;
83 local $SIG{__WARN__} = sub { $w .= shift };
84 use autouse MyTestModule2 => 'test_function2';
85 *MyTestModule2::test_function2 = \&test_function2;
86 require MyTestModule2;
87 is $w, undef,
88 'no redefinition warning when clobbering autouse stub with new sub';
89 undef $w;
90 import MyTestModule2 'test_function2';
91 is $w, undef,
92 'no redefinition warning when clobbering autouse stub via *a=\&b';
93}
94SKIP: {
95 skip "Fails from 5.10 to 5.15.5 (perl bug)", 1
96 if $] < 5.0150051 and $] > 5.0099;
97 use Config;
98 skip "no B", 1 unless $Config{extensions} =~ /\bB\b/;
f965e9d4 99 use warnings; local $^W = 1; no warnings 'once';
799fd3b9
FC
100 my $w;
101 local $SIG{__WARN__} = sub { $w .= shift };
102 use autouse B => "sv_undef";
103 *B::sv_undef = \&sv_undef;
104 require B;
105 is $w, undef,
106 'no redefinition warning when clobbering autouse stub with new XSUB';
107}