This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added porting tests for CUSTOMIZED files
[perl5.git] / t / op / method.t
1 #!./perl -w
2
3 #
4 # test method calls and autoloading.
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = qw(. ../lib);
10     require "test.pl";
11 }
12
13 use strict;
14 no warnings 'once';
15
16 plan(tests => 83);
17
18 @A::ISA = 'B';
19 @B::ISA = 'C';
20
21 sub C::d {"C::d"}
22 sub D::d {"D::d"}
23
24 # First, some basic checks of method-calling syntax:
25 my $obj = bless [], "Pack";
26 sub Pack::method { shift; join(",", "method", @_) }
27 my $mname = "method";
28
29 is(Pack->method("a","b","c"), "method,a,b,c");
30 is(Pack->$mname("a","b","c"), "method,a,b,c");
31 is(method Pack ("a","b","c"), "method,a,b,c");
32 is((method Pack "a","b","c"), "method,a,b,c");
33
34 is(Pack->method(), "method");
35 is(Pack->$mname(), "method");
36 is(method Pack (), "method");
37 is(Pack->method, "method");
38 is(Pack->$mname, "method");
39 is(method Pack, "method");
40
41 is($obj->method("a","b","c"), "method,a,b,c");
42 is($obj->$mname("a","b","c"), "method,a,b,c");
43 is((method $obj ("a","b","c")), "method,a,b,c");
44 is((method $obj "a","b","c"), "method,a,b,c");
45
46 is($obj->method(0), "method,0");
47 is($obj->method(1), "method,1");
48
49 is($obj->method(), "method");
50 is($obj->$mname(), "method");
51 is((method $obj ()), "method");
52 is($obj->method, "method");
53 is($obj->$mname, "method");
54 is(method $obj, "method");
55
56 is( A->d, "C::d");              # Update hash table;
57
58 *B::d = \&D::d;                 # Import now.
59 is(A->d, "D::d");               # Update hash table;
60
61 {
62     local @A::ISA = qw(C);      # Update hash table with split() assignment
63     is(A->d, "C::d");
64     $#A::ISA = -1;
65     is(eval { A->d } || "fail", "fail");
66 }
67 is(A->d, "D::d");
68
69 {
70     local *B::d;
71     eval 'sub B::d {"B::d1"}';  # Import now.
72     is(A->d, "B::d1");  # Update hash table;
73     undef &B::d;
74     is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
75 }
76
77 is(A->d, "D::d");               # Back to previous state
78
79 eval 'no warnings "redefine"; sub B::d {"B::d2"}';      # Import now.
80 is(A->d, "B::d2");              # Update hash table;
81
82 # What follows is hardly guarantied to work, since the names in scripts
83 # are already linked to "pruned" globs. Say, 'undef &B::d' if it were
84 # after 'delete $B::{d}; sub B::d {}' would reach an old subroutine.
85
86 undef &B::d;
87 delete $B::{d};
88 is(A->d, "C::d");               # Update hash table;
89
90 eval 'sub B::d {"B::d3"}';      # Import now.
91 is(A->d, "B::d3");              # Update hash table;
92
93 delete $B::{d};
94 *dummy::dummy = sub {};         # Mark as updated
95 is(A->d, "C::d");
96
97 eval 'sub B::d {"B::d4"}';      # Import now.
98 is(A->d, "B::d4");              # Update hash table;
99
100 delete $B::{d};                 # Should work without any help too
101 is(A->d, "C::d");
102
103 {
104     local *C::d;
105     is(eval { A->d } || "nope", "nope");
106 }
107 is(A->d, "C::d");
108
109 *A::x = *A::d;
110 A->d;
111 is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
112
113 my $counter;
114
115 eval <<'EOF';
116 sub C::e;
117 BEGIN { *B::e = \&C::e }        # Shouldn't prevent AUTOLOAD in original pkg
118 sub Y::f;
119 $counter = 0;
120
121 @X::ISA = 'Y';
122 @Y::ISA = 'B';
123
124 sub B::AUTOLOAD {
125   my $c = ++$counter;
126   my $method = $B::AUTOLOAD; 
127   my $msg = "B: In $method, $c";
128   eval "sub $method { \$msg }";
129   goto &$method;
130 }
131 sub C::AUTOLOAD {
132   my $c = ++$counter;
133   my $method = $C::AUTOLOAD; 
134   my $msg = "C: In $method, $c";
135   eval "sub $method { \$msg }";
136   goto &$method;
137 }
138 EOF
139
140 is(A->e(), "C: In C::e, 1");    # We get a correct autoload
141 is(A->e(), "C: In C::e, 1");    # Which sticks
142
143 is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
144 is(A->ee(), "B: In A::ee, 2"); # Which sticks
145
146 is(Y->f(), "B: In Y::f, 3");    # We vivify a correct method
147 is(Y->f(), "B: In Y::f, 3");    # Which sticks
148
149 # This test is not intended to be reasonable. It is here just to let you
150 # know that you broke some old construction. Feel free to rewrite the test
151 # if your patch breaks it.
152
153 {
154 no warnings 'redefine';
155 *B::AUTOLOAD = sub {
156   use warnings;
157   my $c = ++$counter;
158   my $method = $::AUTOLOAD; 
159   no strict 'refs';
160   *$::AUTOLOAD = sub { "new B: In $method, $c" };
161   goto &$::AUTOLOAD;
162 };
163 }
164
165 is(A->eee(), "new B: In A::eee, 4");    # We get a correct $autoload
166 is(A->eee(), "new B: In A::eee, 4");    # Which sticks
167
168 {
169     no strict 'refs';
170     # this test added due to bug discovery (in 5.004_04, fb73857aa0bfa8ed)
171     is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
172 }
173
174 # test that failed subroutine calls don't affect method calls
175 {
176     package A1;
177     sub foo { "foo" }
178     package A2;
179     @A2::ISA = 'A1';
180     package main;
181     is(A2->foo(), "foo");
182     is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
183     is(A2->foo(), "foo");
184 }
185
186 ## This test was totally misguided.  It passed before only because the
187 ## code to determine if a package was loaded used to look for the hash
188 ## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
189 ## happens to export %Config.
190 #  {
191 #      is(do { use Config; eval 'Config->foo()';
192 #             $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
193 #      is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
194 #             $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
195 #  }
196
197 # test error messages if method loading fails
198 my $e;
199
200 eval '$e = bless {}, "E::A"; E::A->foo()';
201 like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
202 eval '$e = bless {}, "E::B"; $e->foo()';  
203 like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
204 eval 'E::C->foo()';
205 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
206
207 eval 'UNIVERSAL->E::D::foo()';
208 like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
209 eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
210 like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
211
212 $e = bless {}, "E::F";  # force package to exist
213 eval 'UNIVERSAL->E::F::foo()';
214 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
215 eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
216 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
217
218 # TODO: we need some tests for the SUPER:: pseudoclass
219
220 # failed method call or UNIVERSAL::can() should not autovivify packages
221 is( $::{"Foo::"} || "none", "none");  # sanity check 1
222 is( $::{"Foo::"} || "none", "none");  # sanity check 2
223
224 is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
225 is( $::{"Foo::"} || "none", "none");  # still missing?
226
227 is( Foo->UNIVERSAL::can("boogie")   ? "yes":"no", "no" );
228 is( $::{"Foo::"} || "none", "none");  # still missing?
229
230 is( Foo->can("boogie")              ? "yes":"no", "no" );
231 is( $::{"Foo::"} || "none", "none");  # still missing?
232
233 is( eval 'Foo->boogie(); 1'         ? "yes":"no", "no" );
234 is( $::{"Foo::"} || "none", "none");  # still missing?
235
236 is(do { eval 'Foo->boogie()';
237           $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
238
239 eval 'sub Foo::boogie { "yes, sir!" }';
240 is( $::{"Foo::"} ? "ok" : "none", "ok");  # should exist now
241 is( Foo->boogie(), "yes, sir!");
242
243 # TODO: universal.t should test NoSuchPackage->isa()/can()
244
245 # This is actually testing parsing of indirect objects and undefined subs
246 #   print foo("bar") where foo does not exist is not an indirect object.
247 #   print foo "bar"  where foo does not exist is an indirect object.
248 eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
249 ok(1);
250
251 # Bug ID 20010902.002
252 is(
253     eval q[
254         my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
255         sub Foo::x : lvalue { $x }
256         Foo->$x = 'ok';
257     ] || $@, 'ok'
258 );
259
260 # An autoloaded, inherited DESTROY may be invoked differently than normal
261 # methods, and has been known to give rise to spurious warnings
262 # eg <200203121600.QAA11064@gizmo.fdgroup.co.uk>
263
264 {
265     use warnings;
266     my $w = '';
267     local $SIG{__WARN__} = sub { $w = $_[0] };
268
269     sub AutoDest::Base::AUTOLOAD {}
270     @AutoDest::ISA = qw(AutoDest::Base);
271     { my $x = bless {}, 'AutoDest'; }
272     $w =~ s/\n//g;
273     is($w, '');
274 }
275
276 # [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
277
278 package main;
279 our @X;
280 package Amajor;
281 sub test {
282     push @main::X, 'Amajor', @_;
283 }
284 package Bminor;
285 use base qw(Amajor);
286 package main;
287 sub Bminor::test {
288     $_[0]->Bminor::SUPER::test('x', 'y');
289     push @main::X, 'Bminor', @_;
290 }
291 Bminor->test('y', 'z');
292 is("@X", "Amajor Bminor x y Bminor Bminor y z");
293
294 package main;
295 for my $meth (['Bar', 'Foo::Bar'],
296               ['SUPER::Bar', 'main::SUPER::Bar'],
297               ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
298 {
299     fresh_perl_is(<<EOT,
300 package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
301 package Xyz;
302 package main; Foo->$meth->[0]();
303 EOT
304         "Foo $meth->[1]",
305         { switches => [ '-w' ] },
306         "check if UNIVERSAL::AUTOLOAD works",
307     );
308 }
309
310 # Test for #71952: crash when looking for a nonexistent destructor
311 # Regression introduced by fbb3ee5af3d4
312 {
313     fresh_perl_is(<<'EOT',
314 sub M::DESTROY; bless {}, "M" ; print "survived\n";
315 EOT
316     "survived",
317     {},
318         "no crash with a declared but missing DESTROY method"
319     );
320 }
321
322 # Test for calling a method on a packag name return by a magic variable
323 sub TIESCALAR{bless[]}
324 sub FETCH{"main"}
325 my $kalled;
326 sub bolgy { ++$kalled; }
327 tie my $a, "";
328 $a->bolgy;
329 is $kalled, 1, 'calling a class method via a magic variable';
330
331 {
332     package NulTest;
333     sub method { 1 }
334
335     package main;
336     eval {
337         NulTest->${ \"method\0Whoops" };
338     };
339     like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
340             "method lookup is nul-clean";
341
342     *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
343
344     like(NulTest->${ \"nul\0test" }, "nul\0test", "AUTOLOAD is nul-clean");
345 }
346
347
348 {
349     fresh_perl_is(
350     q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
351     "DESTROY created new reference to dead object 'T' during global destruction.",
352     {},
353         "DESTROY creating a new reference to the object generates a warning."
354     );
355 }