Further tweaks to perluniintro.pod
[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
19use Test::More tests => 13;
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
52# Let's define the package
53package Foo;
54require AutoLoader;
7412bda7 55AutoLoader->import( 'AUTOLOAD' );
a30dce55 56
57sub new { bless {}, shift };
58
59package main;
60
7412bda7 61my $foo = new Foo;
a30dce55 62
7412bda7 63is( $foo->foo, 'foo', 'autoloaded first time' );
64is( $foo->foo, 'foo', 'regular call' );
a30dce55 65
a30dce55 66eval {
67 $foo->will_fail;
68};
7412bda7 69like( $@, qr/^Can't locate/, 'undefined method' );
a30dce55 70
71# Used to be trouble with this
72eval {
73 my $foo = new Foo;
74 die "oops";
75};
7412bda7 76like( $@, 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 83is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
84is( $foo->bar($1), 'foo', '(again)' );
85is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
86is( $foo->bazmarkhianish($1), 'foo', '(again)' );
a30dce55 87
16579924 88# test recursive autoloads
7412bda7 89open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
90 or die "Cannot make 'a' file: $!";
16579924 91print F <<'EOT';
92package Foo;
93BEGIN { b() }
7412bda7 94sub a { ::ok( 1, 'adding a new autoloaded method' ); }
16579924 951;
96EOT
97close(F);
98
7412bda7 99open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
100 or die "Cannot make 'b' file: $!";
16579924 101print F <<'EOT';
102package Foo;
7412bda7 103sub b { ::ok( 1, 'adding a new autoloaded method' ) }
16579924 1041;
105EOT
106close(F);
107Foo::a();
108
7412bda7 109package Bar;
110AutoLoader->import();
111::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
112
113package Foo;
114AutoLoader->unimport();
115eval { Foo->baz() };
116::like( $@, qr/locate object method "baz"/,
117 'unimport() should remove imported AUTOLOAD()' );
118
119package Baz;
120
121sub AUTOLOAD { 'i am here' }
122
123AutoLoader->import();
124AutoLoader->unimport();
125
126::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
127
128package main;
129
a30dce55 130# cleanup
131END {
7412bda7 132 return unless $dir && -d $dir;
133 rmtree $fulldir;
a30dce55 134}