xref: /openbsd/gnu/usr.bin/perl/dist/autouse/t/autouse.t (revision d415bd75)
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
11my ($ok1, $ok2);
12BEGIN {
13    require autouse;
14    eval {
15        "autouse"->import('Scalar::Util' => 'Scalar::Util::set_prototype(&$)');
16    };
17    $ok1 = !$@;
18
19    eval {
20        "autouse"->import('Scalar::Util' => 'Foo::min');
21    };
22    $ok2 = $@;
23
24    "autouse"->import('Scalar::Util' => qw(isdual set_prototype(&$)));
25}
26
27use Test::More tests => 15;
28
29ok( $ok1, "Function from package with custom 'import()' correctly imported" );
30like( $ok2, qr/^autouse into different package attempted/, "Catch autouse into different package" );
31
32ok( isdual($!),
33    "Function imported via 'autouse' performs as expected");
34
35
36# set_prototype() has a prototype of &$.  Make sure that's preserved.
37sub sum { return $_[0] + $_[1] };
38is( (set_prototype \&sum, '$$'), \&sum,
39    "Subroutine prototype preserved after import via 'autouse'");
40
41
42# Example from the docs.
43use autouse 'Carp' => qw(carp croak);
44
45{
46    my @warning;
47    local $SIG{__WARN__} = sub { push @warning, @_ };
48    carp "this carp was predeclared and autoused\n";
49    is( scalar @warning, 1, "Expected number of warnings received" );
50    like( $warning[0], qr/^this carp was predeclared and autoused\n/,
51        "Warning received as expected" );
52
53    eval { croak "It is but a scratch!" };
54    like( $@, qr/^It is but a scratch!/,
55        "Failure message received as expected" );
56}
57
58
59# Test that autouse's lazy module loading works.
60use autouse 'Errno' => qw(EPERM);
61
62my $mod_file = 'Errno.pm';   # just fine and portable for %INC
63ok( !exists $INC{$mod_file}, "Module not yet loaded" );
64ok( EPERM, "Access a constant from that module" ); # test if non-zero
65ok( exists $INC{$mod_file}, "Module has been lazily loaded" );
66
67use autouse Env => "something";
68eval { something() };
69like( $@, qr/^\Qautoused module Env has unique import() method/,
70    "Module with unique import() method detected and error reported" );
71
72# Check that UNIVERSAL.pm doesn't interfere with modules that don't use
73# Exporter and have no import() of their own.
74require UNIVERSAL;
75require File::Spec;
76unshift @INC, File::Spec->catdir('t', 'lib'), 'lib';
77autouse->import("MyTestModule" => 'test_function');
78my $ret = test_function();
79is( $ret, 'works', "No interference from UNIVERSAL.pm" );
80
81# Test that autouse is exempt from all methods of triggering the subroutine
82# redefinition warning.
83SKIP: {
84    skip "Fails in 5.15.5 and below (perl bug)", 2 if $] < 5.0150051;
85    use warnings; local $^W = 1; no warnings 'once';
86    my $w;
87    local $SIG{__WARN__} = sub { $w .= shift };
88    use autouse MyTestModule2 => 'test_function2';
89    *MyTestModule2::test_function2 = \&test_function2;
90    require MyTestModule2;
91    is $w, undef,
92       'no redefinition warning when clobbering autouse stub with new sub';
93    undef $w;
94    import MyTestModule2 'test_function2';
95    is $w, undef,
96       'no redefinition warning when clobbering autouse stub via *a=\&b';
97}
98SKIP: {
99    skip "Fails in 5.15.5 and below (perl bug)", 1 if $] < 5.0150051;
100    use Config;
101    skip "no Hash::Util", 1 unless $Config{extensions} =~ /\bHash::Util\b/;
102    use warnings; local $^W = 1; no warnings 'once';
103    my $w;
104    local $SIG{__WARN__} = sub { $w .= shift };
105    # any old XS sub from any old module which uses Exporter
106    use autouse 'Hash::Util' => "all_keys";
107    *Hash::Util::all_keys = \&all_keys;
108    require Hash::Util;
109    is $w, undef,
110      'no redefinition warning when clobbering autouse stub with new XSUB';
111}
112