Commit | Line | Data |
7412bda7 |
1 | #!./perl -w |
a30dce55 |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
7412bda7 |
5 | @INC = '../lib'; |
a30dce55 |
6 | } |
7 | |
7412bda7 |
8 | use strict; |
9 | use File::Spec; |
10 | use File::Path; |
11 | |
12 | my $dir; |
13 | BEGIN |
14 | { |
15 | $dir = File::Spec->catdir( "auto-$$" ); |
16 | unshift @INC, $dir; |
17 | } |
18 | |
19 | use Test::More tests => 13; |
a30dce55 |
20 | |
21 | # First we must set up some autoloader files |
7412bda7 |
22 | my $fulldir = File::Spec->catdir( $dir, 'auto', 'Foo' ); |
23 | mkpath( $fulldir ) or die "Can't mkdir '$fulldir': $!"; |
a30dce55 |
24 | |
7412bda7 |
25 | open(FOO, '>', File::Spec->catfile( $fulldir, 'foo.al' )) |
26 | or die "Can't open foo file: $!"; |
a30dce55 |
27 | print FOO <<'EOT'; |
28 | package Foo; |
29 | sub foo { shift; shift || "foo" } |
30 | 1; |
31 | EOT |
32 | close(FOO); |
33 | |
7412bda7 |
34 | open(BAR, '>', File::Spec->catfile( $fulldir, 'bar.al' )) |
35 | or die "Can't open bar file: $!"; |
a30dce55 |
36 | print BAR <<'EOT'; |
37 | package Foo; |
38 | sub bar { shift; shift || "bar" } |
39 | 1; |
40 | EOT |
41 | close(BAR); |
42 | |
7412bda7 |
43 | open(BAZ, '>', File::Spec->catfile( $fulldir, 'bazmarkhian.al' )) |
44 | or die "Can't open bazmarkhian file: $!"; |
a30dce55 |
45 | print BAZ <<'EOT'; |
46 | package Foo; |
47 | sub bazmarkhianish { shift; shift || "baz" } |
48 | 1; |
49 | EOT |
50 | close(BAZ); |
51 | |
52 | # Let's define the package |
53 | package Foo; |
54 | require AutoLoader; |
7412bda7 |
55 | AutoLoader->import( 'AUTOLOAD' ); |
a30dce55 |
56 | |
57 | sub new { bless {}, shift }; |
58 | |
59 | package main; |
60 | |
7412bda7 |
61 | my $foo = new Foo; |
a30dce55 |
62 | |
7412bda7 |
63 | is( $foo->foo, 'foo', 'autoloaded first time' ); |
64 | is( $foo->foo, 'foo', 'regular call' ); |
a30dce55 |
65 | |
a30dce55 |
66 | eval { |
67 | $foo->will_fail; |
68 | }; |
7412bda7 |
69 | like( $@, qr/^Can't locate/, 'undefined method' ); |
a30dce55 |
70 | |
71 | # Used to be trouble with this |
72 | eval { |
73 | my $foo = new Foo; |
74 | die "oops"; |
75 | }; |
7412bda7 |
76 | like( $@, qr/oops/, 'indirect method call' ); |
a30dce55 |
77 | |
78 | # Pass regular expression variable to autoloaded function. This used |
79 | # to go wrong because AutoLoader used regular expressions to generate |
80 | # autoloaded filename. |
7412bda7 |
81 | 'foo' =~ /(\w+)/; |
a30dce55 |
82 | |
7412bda7 |
83 | is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' ); |
84 | is( $foo->bar($1), 'foo', '(again)' ); |
85 | is( $foo->bazmarkhianish($1), 'foo', 'for any method call' ); |
86 | is( $foo->bazmarkhianish($1), 'foo', '(again)' ); |
a30dce55 |
87 | |
16579924 |
88 | # test recursive autoloads |
7412bda7 |
89 | open(F, '>', File::Spec->catfile( $fulldir, 'a.al')) |
90 | or die "Cannot make 'a' file: $!"; |
16579924 |
91 | print F <<'EOT'; |
92 | package Foo; |
93 | BEGIN { b() } |
7412bda7 |
94 | sub a { ::ok( 1, 'adding a new autoloaded method' ); } |
16579924 |
95 | 1; |
96 | EOT |
97 | close(F); |
98 | |
7412bda7 |
99 | open(F, '>', File::Spec->catfile( $fulldir, 'b.al')) |
100 | or die "Cannot make 'b' file: $!"; |
16579924 |
101 | print F <<'EOT'; |
102 | package Foo; |
7412bda7 |
103 | sub b { ::ok( 1, 'adding a new autoloaded method' ) } |
16579924 |
104 | 1; |
105 | EOT |
106 | close(F); |
107 | Foo::a(); |
108 | |
7412bda7 |
109 | package Bar; |
110 | AutoLoader->import(); |
111 | ::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' ); |
112 | |
113 | package Foo; |
114 | AutoLoader->unimport(); |
115 | eval { Foo->baz() }; |
116 | ::like( $@, qr/locate object method "baz"/, |
117 | 'unimport() should remove imported AUTOLOAD()' ); |
118 | |
119 | package Baz; |
120 | |
121 | sub AUTOLOAD { 'i am here' } |
122 | |
123 | AutoLoader->import(); |
124 | AutoLoader->unimport(); |
125 | |
126 | ::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' ); |
127 | |
128 | package main; |
129 | |
a30dce55 |
130 | # cleanup |
131 | END { |
7412bda7 |
132 | return unless $dir && -d $dir; |
133 | rmtree $fulldir; |
a30dce55 |
134 | } |