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