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