Move the SelfLoader test files in their own subdirectory
[p5sagit/p5-mst-13.2.git] / lib / SelfLoader / t / 01SelfLoader.t
CommitLineData
bfdd1499 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 $dir = "self-$$";
95e8664e 6 $sep = "/";
7
8 if ($^O eq 'MacOS') {
9 $dir = ":" . $dir;
10 $sep = ":";
11 }
12
20822f61 13 @INC = $dir;
14 push @INC, '../lib';
bfdd1499 15
1163f0e4 16 print "1..20\n";
bfdd1499 17
18 # First we must set up some selfloader files
19 mkdir $dir, 0755 or die "Can't mkdir $dir: $!";
20
95e8664e 21 open(FOO, ">$dir${sep}Foo.pm") or die;
bfdd1499 22 print FOO <<'EOT';
23package Foo;
24use SelfLoader;
25
26sub new { bless {}, shift }
27sub foo;
28sub bar;
29sub bazmarkhianish;
30sub a;
31sub never; # declared but definition should never be read
321;
33__DATA__
34
35sub foo { shift; shift || "foo" };
36
37sub bar { shift; shift || "bar" }
38
39sub bazmarkhianish { shift; shift || "baz" }
40
41package sheep;
42sub bleat { shift; shift || "baa" }
bfdd1499 43__END__
44sub never { die "D'oh" }
45EOT
46
47 close(FOO);
48
95e8664e 49 open(BAR, ">$dir${sep}Bar.pm") or die;
bfdd1499 50 print BAR <<'EOT';
51package Bar;
52use SelfLoader;
53
54@ISA = 'Baz';
55
56sub new { bless {}, shift }
57sub a;
1163f0e4 58sub with_whitespace_in_front;
bfdd1499 59
601;
61__DATA__
62
63sub a { 'a Bar'; }
64sub b { 'b Bar' }
65
1163f0e4 66 sub with_whitespace_in_front {
67 "with_whitespace_in_front Bar"
68}
69
bfdd1499 70__END__ DATA
71sub never { die "D'oh" }
72EOT
73
74 close(BAR);
75};
76
77
78package Baz;
79
80sub a { 'a Baz' }
81sub b { 'b Baz' }
82sub c { 'c Baz' }
83
84
85package main;
86use Foo;
87use Bar;
88
89$foo = new Foo;
90
91print "not " unless $foo->foo eq 'foo'; # selfloaded first time
92print "ok 1\n";
93
94print "not " unless $foo->foo eq 'foo'; # regular call
95print "ok 2\n";
96
97# Try an undefined method
98eval {
99 $foo->will_fail;
100};
101if ($@ =~ /^Undefined subroutine/) {
102 print "ok 3\n";
103} else {
104 print "not ok 3 $@\n";
105}
106
107# Used to be trouble with this
108eval {
109 my $foo = new Foo;
110 die "oops";
111};
112if ($@ =~ /oops/) {
113 print "ok 4\n";
114} else {
115 print "not ok 4 $@\n";
116}
117
118# Pass regular expression variable to autoloaded function. This used
119# to go wrong in AutoLoader because it used regular expressions to generate
120# autoloaded filename.
121"foo" =~ /(\w+)/;
122print "not " unless $1 eq 'foo';
123print "ok 5\n";
124
125print "not " unless $foo->bar($1) eq 'foo';
126print "ok 6\n";
127
128print "not " unless $foo->bar($1) eq 'foo';
129print "ok 7\n";
130
131print "not " unless $foo->bazmarkhianish($1) eq 'foo';
132print "ok 8\n";
133
134print "not " unless $foo->bazmarkhianish($1) eq 'foo';
135print "ok 9\n";
136
137# Check nested packages inside __DATA__
138print "not " unless sheep::bleat() eq 'baa';
139print "ok 10\n";
140
141# Now check inheritance:
142
143$bar = new Bar;
144
145# Before anything is SelfLoaded there is no declaration of Foo::b so we should
146# get Baz::b
147print "not " unless $bar->b() eq 'b Baz';
148print "ok 11\n";
149
150# There is no Bar::c so we should get Baz::c
151print "not " unless $bar->c() eq 'c Baz';
152print "ok 12\n";
153
1163f0e4 154# check that subs with whitespace in front work
155print "not " unless $bar->with_whitespace_in_front() eq 'with_whitespace_in_front Bar';
156print "ok 13\n";
157
bfdd1499 158# This selfloads Bar::a because it is stubbed. It also stubs Bar::b as a side
159# effect
160print "not " unless $bar->a() eq 'a Bar';
1163f0e4 161print "ok 14\n";
bfdd1499 162
163print "not " unless $bar->b() eq 'b Bar';
1163f0e4 164print "ok 15\n";
bfdd1499 165
166print "not " unless $bar->c() eq 'c Baz';
1163f0e4 167print "ok 16\n";
bfdd1499 168
169
170
171# Check that __END__ is honoured
172# Try an subroutine that should never be noticed by selfloader
173eval {
174 $foo->never;
175};
176if ($@ =~ /^Undefined subroutine/) {
1163f0e4 177 print "ok 17\n";
bfdd1499 178} else {
1163f0e4 179 print "not ok 17 $@\n";
bfdd1499 180}
181
182# Try to read from the data file handle
183my $foodata = <Foo::DATA>;
efb7d285 184close Foo::DATA;
bfdd1499 185if (defined $foodata) {
1163f0e4 186 print "not ok 18 # $foodata\n";
bfdd1499 187} else {
1163f0e4 188 print "ok 18\n";
bfdd1499 189}
190
191# Check that __END__ DATA is honoured
192# Try an subroutine that should never be noticed by selfloader
193eval {
194 $bar->never;
195};
196if ($@ =~ /^Undefined subroutine/) {
1163f0e4 197 print "ok 19\n";
bfdd1499 198} else {
1163f0e4 199 print "not ok 19 $@\n";
bfdd1499 200}
201
202# Try to read from the data file handle
203my $bardata = <Bar::DATA>;
efb7d285 204close Bar::DATA;
bfdd1499 205if ($bardata ne "sub never { die \"D'oh\" }\n") {
1163f0e4 206 print "not ok 20 # $bardata\n";
bfdd1499 207} else {
1163f0e4 208 print "ok 20\n";
bfdd1499 209}
210
211# cleanup
212END {
213return unless $dir && -d $dir;
95e8664e 214unlink "$dir${sep}Foo.pm", "$dir${sep}Bar.pm";
bfdd1499 215rmdir "$dir";
216}