Commit | Line | Data |
bfdd1499 |
1 | #!./perl |
2 | |
3 | BEGIN { |
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'; |
23 | package Foo; |
24 | use SelfLoader; |
25 | |
26 | sub new { bless {}, shift } |
27 | sub foo; |
28 | sub bar; |
29 | sub bazmarkhianish; |
30 | sub a; |
31 | sub never; # declared but definition should never be read |
32 | 1; |
33 | __DATA__ |
34 | |
35 | sub foo { shift; shift || "foo" }; |
36 | |
37 | sub bar { shift; shift || "bar" } |
38 | |
39 | sub bazmarkhianish { shift; shift || "baz" } |
40 | |
41 | package sheep; |
42 | sub bleat { shift; shift || "baa" } |
bfdd1499 |
43 | __END__ |
44 | sub never { die "D'oh" } |
45 | EOT |
46 | |
47 | close(FOO); |
48 | |
95e8664e |
49 | open(BAR, ">$dir${sep}Bar.pm") or die; |
bfdd1499 |
50 | print BAR <<'EOT'; |
51 | package Bar; |
52 | use SelfLoader; |
53 | |
54 | @ISA = 'Baz'; |
55 | |
56 | sub new { bless {}, shift } |
57 | sub a; |
1163f0e4 |
58 | sub with_whitespace_in_front; |
bfdd1499 |
59 | |
60 | 1; |
61 | __DATA__ |
62 | |
63 | sub a { 'a Bar'; } |
64 | sub b { 'b Bar' } |
65 | |
1163f0e4 |
66 | sub with_whitespace_in_front { |
67 | "with_whitespace_in_front Bar" |
68 | } |
69 | |
bfdd1499 |
70 | __END__ DATA |
71 | sub never { die "D'oh" } |
72 | EOT |
73 | |
74 | close(BAR); |
75 | }; |
76 | |
77 | |
78 | package Baz; |
79 | |
80 | sub a { 'a Baz' } |
81 | sub b { 'b Baz' } |
82 | sub c { 'c Baz' } |
83 | |
84 | |
85 | package main; |
86 | use Foo; |
87 | use Bar; |
88 | |
89 | $foo = new Foo; |
90 | |
91 | print "not " unless $foo->foo eq 'foo'; # selfloaded first time |
92 | print "ok 1\n"; |
93 | |
94 | print "not " unless $foo->foo eq 'foo'; # regular call |
95 | print "ok 2\n"; |
96 | |
97 | # Try an undefined method |
98 | eval { |
99 | $foo->will_fail; |
100 | }; |
101 | if ($@ =~ /^Undefined subroutine/) { |
102 | print "ok 3\n"; |
103 | } else { |
104 | print "not ok 3 $@\n"; |
105 | } |
106 | |
107 | # Used to be trouble with this |
108 | eval { |
109 | my $foo = new Foo; |
110 | die "oops"; |
111 | }; |
112 | if ($@ =~ /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+)/; |
122 | print "not " unless $1 eq 'foo'; |
123 | print "ok 5\n"; |
124 | |
125 | print "not " unless $foo->bar($1) eq 'foo'; |
126 | print "ok 6\n"; |
127 | |
128 | print "not " unless $foo->bar($1) eq 'foo'; |
129 | print "ok 7\n"; |
130 | |
131 | print "not " unless $foo->bazmarkhianish($1) eq 'foo'; |
132 | print "ok 8\n"; |
133 | |
134 | print "not " unless $foo->bazmarkhianish($1) eq 'foo'; |
135 | print "ok 9\n"; |
136 | |
137 | # Check nested packages inside __DATA__ |
138 | print "not " unless sheep::bleat() eq 'baa'; |
139 | print "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 |
147 | print "not " unless $bar->b() eq 'b Baz'; |
148 | print "ok 11\n"; |
149 | |
150 | # There is no Bar::c so we should get Baz::c |
151 | print "not " unless $bar->c() eq 'c Baz'; |
152 | print "ok 12\n"; |
153 | |
1163f0e4 |
154 | # check that subs with whitespace in front work |
155 | print "not " unless $bar->with_whitespace_in_front() eq 'with_whitespace_in_front Bar'; |
156 | print "ok 13\n"; |
157 | |
bfdd1499 |
158 | # This selfloads Bar::a because it is stubbed. It also stubs Bar::b as a side |
159 | # effect |
160 | print "not " unless $bar->a() eq 'a Bar'; |
1163f0e4 |
161 | print "ok 14\n"; |
bfdd1499 |
162 | |
163 | print "not " unless $bar->b() eq 'b Bar'; |
1163f0e4 |
164 | print "ok 15\n"; |
bfdd1499 |
165 | |
166 | print "not " unless $bar->c() eq 'c Baz'; |
1163f0e4 |
167 | print "ok 16\n"; |
bfdd1499 |
168 | |
169 | |
170 | |
171 | # Check that __END__ is honoured |
172 | # Try an subroutine that should never be noticed by selfloader |
173 | eval { |
174 | $foo->never; |
175 | }; |
176 | if ($@ =~ /^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 |
183 | my $foodata = <Foo::DATA>; |
efb7d285 |
184 | close Foo::DATA; |
bfdd1499 |
185 | if (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 |
193 | eval { |
194 | $bar->never; |
195 | }; |
196 | if ($@ =~ /^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 |
203 | my $bardata = <Bar::DATA>; |
efb7d285 |
204 | close Bar::DATA; |
bfdd1499 |
205 | if ($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 |
212 | END { |
213 | return unless $dir && -d $dir; |
95e8664e |
214 | unlink "$dir${sep}Foo.pm", "$dir${sep}Bar.pm"; |
bfdd1499 |
215 | rmdir "$dir"; |
216 | } |