This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[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
1178d2cf 13plan tests => 143;
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
68b40612 110ok UNIVERSAL::can(23, "can");
cee59a6a
FC
111++${"23::foo"};
112ok UNIVERSAL::can("23", "can"), '"23" can can when the pack exists';
113ok UNIVERSAL::can(23, "can"), '23 can can when the pack exists';
4178f891
FC
114sub IO::Handle::turn {}
115ok UNIVERSAL::can(*STDOUT, 'turn'), 'globs with IOs can';
116ok UNIVERSAL::can(\*STDOUT, 'turn'), 'globrefs with IOs can';
2bde9ae6 117ok UNIVERSAL::can("STDOUT", 'turn'), 'IO barewords can';
e09f3e01 118
3e44d7c6 119ok $a->can("VERSION");
e09f3e01 120
3e44d7c6
MS
121ok $a->can("can");
122ok ! $a->can("export_tags"); # a method in Exporter
e09f3e01 123
3e44d7c6 124cmp_ok eval { $a->VERSION }, '==', 2.718;
e09f3e01 125
3e44d7c6 126ok ! (eval { $a->VERSION(2.719) });
ac0e6a2f 127like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
44a8e56a 128
3e44d7c6
MS
129ok (eval { $a->VERSION(2.718) });
130is $@, '';
ff0cee69 131
c2a3bbbf
FC
132ok ! (eval { $a->VERSION("version") });
133like $@, qr/^Invalid version format/;
134
135$aversion::VERSION = "version";
136ok ! (eval { aversion->VERSION(2.719) });
137like $@, qr/^Invalid version format/;
138
e09f3e01 139my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
9d116dd7 140if ('a' lt 'A') {
1178d2cf 141 is $subs, "can isa DOES VERSION";
9d116dd7 142} else {
1178d2cf 143 is $subs, "DOES VERSION can isa";
9d116dd7 144}
ff0cee69 145
3e44d7c6 146ok $a->isa("UNIVERSAL");
ff0cee69 147
3e44d7c6 148ok ! UNIVERSAL::isa([], "UNIVERSAL");
b4c2bf25 149
3e44d7c6 150ok ! UNIVERSAL::can({}, "can");
b4c2bf25 151
3e44d7c6 152ok UNIVERSAL::isa(Alice => "UNIVERSAL");
b4c2bf25 153
3e44d7c6 154cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
b4c2bf25 155
84902520 156# now use UNIVERSAL.pm and see what changes
e09f3e01 157eval "use UNIVERSAL";
ff0cee69 158
3e44d7c6 159ok $a->isa("UNIVERSAL");
44a8e56a 160
46e4b22b 161my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 162# XXX import being here is really a bug
9d116dd7 163if ('a' lt 'A') {
4bf88892 164 is $sub2, "can import isa DOES VERSION";
9d116dd7 165} else {
4bf88892 166 is $sub2, "DOES VERSION can import isa";
9d116dd7 167}
44a8e56a 168
e09f3e01 169eval 'sub UNIVERSAL::sleep {}';
3e44d7c6 170ok $a->can("sleep");
44a8e56a 171
68b40612 172ok UNIVERSAL::can($b, "can");
84902520 173
3e44d7c6 174ok ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 175
3e44d7c6 176ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29
BS
177
178{
253ecd6d
RGS
179 # test isa() and can() on magic variables
180 "Human" =~ /(.*)/;
3e44d7c6
MS
181 ok $1->isa("Human");
182 ok $1->can("eat");
253ecd6d
RGS
183 package HumanTie;
184 sub TIESCALAR { bless {} }
185 sub FETCH { "Human" }
186 tie my($x), "HumanTie";
3e44d7c6
MS
187 ::ok $x->isa("Human");
188 ::ok $x->can("eat");
253ecd6d 189}
a1d407e8
DM
190
191# bugid 3284
192# a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
193
194@X::ISA=();
195my $x = {}; bless $x, 'X';
3e44d7c6
MS
196ok $x->isa('UNIVERSAL');
197ok $x->isa('UNIVERSAL');
2bfd5681
MS
198
199
200# Check that the "historical accident" of UNIVERSAL having an import()
201# method doesn't effect anyone else.
202eval { Some::Package->import("bar") };
3e44d7c6
MS
203is $@, '';
204
205
206# This segfaulted in a blead.
207fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
208
1f656fcf
FC
209# So did this.
210fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
211
cbc021f9 212package Foo;
213
4bf88892 214sub DOES { 1 }
cbc021f9 215
216package Bar;
217
218@Bar::ISA = 'Foo';
219
220package Baz;
221
222package main;
4bf88892
RGS
223ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
224ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
225ok( Bar->DOES( 'Foo' ), '... even when inherited' );
226ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
227ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
ae6d515f 228
072cb3f5
BF
229ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
230ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
231
ae6d515f
RGS
232package Pig;
233package Bodine;
234Bodine->isa('Pig');
235*isa = \&UNIVERSAL::isa;
236eval { isa({}, 'HASH') };
59e7186f
RGS
237::is($@, '', "*isa correctly found");
238
239package main;
240eval { UNIVERSAL::DOES([], "foo") };
241like( $@, qr/Can't call method "DOES" on unblessed reference/,
242 'DOES call error message says DOES, not isa' );
b91ba1f2
NC
243
244# Tests for can seem to be split between here and method.t
245# Add the verbatim perl code mentioned in the comments of
246# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
247# but never actually tested.
248is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
5782d502
NC
249
250@splatt::ISA = 'zlopp';
251ok (splatt->isa('zlopp'));
252ok (!splatt->isa('plop'));
253
254# This should reset the ->isa lookup cache
255@splatt::ISA = 'plop';
256# And here is the new truth.
257ok (!splatt->isa('zlopp'));
258ok (splatt->isa('plop'));
259
cd477a63
RGS
260use warnings "deprecated";
261{
262 my $m;
263 local $SIG{__WARN__} = sub { $m = $_[0] };
252143cd 264 eval "use UNIVERSAL 'can'";
1178d2cf
DIM
265 like($@, qr/^UNIVERSAL does not export anything\b/,
266 "error for UNIVERSAL->import('can')");
267 is($m, undef,
268 "no deprecation warning for UNIVERSAL->import('can')");
252143cd
RS
269
270 undef $m;
271 eval "use UNIVERSAL";
1178d2cf
DIM
272 is($@, "",
273 "no error for UNIVERSAL->import");
252143cd
RS
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}
68b40612
JL
331
332ok(Undeclared->can("can"));
333sub Undeclared::foo { }
334ok(Undeclared->can("foo"));
335ok(!Undeclared->can("something_else"));
61c2a935
JL
336
337ok(Undeclared->isa("UNIVERSAL"));
d70bfb04
JL
338
339# keep this at the end to avoid messing up earlier tests, since it modifies
340# @UNIVERSAL::ISA
341@UNIVERSAL::ISA = ('UniversalParent');
342{ package UniversalIsaTest1; }
343ok(UniversalIsaTest1->isa('UniversalParent'));
344ok(UniversalIsaTest2->isa('UniversalParent'));