I don't think trying to bracket the hires time with lores
[p5sagit/p5-mst-13.2.git] / lib / AutoLoader.t
CommitLineData
a30dce55 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
95e8664e 5 if ($^O eq 'MacOS') {
6 $dir = ":auto-$$";
7 $sep = ":";
8 } else {
9 $dir = "auto-$$";
10 $sep = "/";
11 }
20822f61 12 @INC = $dir;
13 push @INC, '../lib';
a30dce55 14}
15
16579924 16print "1..11\n";
a30dce55 17
18# First we must set up some autoloader files
19mkdir $dir, 0755 or die "Can't mkdir $dir: $!";
95e8664e 20mkdir "$dir${sep}auto", 0755 or die "Can't mkdir: $!";
21mkdir "$dir${sep}auto${sep}Foo", 0755 or die "Can't mkdir: $!";
a30dce55 22
95e8664e 23open(FOO, ">$dir${sep}auto${sep}Foo${sep}foo.al") or die;
a30dce55 24print FOO <<'EOT';
25package Foo;
26sub foo { shift; shift || "foo" }
271;
28EOT
29close(FOO);
30
95e8664e 31open(BAR, ">$dir${sep}auto${sep}Foo${sep}bar.al") or die;
a30dce55 32print BAR <<'EOT';
33package Foo;
34sub bar { shift; shift || "bar" }
351;
36EOT
37close(BAR);
38
95e8664e 39open(BAZ, ">$dir${sep}auto${sep}Foo${sep}bazmarkhian.al") or die;
a30dce55 40print BAZ <<'EOT';
41package Foo;
42sub bazmarkhianish { shift; shift || "baz" }
431;
44EOT
45close(BAZ);
46
47# Let's define the package
48package Foo;
49require AutoLoader;
50@ISA=qw(AutoLoader);
51
52sub new { bless {}, shift };
53
54package main;
55
56$foo = new Foo;
57
58print "not " unless $foo->foo eq 'foo'; # autoloaded first time
59print "ok 1\n";
60
61print "not " unless $foo->foo eq 'foo'; # regular call
62print "ok 2\n";
63
64# Try an undefined method
65eval {
66 $foo->will_fail;
67};
68print "not " unless $@ =~ /^Can't locate/;
69print "ok 3\n";
70
71# Used to be trouble with this
72eval {
73 my $foo = new Foo;
74 die "oops";
75};
76print "not " unless $@ =~ /oops/;
77print "ok 4\n";
78
79# Pass regular expression variable to autoloaded function. This used
80# to go wrong because AutoLoader used regular expressions to generate
81# autoloaded filename.
82"foo" =~ /(\w+)/;
83print "not " unless $1 eq 'foo';
84print "ok 5\n";
85
86print "not " unless $foo->bar($1) eq 'foo';
87print "ok 6\n";
88
89print "not " unless $foo->bar($1) eq 'foo';
90print "ok 7\n";
91
92print "not " unless $foo->bazmarkhianish($1) eq 'foo';
93print "ok 8\n";
94
95print "not " unless $foo->bazmarkhianish($1) eq 'foo';
96print "ok 9\n";
97
16579924 98# test recursive autoloads
95e8664e 99open(F, ">$dir${sep}auto${sep}Foo${sep}a.al") or die;
16579924 100print F <<'EOT';
101package Foo;
102BEGIN { b() }
103sub a { print "ok 11\n"; }
1041;
105EOT
106close(F);
107
95e8664e 108open(F, ">$dir${sep}auto${sep}Foo${sep}b.al") or die;
16579924 109print F <<'EOT';
110package Foo;
111sub b { print "ok 10\n"; }
1121;
113EOT
114close(F);
115Foo::a();
116
a30dce55 117# cleanup
118END {
119return unless $dir && -d $dir;
95e8664e 120unlink "$dir${sep}auto${sep}Foo${sep}foo.al";
121unlink "$dir${sep}auto${sep}Foo${sep}bar.al";
122unlink "$dir${sep}auto${sep}Foo${sep}bazmarkhian.al";
123unlink "$dir${sep}auto${sep}Foo${sep}a.al";
124unlink "$dir${sep}auto${sep}Foo${sep}b.al";
125rmdir "$dir${sep}auto${sep}Foo";
126rmdir "$dir${sep}auto";
a30dce55 127rmdir "$dir";
128}