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