xref: /openbsd/gnu/usr.bin/perl/dist/autouse/t/autouse.t (revision 8529ddd3)
1#!./perl
2
3BEGIN {
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
11use Test::More tests => 15;
12
13BEGIN {
14    require autouse;
15    eval {
16        "autouse"->import('Scalar::Util' => 'Scalar::Util::set_prototype(&$)');
17    };
18    ok( !$@, "Function from package with custom 'import()' correctly imported" );
19
20    eval {
21        "autouse"->import('Scalar::Util' => 'Foo::min');
22    };
23    ok( $@, qr/^autouse into different package attempted/ );
24
25    "autouse"->import('Scalar::Util' => qw(isdual set_prototype(&$)));
26}
27
28ok( isdual($!),
29    "Function imported via 'autouse' performs as expected");
30
31
32# set_prototype() has a prototype of &$.  Make sure that's preserved.
33sub sum { return $_[0] + $_[1] };
34is( (set_prototype \&sum, '$$'), \&sum,
35    "Subroutine prototype preserved after import via 'autouse'");
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";
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" );
48
49    eval { croak "It is but a scratch!" };
50    like( $@, qr/^It is but a scratch!/,
51        "Failure message received as expected" );
52}
53
54
55# Test that autouse's lazy module loading works.
56use autouse 'Errno' => qw(EPERM);
57
58my $mod_file = 'Errno.pm';   # just fine and portable for %INC
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" );
62
63use autouse Env => "something";
64eval { something() };
65like( $@, qr/^\Qautoused module Env has unique import() method/,
66    "Module with unique import() method detected and error reported" );
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;
71require File::Spec;
72unshift @INC, File::Spec->catdir('t', 'lib'), 'lib';
73autouse->import("MyTestModule" => 'test_function');
74my $ret = test_function();
75is( $ret, 'works', "No interference from UNIVERSAL.pm" );
76
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;
81    use warnings; local $^W = 1; no warnings 'once';
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/;
99    use warnings; local $^W = 1; no warnings 'once';
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}
108