method.t revision 7c478bd95313f5f23a4c958a745db2134aa03244
#!./perl
#
# test method calls and autoloading.
#
BEGIN {
chdir 't' if -d 't';
}
print "1..53\n";
@A::ISA = 'B';
@B::ISA = 'C';
sub C::d {"C::d"}
sub D::d {"D::d"}
my $cnt = 0;
sub test {
}
# First, some basic checks of method-calling syntax:
test( A->d, "C::d"); # Update hash table;
*B::d = \&D::d; # Import now.
test (A->d, "D::d"); # Update hash table;
{
local @A::ISA = qw(C); # Update hash table with split() assignment
test (A->d, "C::d");
$#A::ISA = -1;
}
test (A->d, "D::d");
{
local *B::d;
undef &B::d;
test ((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
}
test (A->d, "D::d"); # Back to previous state
# What follows is hardly guarantied to work, since the names in scripts
# after `delete $B::{d}; sub B::d {}' would reach an old subroutine.
undef &B::d;
delete $B::{d};
test (A->d, "C::d"); # Update hash table;
eval 'sub B::d {"B::d3"}'; # Import now.
test (A->d, "B::d3"); # Update hash table;
delete $B::{d};
test (A->d, "C::d");
eval 'sub B::d {"B::d4"}'; # Import now.
test (A->d, "B::d4"); # Update hash table;
delete $B::{d}; # Should work without any help too
test (A->d, "C::d");
{
local *C::d;
test (eval { A->d } || "nope", "nope");
}
test (A->d, "C::d");
*A::x = *A::d; # See if cache incorrectly follows synonyms
A->d;
test (eval { A->x } || "nope", "nope");
eval <<'EOF';
sub C::e;
sub Y::f;
$counter = 0;
sub B::AUTOLOAD {
my $c = ++$counter;
goto &$method;
}
sub C::AUTOLOAD {
my $c = ++$counter;
goto &$method;
}
test(A->e(), "C: In C::e, 1"); # We get a correct autoload
test(A->e(), "C: In C::e, 1"); # Which sticks
test(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
test(Y->f(), "B: In Y::f, 3"); # Which sticks
# This test is not intended to be reasonable. It is here just to let you
# know that you broke some old construction. Feel free to rewrite the test
# if your patch breaks it.
*B::AUTOLOAD = sub {
my $c = ++$counter;
goto &$AUTOLOAD;
};
# this test added due to bug discovery
# test that failed subroutine calls don't affect method calls
{
test(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
}
{
$@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
}
test(do { eval 'E->foo()';
$@ =~ /^\QCan't locate object method "foo" via package "E" (perhaps / ? 1 : $@}, 1);