| 1 | #!./perl
|
|---|
| 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 | print "1..78\n";
|
|---|
| 14 |
|
|---|
| 15 | @A::ISA = 'B';
|
|---|
| 16 | @B::ISA = 'C';
|
|---|
| 17 |
|
|---|
| 18 | sub C::d {"C::d"}
|
|---|
| 19 | sub D::d {"D::d"}
|
|---|
| 20 |
|
|---|
| 21 | # First, some basic checks of method-calling syntax:
|
|---|
| 22 | $obj = bless [], "Pack";
|
|---|
| 23 | sub Pack::method { shift; join(",", "method", @_) }
|
|---|
| 24 | $mname = "method";
|
|---|
| 25 |
|
|---|
| 26 | is(Pack->method("a","b","c"), "method,a,b,c");
|
|---|
| 27 | is(Pack->$mname("a","b","c"), "method,a,b,c");
|
|---|
| 28 | is(method Pack ("a","b","c"), "method,a,b,c");
|
|---|
| 29 | is((method Pack "a","b","c"), "method,a,b,c");
|
|---|
| 30 |
|
|---|
| 31 | is(Pack->method(), "method");
|
|---|
| 32 | is(Pack->$mname(), "method");
|
|---|
| 33 | is(method Pack (), "method");
|
|---|
| 34 | is(Pack->method, "method");
|
|---|
| 35 | is(Pack->$mname, "method");
|
|---|
| 36 | is(method Pack, "method");
|
|---|
| 37 |
|
|---|
| 38 | is($obj->method("a","b","c"), "method,a,b,c");
|
|---|
| 39 | is($obj->$mname("a","b","c"), "method,a,b,c");
|
|---|
| 40 | is((method $obj ("a","b","c")), "method,a,b,c");
|
|---|
| 41 | is((method $obj "a","b","c"), "method,a,b,c");
|
|---|
| 42 |
|
|---|
| 43 | is($obj->method(0), "method,0");
|
|---|
| 44 | is($obj->method(1), "method,1");
|
|---|
| 45 |
|
|---|
| 46 | is($obj->method(), "method");
|
|---|
| 47 | is($obj->$mname(), "method");
|
|---|
| 48 | is((method $obj ()), "method");
|
|---|
| 49 | is($obj->method, "method");
|
|---|
| 50 | is($obj->$mname, "method");
|
|---|
| 51 | is(method $obj, "method");
|
|---|
| 52 |
|
|---|
| 53 | is( A->d, "C::d"); # Update hash table;
|
|---|
| 54 |
|
|---|
| 55 | *B::d = \&D::d; # Import now.
|
|---|
| 56 | is(A->d, "D::d"); # Update hash table;
|
|---|
| 57 |
|
|---|
| 58 | {
|
|---|
| 59 | local @A::ISA = qw(C); # Update hash table with split() assignment
|
|---|
| 60 | is(A->d, "C::d");
|
|---|
| 61 | $#A::ISA = -1;
|
|---|
| 62 | is(eval { A->d } || "fail", "fail");
|
|---|
| 63 | }
|
|---|
| 64 | is(A->d, "D::d");
|
|---|
| 65 |
|
|---|
| 66 | {
|
|---|
| 67 | local *B::d;
|
|---|
| 68 | eval 'sub B::d {"B::d1"}'; # Import now.
|
|---|
| 69 | is(A->d, "B::d1"); # Update hash table;
|
|---|
| 70 | undef &B::d;
|
|---|
| 71 | is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | is(A->d, "D::d"); # Back to previous state
|
|---|
| 75 |
|
|---|
| 76 | eval 'sub B::d {"B::d2"}'; # Import now.
|
|---|
| 77 | is(A->d, "B::d2"); # Update hash table;
|
|---|
| 78 |
|
|---|
| 79 | # What follows is hardly guarantied to work, since the names in scripts
|
|---|
| 80 | # are already linked to "pruned" globs. Say, `undef &B::d' if it were
|
|---|
| 81 | # after `delete $B::{d}; sub B::d {}' would reach an old subroutine.
|
|---|
| 82 |
|
|---|
| 83 | undef &B::d;
|
|---|
| 84 | delete $B::{d};
|
|---|
| 85 | is(A->d, "C::d"); # Update hash table;
|
|---|
| 86 |
|
|---|
| 87 | eval 'sub B::d {"B::d3"}'; # Import now.
|
|---|
| 88 | is(A->d, "B::d3"); # Update hash table;
|
|---|
| 89 |
|
|---|
| 90 | delete $B::{d};
|
|---|
| 91 | *dummy::dummy = sub {}; # Mark as updated
|
|---|
| 92 | is(A->d, "C::d");
|
|---|
| 93 |
|
|---|
| 94 | eval 'sub B::d {"B::d4"}'; # Import now.
|
|---|
| 95 | is(A->d, "B::d4"); # Update hash table;
|
|---|
| 96 |
|
|---|
| 97 | delete $B::{d}; # Should work without any help too
|
|---|
| 98 | is(A->d, "C::d");
|
|---|
| 99 |
|
|---|
| 100 | {
|
|---|
| 101 | local *C::d;
|
|---|
| 102 | is(eval { A->d } || "nope", "nope");
|
|---|
| 103 | }
|
|---|
| 104 | is(A->d, "C::d");
|
|---|
| 105 |
|
|---|
| 106 | *A::x = *A::d; # See if cache incorrectly follows synonyms
|
|---|
| 107 | A->d;
|
|---|
| 108 | is(eval { A->x } || "nope", "nope");
|
|---|
| 109 |
|
|---|
| 110 | eval <<'EOF';
|
|---|
| 111 | sub C::e;
|
|---|
| 112 | BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
|
|---|
| 113 | sub Y::f;
|
|---|
| 114 | $counter = 0;
|
|---|
| 115 |
|
|---|
| 116 | @X::ISA = 'Y';
|
|---|
| 117 | @Y::ISA = 'B';
|
|---|
| 118 |
|
|---|
| 119 | sub B::AUTOLOAD {
|
|---|
| 120 | my $c = ++$counter;
|
|---|
| 121 | my $method = $B::AUTOLOAD;
|
|---|
| 122 | my $msg = "B: In $method, $c";
|
|---|
| 123 | eval "sub $method { \$msg }";
|
|---|
| 124 | goto &$method;
|
|---|
| 125 | }
|
|---|
| 126 | sub C::AUTOLOAD {
|
|---|
| 127 | my $c = ++$counter;
|
|---|
| 128 | my $method = $C::AUTOLOAD;
|
|---|
| 129 | my $msg = "C: In $method, $c";
|
|---|
| 130 | eval "sub $method { \$msg }";
|
|---|
| 131 | goto &$method;
|
|---|
| 132 | }
|
|---|
| 133 | EOF
|
|---|
| 134 |
|
|---|
| 135 | is(A->e(), "C: In C::e, 1"); # We get a correct autoload
|
|---|
| 136 | is(A->e(), "C: In C::e, 1"); # Which sticks
|
|---|
| 137 |
|
|---|
| 138 | is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
|
|---|
| 139 | is(A->ee(), "B: In A::ee, 2"); # Which sticks
|
|---|
| 140 |
|
|---|
| 141 | is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
|
|---|
| 142 | is(Y->f(), "B: In Y::f, 3"); # Which sticks
|
|---|
| 143 |
|
|---|
| 144 | # This test is not intended to be reasonable. It is here just to let you
|
|---|
| 145 | # know that you broke some old construction. Feel free to rewrite the test
|
|---|
| 146 | # if your patch breaks it.
|
|---|
| 147 |
|
|---|
| 148 | *B::AUTOLOAD = sub {
|
|---|
| 149 | my $c = ++$counter;
|
|---|
| 150 | my $method = $AUTOLOAD;
|
|---|
| 151 | *$AUTOLOAD = sub { "new B: In $method, $c" };
|
|---|
| 152 | goto &$AUTOLOAD;
|
|---|
| 153 | };
|
|---|
| 154 |
|
|---|
| 155 | is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
|
|---|
| 156 | is(A->eee(), "new B: In A::eee, 4"); # Which sticks
|
|---|
| 157 |
|
|---|
| 158 | # this test added due to bug discovery
|
|---|
| 159 | is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
|
|---|
| 160 |
|
|---|
| 161 | # test that failed subroutine calls don't affect method calls
|
|---|
| 162 | {
|
|---|
| 163 | package A1;
|
|---|
| 164 | sub foo { "foo" }
|
|---|
| 165 | package A2;
|
|---|
| 166 | @ISA = 'A1';
|
|---|
| 167 | package main;
|
|---|
| 168 | is(A2->foo(), "foo");
|
|---|
| 169 | is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
|
|---|
| 170 | is(A2->foo(), "foo");
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | ## This test was totally misguided. It passed before only because the
|
|---|
| 174 | ## code to determine if a package was loaded used to look for the hash
|
|---|
| 175 | ## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
|
|---|
| 176 | ## happens to export %Config.
|
|---|
| 177 | # {
|
|---|
| 178 | # is(do { use Config; eval 'Config->foo()';
|
|---|
| 179 | # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
|
|---|
| 180 | # is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
|
|---|
| 181 | # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
|
|---|
| 182 | # }
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | # test error messages if method loading fails
|
|---|
| 186 | is(do { eval '$e = bless {}, "E::A"; E::A->foo()';
|
|---|
| 187 | $@ =~ /^\QCan't locate object method "foo" via package "E::A" at/ ? 1 : $@}, 1);
|
|---|
| 188 | is(do { eval '$e = bless {}, "E::B"; $e->foo()';
|
|---|
|
|---|