Some escapes were mentioned twice, although they're not qr//-specific
[p5sagit/p5-mst-13.2.git] / lib / AutoLoader.t
CommitLineData
7412bda7 1#!./perl -w
a30dce55 2
3BEGIN {
4 chdir 't' if -d 't';
7412bda7 5 @INC = '../lib';
a30dce55 6}
7
7412bda7 8use strict;
9use File::Spec;
10use File::Path;
11
12my $dir;
13BEGIN
14{
15 $dir = File::Spec->catdir( "auto-$$" );
16 unshift @INC, $dir;
17}
18
3256a4f8 19use Test::More tests => 22;
a30dce55 20
21# First we must set up some autoloader files
7412bda7 22my $fulldir = File::Spec->catdir( $dir, 'auto', 'Foo' );
23mkpath( $fulldir ) or die "Can't mkdir '$fulldir': $!";
a30dce55 24
7412bda7 25open(FOO, '>', File::Spec->catfile( $fulldir, 'foo.al' ))
26 or die "Can't open foo file: $!";
a30dce55 27print FOO <<'EOT';
28package Foo;
29sub foo { shift; shift || "foo" }
301;
31EOT
32close(FOO);
33
7412bda7 34open(BAR, '>', File::Spec->catfile( $fulldir, 'bar.al' ))
35 or die "Can't open bar file: $!";
a30dce55 36print BAR <<'EOT';
37package Foo;
38sub bar { shift; shift || "bar" }
391;
40EOT
41close(BAR);
42
7412bda7 43open(BAZ, '>', File::Spec->catfile( $fulldir, 'bazmarkhian.al' ))
44 or die "Can't open bazmarkhian file: $!";
a30dce55 45print BAZ <<'EOT';
46package Foo;
47sub bazmarkhianish { shift; shift || "baz" }
481;
49EOT
50close(BAZ);
51
94825128 52open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawilla.al' ))
53 or die "Can't open blech file: $!";
54print BLECH <<'EOT';
55package Foo;
56sub blechanawilla { compilation error (
57EOT
58close(BLECH);
59
60# This is just to keep the old SVR3 systems happy; they may fail
61# to find the above file so we duplicate it where they should find it.
62open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawil.al' ))
63 or die "Can't open blech file: $!";
64print BLECH <<'EOT';
65package Foo;
66sub blechanawilla { compilation error (
67EOT
68close(BLECH);
69
a30dce55 70# Let's define the package
71package Foo;
72require AutoLoader;
7412bda7 73AutoLoader->import( 'AUTOLOAD' );
a30dce55 74
75sub new { bless {}, shift };
a498340c 76sub foo;
a498340c 77sub bazmarkhianish;
a30dce55 78
79package main;
80
00bb01c7 81my $foo = Foo->new();
a30dce55 82
a498340c 83my $result = $foo->can( 'foo' );
84ok( $result, 'can() first time' );
7412bda7 85is( $foo->foo, 'foo', 'autoloaded first time' );
86is( $foo->foo, 'foo', 'regular call' );
a498340c 87is( $result, \&Foo::foo, 'can() returns ref to regular installed sub' );
00bb01c7 88$result = $foo->can( 'bar' );
89ok( $result, 'can() should work when importing AUTOLOAD too' );
90is( $foo->bar, 'bar', 'regular call' );
91is( $result, \&Foo::bar, '... returning ref to regular installed sub' );
a30dce55 92
a30dce55 93eval {
94 $foo->will_fail;
95};
7412bda7 96like( $@, qr/^Can't locate/, 'undefined method' );
a30dce55 97
a498340c 98$result = $foo->can( 'will_fail' );
99ok( ! $result, 'can() should fail on undefined methods' );
100
a30dce55 101# Used to be trouble with this
102eval {
00bb01c7 103 my $foo = Foo->new();
a30dce55 104 die "oops";
105};
7412bda7 106like( $@, qr/oops/, 'indirect method call' );
a30dce55 107
108# Pass regular expression variable to autoloaded function. This used
109# to go wrong because AutoLoader used regular expressions to generate
110# autoloaded filename.
7412bda7 111'foo' =~ /(\w+)/;
a30dce55 112
7412bda7 113is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
114is( $foo->bar($1), 'foo', '(again)' );
115is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
116is( $foo->bazmarkhianish($1), 'foo', '(again)' );
a30dce55 117
94825128 118# Used to retry long subnames with shorter filenames on any old
119# exception, including compilation error. Now AutoLoader only
120# tries shorter filenames if it can't find the long one.
121eval {
122 $foo->blechanawilla;
123};
2f3efc97 124like( $@, qr/syntax error/i, 'require error propagates' );
94825128 125
16579924 126# test recursive autoloads
7412bda7 127open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
128 or die "Cannot make 'a' file: $!";
16579924 129print F <<'EOT';
130package Foo;
131BEGIN { b() }
7412bda7 132sub a { ::ok( 1, 'adding a new autoloaded method' ); }
16579924 1331;
134EOT
135close(F);
136
7412bda7 137open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
138 or die "Cannot make 'b' file: $!";
16579924 139print F <<'EOT';
140package Foo;
7412bda7 141sub b { ::ok( 1, 'adding a new autoloaded method' ) }
16579924 1421;
143EOT
144close(F);
145Foo::a();
146
7412bda7 147package Bar;
148AutoLoader->import();
149::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
00bb01c7 150::ok( ! defined &can, '... nor can()' );
7412bda7 151
152package Foo;
153AutoLoader->unimport();
154eval { Foo->baz() };
155::like( $@, qr/locate object method "baz"/,
156 'unimport() should remove imported AUTOLOAD()' );
157
158package Baz;
159
160sub AUTOLOAD { 'i am here' }
161
162AutoLoader->import();
163AutoLoader->unimport();
164
165::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
166
3256a4f8 167
168package SomeClass;
169use AutoLoader 'AUTOLOAD';
170sub new {
171 bless {} => shift;
172}
173
7412bda7 174package main;
175
3256a4f8 176$INC{"SomeClass.pm"} = $0; # Prepare possible recursion
177{
178 my $p = SomeClass->new();
179} # <-- deep recursion in AUTOLOAD looking for SomeClass::DESTROY?
180::ok(1, "AutoLoader shouldn't loop forever if \%INC is modified");
181
a30dce55 182# cleanup
183END {
7412bda7 184 return unless $dir && -d $dir;
94825128 185 rmtree $dir;
a30dce55 186}