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 / universal.t
CommitLineData
44a8e56a 1#!./perl
2#
3# check UNIVERSAL
4#
5
e09f3e01
MG
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
46e4b22b 9 $| = 1;
3e44d7c6 10 require "./test.pl";
e09f3e01
MG
11}
12
c2a3bbbf 13plan tests => 133;
44a8e56a 14
15$a = {};
16bless $a, "Bob";
3e44d7c6 17ok $a->isa("Bob");
44a8e56a 18
ff0cee69 19package Human;
20sub eat {}
44a8e56a 21
ff0cee69 22package Female;
23@ISA=qw(Human);
44a8e56a 24
ff0cee69 25package Alice;
26@ISA=qw(Bob Female);
39d11b7f
TB
27sub sing;
28sub drink { return "drinking " . $_[1] }
ff0cee69 29sub new { bless {} }
44a8e56a 30
e09f3e01
MG
31$Alice::VERSION = 2.718;
32
46e4b22b
GS
33{
34 package Cedric;
35 our @ISA;
36 use base qw(Human);
37}
38
39{
40 package Programmer;
41 our $VERSION = 1.667;
42
43 sub write_perl { 1 }
44}
45
44a8e56a 46package main;
e09f3e01 47
3e44d7c6 48
e09f3e01 49
ff0cee69 50$a = new Alice;
44a8e56a 51
3e44d7c6
MS
52ok $a->isa("Alice");
53ok $a->isa("main::Alice"); # check that alternate class names work
44a8e56a 54
3e44d7c6 55ok(("main::Alice"->new)->isa("Alice"));
178d71da 56
3e44d7c6
MS
57ok $a->isa("Bob");
58ok $a->isa("main::Bob");
e09f3e01 59
3e44d7c6 60ok $a->isa("Female");
e09f3e01 61
072cb3f5
BF
62ok ! $a->isa("Female\0NOT REALLY!"), "->isa is nul-clean.";
63
3e44d7c6 64ok $a->isa("Human");
e09f3e01 65
3e44d7c6 66ok ! $a->isa("Male");
e09f3e01 67
3e44d7c6 68ok ! $a->isa('Programmer');
46e4b22b 69
3e44d7c6 70ok $a->isa("HASH");
986114cf 71
3e44d7c6 72ok $a->can("eat");
072cb3f5 73ok ! $a->can("eat\0Except not!"), "->can is nul-clean.";
3e44d7c6
MS
74ok ! $a->can("sleep");
75ok my $ref = $a->can("drink"); # returns a coderef
76is $a->$ref("tea"), "drinking tea"; # ... which works
77ok $ref = $a->can("sing");
444e39b5 78eval { $a->$ref() };
3e44d7c6 79ok $@; # ... but not if no actual subroutine
e09f3e01 80
3e44d7c6 81ok (!Cedric->isa('Programmer'));
46e4b22b 82
3e44d7c6 83ok (Cedric->isa('Human'));
46e4b22b
GS
84
85push(@Cedric::ISA,'Programmer');
86
3e44d7c6 87ok (Cedric->isa('Programmer'));
46e4b22b
GS
88
89{
90 package Alice;
91 base::->import('Programmer');
92}
93
3e44d7c6
MS
94ok $a->isa('Programmer');
95ok $a->isa("Female");
46e4b22b
GS
96
97@Cedric::ISA = qw(Bob);
98
3e44d7c6 99ok (!Cedric->isa('Programmer'));
46e4b22b 100
e09f3e01
MG
101my $b = 'abc';
102my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
103my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
104for ($p=0; $p < @refs; $p++) {
105 for ($q=0; $q < @vals; $q++) {
3e44d7c6 106 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
e09f3e01
MG
107 };
108};
109
3e44d7c6 110ok ! UNIVERSAL::can(23, "can");
e09f3e01 111
3e44d7c6 112ok $a->can("VERSION");
e09f3e01 113
3e44d7c6
MS
114ok $a->can("can");
115ok ! $a->can("export_tags"); # a method in Exporter
e09f3e01 116
3e44d7c6 117cmp_ok eval { $a->VERSION }, '==', 2.718;
e09f3e01 118
3e44d7c6 119ok ! (eval { $a->VERSION(2.719) });
ac0e6a2f 120like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
44a8e56a 121
3e44d7c6
MS
122ok (eval { $a->VERSION(2.718) });
123is $@, '';
ff0cee69 124
c2a3bbbf
FC
125ok ! (eval { $a->VERSION("version") });
126like $@, qr/^Invalid version format/;
127
128$aversion::VERSION = "version";
129ok ! (eval { aversion->VERSION(2.719) });
130like $@, qr/^Invalid version format/;
131
e09f3e01 132my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
ea8fae29
BS
133## The test for import here is *not* because we want to ensure that UNIVERSAL
134## can always import; it is an historical accident that UNIVERSAL can import.
9d116dd7 135if ('a' lt 'A') {
4bf88892 136 is $subs, "can import isa DOES VERSION";
9d116dd7 137} else {
4bf88892 138 is $subs, "DOES VERSION can import isa";
9d116dd7 139}
ff0cee69 140
3e44d7c6 141ok $a->isa("UNIVERSAL");
ff0cee69 142
3e44d7c6 143ok ! UNIVERSAL::isa([], "UNIVERSAL");
b4c2bf25 144
3e44d7c6 145ok ! UNIVERSAL::can({}, "can");
b4c2bf25 146
3e44d7c6 147ok UNIVERSAL::isa(Alice => "UNIVERSAL");
b4c2bf25 148
3e44d7c6 149cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
b4c2bf25 150
84902520 151# now use UNIVERSAL.pm and see what changes
e09f3e01 152eval "use UNIVERSAL";
ff0cee69 153
3e44d7c6 154ok $a->isa("UNIVERSAL");
44a8e56a 155
46e4b22b 156my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 157# XXX import being here is really a bug
9d116dd7 158if ('a' lt 'A') {
4bf88892 159 is $sub2, "can import isa DOES VERSION";
9d116dd7 160} else {
4bf88892 161 is $sub2, "DOES VERSION can import isa";
9d116dd7 162}
44a8e56a 163
e09f3e01 164eval 'sub UNIVERSAL::sleep {}';
3e44d7c6 165ok $a->can("sleep");
44a8e56a 166
3e44d7c6 167ok ! UNIVERSAL::can($b, "can");
84902520 168
3e44d7c6 169ok ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 170
3e44d7c6 171ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29
BS
172
173{
174 package Pickup;
175 use UNIVERSAL qw( isa can VERSION );
176
3e44d7c6
MS
177 ::ok isa "Pickup", UNIVERSAL;
178 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
179 ::ok VERSION "UNIVERSAL" ;
ea8fae29 180}
253ecd6d
RGS
181
182{
183 # test isa() and can() on magic variables
184 "Human" =~ /(.*)/;
3e44d7c6
MS
185 ok $1->isa("Human");
186 ok $1->can("eat");
253ecd6d
RGS
187 package HumanTie;
188 sub TIESCALAR { bless {} }
189 sub FETCH { "Human" }
190 tie my($x), "HumanTie";
3e44d7c6
MS
191 ::ok $x->isa("Human");
192 ::ok $x->can("eat");
253ecd6d 193}
a1d407e8
DM
194
195# bugid 3284
196# a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
197
198@X::ISA=();
199my $x = {}; bless $x, 'X';
3e44d7c6
MS
200ok $x->isa('UNIVERSAL');
201ok $x->isa('UNIVERSAL');
2bfd5681
MS
202
203
204# Check that the "historical accident" of UNIVERSAL having an import()
205# method doesn't effect anyone else.
206eval { Some::Package->import("bar") };
3e44d7c6
MS
207is $@, '';
208
209
210# This segfaulted in a blead.
211fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
212
1f656fcf
FC
213# So did this.
214fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
215
cbc021f9 216package Foo;
217
4bf88892 218sub DOES { 1 }
cbc021f9 219
220package Bar;
221
222@Bar::ISA = 'Foo';
223
224package Baz;
225
226package main;
4bf88892
RGS
227ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
228ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
229ok( Bar->DOES( 'Foo' ), '... even when inherited' );
230ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
231ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
ae6d515f 232
072cb3f5
BF
233ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
234ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
235
ae6d515f
RGS
236package Pig;
237package Bodine;
238Bodine->isa('Pig');
239*isa = \&UNIVERSAL::isa;
240eval { isa({}, 'HASH') };
59e7186f
RGS
241::is($@, '', "*isa correctly found");
242
243package main;
244eval { UNIVERSAL::DOES([], "foo") };
245like( $@, qr/Can't call method "DOES" on unblessed reference/,
246 'DOES call error message says DOES, not isa' );
b91ba1f2
NC
247
248# Tests for can seem to be split between here and method.t
249# Add the verbatim perl code mentioned in the comments of
250# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
251# but never actually tested.
252is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
5782d502
NC
253
254@splatt::ISA = 'zlopp';
255ok (splatt->isa('zlopp'));
256ok (!splatt->isa('plop'));
257
258# This should reset the ->isa lookup cache
259@splatt::ISA = 'plop';
260# And here is the new truth.
261ok (!splatt->isa('zlopp'));
262ok (splatt->isa('plop'));
263
cd477a63
RGS
264use warnings "deprecated";
265{
266 my $m;
267 local $SIG{__WARN__} = sub { $m = $_[0] };
252143cd 268 eval "use UNIVERSAL 'can'";
cd477a63 269 like($m, qr/^UNIVERSAL->import is deprecated/,
252143cd
RS
270 "deprecation warning for UNIVERSAL->import('can')");
271
272 undef $m;
273 eval "use UNIVERSAL";
274 is($m, undef,
275 "no deprecation warning for UNIVERSAL->import");
cd477a63 276}
27889255
B
277
278# Test: [perl #66112]: change @ISA inside sub isa
279{
280 package RT66112::A;
281
282 package RT66112::B;
283
284 sub isa {
285 my $self = shift;
286 @ISA = qw/RT66112::A/;
287 return $self->SUPER::isa(@_);
288 }
289
290 package RT66112::C;
291
292 package RT66112::D;
293
294 sub isa {
295 my $self = shift;
296 @RT66112::E::ISA = qw/RT66112::A/;
297 return $self->SUPER::isa(@_);
298 }
299
300 package RT66112::E;
301
302 package main;
303
304 @RT66112::B::ISA = qw//;
305 @RT66112::C::ISA = qw/RT66112::B/;
306 @RT66112::T1::ISA = qw/RT66112::C/;
307 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
308
309 @RT66112::B::ISA = qw//;
310 @RT66112::C::ISA = qw/RT66112::B/;
311 @RT66112::T2::ISA = qw/RT66112::C/;
312 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
313
314 @RT66112::B::ISA = qw//;
315 @RT66112::C::ISA = qw/RT66112::B/;
316 @RT66112::T3::ISA = qw/RT66112::C/;
80ebaca2 317 ok(RT66112::T3->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T3 isa RT66112::A)") or require mro, diag "@{mro::get_linear_isa('RT66112::T3')}";
27889255
B
318
319 @RT66112::E::ISA = qw/RT66112::D/;
320 @RT66112::T4::ISA = qw/RT66112::E/;
321 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
322
323 @RT66112::E::ISA = qw/RT66112::D/;
324 @RT66112::T5::ISA = qw/RT66112::E/;
325 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
326
327 @RT66112::E::ISA = qw/RT66112::D/;
328 @RT66112::T6::ISA = qw/RT66112::E/;
329 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
330}