1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#!./perl
use strict;
use warnings;
require q(./test.pl); plan(tests => 38);
{
package MRO_A;
our @ISA = qw//;
package MRO_B;
our @ISA = qw//;
package MRO_C;
our @ISA = qw//;
package MRO_D;
our @ISA = qw/MRO_A MRO_B MRO_C/;
package MRO_E;
our @ISA = qw/MRO_A MRO_B MRO_C/;
package MRO_F;
our @ISA = qw/MRO_D MRO_E/;
}
my @MFO_F_DFS = qw/MRO_F MRO_D MRO_A MRO_B MRO_C MRO_E/;
my @MFO_F_C3 = qw/MRO_F MRO_D MRO_E MRO_A MRO_B MRO_C/;
is(mro::get_mro('MRO_F'), 'dfs');
ok(eq_array(
mro::get_linear_isa('MRO_F'), \@MFO_F_DFS
));
ok(eq_array(mro::get_linear_isa('MRO_F', 'dfs'), \@MFO_F_DFS));
ok(eq_array(mro::get_linear_isa('MRO_F', 'c3'), \@MFO_F_C3));
eval{mro::get_linear_isa('MRO_F', 'C3')};
like($@, qr/^Invalid mro name: 'C3'/);
mro::set_mro('MRO_F', 'c3');
is(mro::get_mro('MRO_F'), 'c3');
ok(eq_array(
mro::get_linear_isa('MRO_F'), \@MFO_F_C3
));
ok(eq_array(mro::get_linear_isa('MRO_F', 'dfs'), \@MFO_F_DFS));
ok(eq_array(mro::get_linear_isa('MRO_F', 'c3'), \@MFO_F_C3));
eval{mro::get_linear_isa('MRO_F', 'C3')};
like($@, qr/^Invalid mro name: 'C3'/);
my @isarev = sort { $a cmp $b } @{mro::get_isarev('MRO_B')};
ok(eq_array(
\@isarev,
[qw/MRO_D MRO_E MRO_F/]
));
ok(!mro::is_universal('MRO_B'));
@UNIVERSAL::ISA = qw/MRO_F/;
ok(mro::is_universal('MRO_B'));
@UNIVERSAL::ISA = ();
ok(mro::is_universal('MRO_B'));
# is_universal, get_mro, and get_linear_isa should
# handle non-existant packages sanely
ok(!mro::is_universal('Does_Not_Exist'));
is(mro::get_mro('Also_Does_Not_Exist'), 'dfs');
ok(eq_array(
mro::get_linear_isa('Does_Not_Exist_Three'),
[qw/Does_Not_Exist_Three/]
));
# Assigning @ISA via globref
{
package MRO_TestBase;
sub testfunc { return 123 }
package MRO_TestOtherBase;
sub testfunctwo { return 321 }
package MRO_M; our @ISA = qw/MRO_TestBase/;
}
*MRO_N::ISA = *MRO_M::ISA;
is(eval { MRO_N->testfunc() }, 123);
# XXX TODO (when there's a way to backtrack through a glob's aliases)
# push(@MRO_M::ISA, 'MRO_TestOtherBase');
# is(eval { MRO_N->testfunctwo() }, 321);
# Simple DESTROY Baseline
{
my $x = 0;
my $obj;
{
package DESTROY_MRO_Baseline;
sub new { bless {} => shift }
sub DESTROY { $x++ }
package DESTROY_MRO_Baseline_Child;
our @ISA = qw/DESTROY_MRO_Baseline/;
}
$obj = DESTROY_MRO_Baseline->new();
undef $obj;
is($x, 1);
$obj = DESTROY_MRO_Baseline_Child->new();
undef $obj;
is($x, 2);
}
# Dynamic DESTROY
{
my $x = 0;
my $obj;
{
package DESTROY_MRO_Dynamic;
sub new { bless {} => shift }
package DESTROY_MRO_Dynamic_Child;
our @ISA = qw/DESTROY_MRO_Dynamic/;
}
$obj = DESTROY_MRO_Dynamic->new();
undef $obj;
is($x, 0);
$obj = DESTROY_MRO_Dynamic_Child->new();
undef $obj;
is($x, 0);
no warnings 'once';
*DESTROY_MRO_Dynamic::DESTROY = sub { $x++ };
$obj = DESTROY_MRO_Dynamic->new();
undef $obj;
is($x, 1);
$obj = DESTROY_MRO_Dynamic_Child->new();
undef $obj;
is($x, 2);
}
# clearing @ISA in different ways
# some are destructive to the package, hence the new
# package name each time
{
no warnings 'uninitialized';
{
package ISACLEAR;
our @ISA = qw/XX YY ZZ/;
}
# baseline
ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR XX YY ZZ/]));
# this looks dumb, but it preserves existing behavior for compatibility
# (undefined @ISA elements treated as "main")
$ISACLEAR::ISA[1] = undef;
ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR XX main ZZ/]));
# undef the array itself
undef @ISACLEAR::ISA;
ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR/]));
# Now, clear more than one package's @ISA at once
{
package ISACLEAR1;
our @ISA = qw/WW XX/;
package ISACLEAR2;
our @ISA = qw/YY ZZ/;
}
# baseline
ok(eq_array(mro::get_linear_isa('ISACLEAR1'),[qw/ISACLEAR1 WW XX/]));
ok(eq_array(mro::get_linear_isa('ISACLEAR2'),[qw/ISACLEAR2 YY ZZ/]));
(@ISACLEAR1::ISA, @ISACLEAR2::ISA) = ();
ok(eq_array(mro::get_linear_isa('ISACLEAR1'),[qw/ISACLEAR1/]));
ok(eq_array(mro::get_linear_isa('ISACLEAR2'),[qw/ISACLEAR2/]));
}
# Check that recursion bails out "cleanly" in a variety of cases
# (as opposed to say, bombing the interpreter or something)
{
my @recurse_codes = (
'@MRO_R1::ISA = "MRO_R2"; @MRO_R2::ISA = "MRO_R1";',
'@MRO_R3::ISA = "MRO_R4"; push(@MRO_R4::ISA, "MRO_R3");',
'@MRO_R5::ISA = "MRO_R6"; @MRO_R6::ISA = qw/XX MRO_R5 YY/;',
'@MRO_R7::ISA = "MRO_R8"; push(@MRO_R8::ISA, qw/XX MRO_R7 YY/)',
);
foreach my $code (@recurse_codes) {
eval $code;
ok($@ =~ /Recursive inheritance detected/);
}
}
# Check that SUPER caches get invalidated correctly
{
{
package SUPERTEST;
sub new { bless {} => shift }
sub foo { $_[1]+1 }
package SUPERTEST::MID;
our @ISA = 'SUPERTEST';
package SUPERTEST::KID;
our @ISA = 'SUPERTEST::MID';
sub foo { my $s = shift; $s->SUPER::foo(@_) }
package SUPERTEST::REBASE;
sub foo { $_[1]+3 }
}
my $stk_obj = SUPERTEST::KID->new();
is($stk_obj->foo(1), 2);
{ no warnings 'redefine';
*SUPERTEST::foo = sub { $_[1]+2 };
}
is($stk_obj->foo(2), 4);
@SUPERTEST::MID::ISA = 'SUPERTEST::REBASE';
is($stk_obj->foo(3), 6);
}
|