summaryrefslogtreecommitdiff
path: root/lib/AutoLoader.t
blob: f2fae7f309d835ebfe69e2383ada140ba0566e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!./perl

BEGIN {
    chdir 't' if -d 't';
    if ($^O eq 'MacOS') {
	$dir = ":auto-$$";
	$sep = ":";
    } else {
	$dir = "auto-$$";
	$sep = "/";
    }
    @INC = $dir;
    push @INC, '../lib';
}

print "1..11\n";

# First we must set up some autoloader files
mkdir $dir, 0755            or die "Can't mkdir $dir: $!";
mkdir "$dir${sep}auto", 0755     or die "Can't mkdir: $!";
mkdir "$dir${sep}auto${sep}Foo", 0755 or die "Can't mkdir: $!";

open(FOO, ">$dir${sep}auto${sep}Foo${sep}foo.al") or die;
print FOO <<'EOT';
package Foo;
sub foo { shift; shift || "foo" }
1;
EOT
close(FOO);

open(BAR, ">$dir${sep}auto${sep}Foo${sep}bar.al") or die;
print BAR <<'EOT';
package Foo;
sub bar { shift; shift || "bar" }
1;
EOT
close(BAR);

open(BAZ, ">$dir${sep}auto${sep}Foo${sep}bazmarkhian.al") or die;
print BAZ <<'EOT';
package Foo;
sub bazmarkhianish { shift; shift || "baz" }
1;
EOT
close(BAZ);

# Let's define the package
package Foo;
require AutoLoader;
@ISA=qw(AutoLoader);

sub new { bless {}, shift };

package main;

$foo = new Foo;

print "not " unless $foo->foo eq 'foo';  # autoloaded first time
print "ok 1\n";

print "not " unless $foo->foo eq 'foo';  # regular call
print "ok 2\n";

# Try an undefined method
eval {
    $foo->will_fail;
};
print "not " unless $@ =~ /^Can't locate/;
print "ok 3\n";

# Used to be trouble with this
eval {
    my $foo = new Foo;
    die "oops";
};
print "not " unless $@ =~ /oops/;
print "ok 4\n";

# Pass regular expression variable to autoloaded function.  This used
# to go wrong because AutoLoader used regular expressions to generate
# autoloaded filename.
"foo" =~ /(\w+)/;
print "not " unless $1 eq 'foo';
print "ok 5\n";

print "not " unless $foo->bar($1) eq 'foo';
print "ok 6\n";

print "not " unless $foo->bar($1) eq 'foo';
print "ok 7\n";

print "not " unless $foo->bazmarkhianish($1) eq 'foo';
print "ok 8\n";

print "not " unless $foo->bazmarkhianish($1) eq 'foo';
print "ok 9\n";

# test recursive autoloads
open(F, ">$dir${sep}auto${sep}Foo${sep}a.al") or die;
print F <<'EOT';
package Foo;
BEGIN { b() }
sub a { print "ok 11\n"; }
1;
EOT
close(F);

open(F, ">$dir${sep}auto${sep}Foo${sep}b.al") or die;
print F <<'EOT';
package Foo;
sub b { print "ok 10\n"; }
1;
EOT
close(F);
Foo::a();

# cleanup
END {
return unless $dir && -d $dir;
unlink "$dir${sep}auto${sep}Foo${sep}foo.al";
unlink "$dir${sep}auto${sep}Foo${sep}bar.al";
unlink "$dir${sep}auto${sep}Foo${sep}bazmarkhian.al";
unlink "$dir${sep}auto${sep}Foo${sep}a.al";
unlink "$dir${sep}auto${sep}Foo${sep}b.al";
rmdir "$dir${sep}auto${sep}Foo";
rmdir "$dir${sep}auto";
rmdir "$dir";
}