This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gmagic.t: Correct two test names
[perl5.git] / t / op / method.t
CommitLineData
9bfdb36e 1#!./perl -w
92d69e20
IZ
2
3#
4# test method calls and autoloading.
5#
6
4755096e
GS
7BEGIN {
8 chdir 't' if -d 't';
6dc4e5ce
JH
9 @INC = qw(. ../lib);
10 require "test.pl";
4755096e
GS
11}
12
9bfdb36e
NC
13use strict;
14no warnings 'once';
15
16plan(tests => 79);
92d69e20
IZ
17
18@A::ISA = 'B';
19@B::ISA = 'C';
20
21sub C::d {"C::d"}
22sub D::d {"D::d"}
23
567ce7b1 24# First, some basic checks of method-calling syntax:
9bfdb36e 25my $obj = bless [], "Pack";
567ce7b1 26sub Pack::method { shift; join(",", "method", @_) }
9bfdb36e 27my $mname = "method";
567ce7b1 28
6dc4e5ce
JH
29is(Pack->method("a","b","c"), "method,a,b,c");
30is(Pack->$mname("a","b","c"), "method,a,b,c");
31is(method Pack ("a","b","c"), "method,a,b,c");
32is((method Pack "a","b","c"), "method,a,b,c");
567ce7b1 33
6dc4e5ce
JH
34is(Pack->method(), "method");
35is(Pack->$mname(), "method");
36is(method Pack (), "method");
37is(Pack->method, "method");
38is(Pack->$mname, "method");
39is(method Pack, "method");
567ce7b1 40
6dc4e5ce
JH
41is($obj->method("a","b","c"), "method,a,b,c");
42is($obj->$mname("a","b","c"), "method,a,b,c");
43is((method $obj ("a","b","c")), "method,a,b,c");
44is((method $obj "a","b","c"), "method,a,b,c");
145eb477 45
6dc4e5ce
JH
46is($obj->method(0), "method,0");
47is($obj->method(1), "method,1");
567ce7b1 48
6dc4e5ce
JH
49is($obj->method(), "method");
50is($obj->$mname(), "method");
51is((method $obj ()), "method");
52is($obj->method, "method");
53is($obj->$mname, "method");
54is(method $obj, "method");
567ce7b1 55
6dc4e5ce 56is( A->d, "C::d"); # Update hash table;
92d69e20
IZ
57
58*B::d = \&D::d; # Import now.
6dc4e5ce 59is(A->d, "D::d"); # Update hash table;
92d69e20 60
44a8e56a 61{
fb73857a 62 local @A::ISA = qw(C); # Update hash table with split() assignment
6dc4e5ce 63 is(A->d, "C::d");
fb73857a 64 $#A::ISA = -1;
6dc4e5ce 65 is(eval { A->d } || "fail", "fail");
fb73857a 66}
6dc4e5ce 67is(A->d, "D::d");
fb73857a
PP
68
69{
44a8e56a
PP
70 local *B::d;
71 eval 'sub B::d {"B::d1"}'; # Import now.
6dc4e5ce 72 is(A->d, "B::d1"); # Update hash table;
44a8e56a 73 undef &B::d;
6dc4e5ce 74 is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
44a8e56a 75}
92d69e20 76
6dc4e5ce 77is(A->d, "D::d"); # Back to previous state
92d69e20 78
9bfdb36e 79eval 'no warnings "redefine"; sub B::d {"B::d2"}'; # Import now.
6dc4e5ce 80is(A->d, "B::d2"); # Update hash table;
92d69e20
IZ
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
86undef &B::d;
87delete $B::{d};
6dc4e5ce 88is(A->d, "C::d"); # Update hash table;
92d69e20
IZ
89
90eval 'sub B::d {"B::d3"}'; # Import now.
6dc4e5ce 91is(A->d, "B::d3"); # Update hash table;
92d69e20
IZ
92
93delete $B::{d};
94*dummy::dummy = sub {}; # Mark as updated
6dc4e5ce 95is(A->d, "C::d");
92d69e20
IZ
96
97eval 'sub B::d {"B::d4"}'; # Import now.
6dc4e5ce 98is(A->d, "B::d4"); # Update hash table;
92d69e20
IZ
99
100delete $B::{d}; # Should work without any help too
6dc4e5ce 101is(A->d, "C::d");
92d69e20 102
fae75791
CS
103{
104 local *C::d;
6dc4e5ce 105 is(eval { A->d } || "nope", "nope");
fae75791 106}
6dc4e5ce 107is(A->d, "C::d");
fae75791 108
9bfdb36e 109*A::x = *A::d;
44a8e56a 110A->d;
9bfdb36e
NC
111is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
112
113my $counter;
44a8e56a 114
92d69e20
IZ
115eval <<'EOF';
116sub C::e;
09280a33 117BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
92d69e20
IZ
118sub Y::f;
119$counter = 0;
120
54310121 121@X::ISA = 'Y';
dc848c6f 122@Y::ISA = 'B';
92d69e20
IZ
123
124sub B::AUTOLOAD {
125 my $c = ++$counter;
126 my $method = $B::AUTOLOAD;
09280a33
CS
127 my $msg = "B: In $method, $c";
128 eval "sub $method { \$msg }";
129 goto &$method;
92d69e20
IZ
130}
131sub C::AUTOLOAD {
132 my $c = ++$counter;
133 my $method = $C::AUTOLOAD;
09280a33
CS
134 my $msg = "C: In $method, $c";
135 eval "sub $method { \$msg }";
136 goto &$method;
92d69e20
IZ
137}
138EOF
139
6dc4e5ce
JH
140is(A->e(), "C: In C::e, 1"); # We get a correct autoload
141is(A->e(), "C: In C::e, 1"); # Which sticks
92d69e20 142
6dc4e5ce
JH
143is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
144is(A->ee(), "B: In A::ee, 2"); # Which sticks
92d69e20 145
6dc4e5ce
JH
146is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
147is(Y->f(), "B: In Y::f, 3"); # Which sticks
92d69e20
IZ
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
9bfdb36e
NC
153{
154no warnings 'redefine';
92d69e20 155*B::AUTOLOAD = sub {
9bfdb36e 156 use warnings;
92d69e20 157 my $c = ++$counter;
9bfdb36e
NC
158 my $method = $::AUTOLOAD;
159 no strict 'refs';
160 *$::AUTOLOAD = sub { "new B: In $method, $c" };
161 goto &$::AUTOLOAD;
92d69e20 162};
9bfdb36e 163}
92d69e20 164
6dc4e5ce
JH
165is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
166is(A->eee(), "new B: In A::eee, 4"); # Which sticks
fb73857a 167
9bfdb36e
NC
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}
f6ec51f7
GS
173
174# test that failed subroutine calls don't affect method calls
175{
176 package A1;
177 sub foo { "foo" }
178 package A2;
9bfdb36e 179 @A2::ISA = 'A1';
f6ec51f7 180 package main;
6dc4e5ce
JH
181 is(A2->foo(), "foo");
182 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
183 is(A2->foo(), "foo");
f6ec51f7 184}
c1899e02 185
af09ea45
IK
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# {
6dc4e5ce 191# is(do { use Config; eval 'Config->foo()';
af09ea45 192# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
6dc4e5ce 193# is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
af09ea45
IK
194# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
195# }
196
af09ea45 197# test error messages if method loading fails
9bfdb36e
NC
198my $e;
199
2f907243
NC
200eval '$e = bless {}, "E::A"; E::A->foo()';
201like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
202eval '$e = bless {}, "E::B"; $e->foo()';
203like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
204eval 'E::C->foo()';
205like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
206
207eval 'UNIVERSAL->E::D::foo()';
208like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
9bfdb36e 209eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
2f907243 210like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
af09ea45
IK
211
212$e = bless {}, "E::F"; # force package to exist
2f907243
NC
213eval 'UNIVERSAL->E::F::foo()';
214like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
215eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
216like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
af09ea45
IK
217
218# TODO: we need some tests for the SUPER:: pseudoclass
219
220# failed method call or UNIVERSAL::can() should not autovivify packages
6dc4e5ce
JH
221is( $::{"Foo::"} || "none", "none"); # sanity check 1
222is( $::{"Foo::"} || "none", "none"); # sanity check 2
c1899e02 223
6dc4e5ce
JH
224is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
225is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 226
6dc4e5ce
JH
227is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
228is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 229
6dc4e5ce
JH
230is( Foo->can("boogie") ? "yes":"no", "no" );
231is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 232
6dc4e5ce
JH
233is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
234is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 235
6dc4e5ce 236is(do { eval 'Foo->boogie()';
af09ea45
IK
237 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
238
239eval 'sub Foo::boogie { "yes, sir!" }';
6dc4e5ce
JH
240is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
241is( Foo->boogie(), "yes, sir!");
af09ea45
IK
242
243# TODO: universal.t should test NoSuchPackage->isa()/can()
c1899e02 244
f0670693
SC
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.
84251760 248eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
6dc4e5ce 249ok(1);
af09ea45 250
a397c3d9 251# Bug ID 20010902.002
6dc4e5ce 252is(
a397c3d9 253 eval q[
9bfdb36e 254 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
a397c3d9
RGS
255 sub Foo::x : lvalue { $x }
256 Foo->$x = 'ok';
257 ] || $@, 'ok'
258);
259
3ad83ce7
AMS
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;
6dc4e5ce 273 is($w, '');
3ad83ce7
AMS
274}
275
e189a56d
IK
276# [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
277
278package main;
279our @X;
280package Amajor;
281sub test {
282 push @main::X, 'Amajor', @_;
283}
284package Bminor;
285use base qw(Amajor);
286package main;
287sub Bminor::test {
288 $_[0]->Bminor::SUPER::test('x', 'y');
289 push @main::X, 'Bminor', @_;
290}
291Bminor->test('y', 'z');
292is("@X", "Amajor Bminor x y Bminor Bminor y z");
293
0dae17bd
GS
294package main;
295for my $meth (['Bar', 'Foo::Bar'],
296 ['SUPER::Bar', 'main::SUPER::Bar'],
297 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
298{
299 fresh_perl_is(<<EOT,
300package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
301package Xyz;
302package main; Foo->$meth->[0]();
303EOT
304 "Foo $meth->[1]",
305 { switches => [ '-w' ] },
306 "check if UNIVERSAL::AUTOLOAD works",
307 );
308}
1f15e670
NT
309
310# Test for #71952: crash when looking for a nonexistent destructor
311# Regression introduced by fbb3ee5af3d4
312{
313 fresh_perl_is(<<'EOT',
314sub M::DESTROY; bless {}, "M" ; print "survived\n";
315EOT
316 "survived",
317 {},
318 "no crash with a declared but missing DESTROY method"
319 );
320}
321