This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
printf "%vd", "version" should not SEGV
[perl5.git] / t / op / universal.t
CommitLineData
44a8e56a
PP
1#!./perl
2#
3# check UNIVERSAL
4#
5
e09f3e01
G
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
46e4b22b 9 $| = 1;
3e44d7c6 10 require "./test.pl";
e09f3e01
G
11}
12
072cb3f5 13plan tests => 129;
44a8e56a
PP
14
15$a = {};
16bless $a, "Bob";
3e44d7c6 17ok $a->isa("Bob");
44a8e56a 18
ff0cee69
PP
19package Human;
20sub eat {}
44a8e56a 21
ff0cee69
PP
22package Female;
23@ISA=qw(Human);
44a8e56a 24
ff0cee69
PP
25package Alice;
26@ISA=qw(Bob Female);
39d11b7f
TB
27sub sing;
28sub drink { return "drinking " . $_[1] }
ff0cee69 29sub new { bless {} }
44a8e56a 30
e09f3e01
G
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
G
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
G
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
e09f3e01 125my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
ea8fae29
BS
126## The test for import here is *not* because we want to ensure that UNIVERSAL
127## can always import; it is an historical accident that UNIVERSAL can import.
9d116dd7 128if ('a' lt 'A') {
4bf88892 129 is $subs, "can import isa DOES VERSION";
9d116dd7 130} else {
4bf88892 131 is $subs, "DOES VERSION can import isa";
9d116dd7 132}
ff0cee69 133
3e44d7c6 134ok $a->isa("UNIVERSAL");
ff0cee69 135
3e44d7c6 136ok ! UNIVERSAL::isa([], "UNIVERSAL");
b4c2bf25 137
3e44d7c6 138ok ! UNIVERSAL::can({}, "can");
b4c2bf25 139
3e44d7c6 140ok UNIVERSAL::isa(Alice => "UNIVERSAL");
b4c2bf25 141
3e44d7c6 142cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
b4c2bf25 143
84902520 144# now use UNIVERSAL.pm and see what changes
e09f3e01 145eval "use UNIVERSAL";
ff0cee69 146
3e44d7c6 147ok $a->isa("UNIVERSAL");
44a8e56a 148
46e4b22b 149my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 150# XXX import being here is really a bug
9d116dd7 151if ('a' lt 'A') {
4bf88892 152 is $sub2, "can import isa DOES VERSION";
9d116dd7 153} else {
4bf88892 154 is $sub2, "DOES VERSION can import isa";
9d116dd7 155}
44a8e56a 156
e09f3e01 157eval 'sub UNIVERSAL::sleep {}';
3e44d7c6 158ok $a->can("sleep");
44a8e56a 159
3e44d7c6 160ok ! UNIVERSAL::can($b, "can");
84902520 161
3e44d7c6 162ok ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 163
3e44d7c6 164ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29
BS
165
166{
167 package Pickup;
168 use UNIVERSAL qw( isa can VERSION );
169
3e44d7c6
MS
170 ::ok isa "Pickup", UNIVERSAL;
171 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
172 ::ok VERSION "UNIVERSAL" ;
ea8fae29 173}
253ecd6d
RGS
174
175{
176 # test isa() and can() on magic variables
177 "Human" =~ /(.*)/;
3e44d7c6
MS
178 ok $1->isa("Human");
179 ok $1->can("eat");
253ecd6d
RGS
180 package HumanTie;
181 sub TIESCALAR { bless {} }
182 sub FETCH { "Human" }
183 tie my($x), "HumanTie";
3e44d7c6
MS
184 ::ok $x->isa("Human");
185 ::ok $x->can("eat");
253ecd6d 186}
a1d407e8
DM
187
188# bugid 3284
189# a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
190
191@X::ISA=();
192my $x = {}; bless $x, 'X';
3e44d7c6
MS
193ok $x->isa('UNIVERSAL');
194ok $x->isa('UNIVERSAL');
2bfd5681
MS
195
196
197# Check that the "historical accident" of UNIVERSAL having an import()
198# method doesn't effect anyone else.
199eval { Some::Package->import("bar") };
3e44d7c6
MS
200is $@, '';
201
202
203# This segfaulted in a blead.
204fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
205
1f656fcf
FC
206# So did this.
207fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
208
cbc021f9 209package Foo;
210
4bf88892 211sub DOES { 1 }
cbc021f9 212
213package Bar;
214
215@Bar::ISA = 'Foo';
216
217package Baz;
218
219package main;
4bf88892
RGS
220ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
221ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
222ok( Bar->DOES( 'Foo' ), '... even when inherited' );
223ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
224ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
ae6d515f 225
072cb3f5
BF
226ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
227ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
228
ae6d515f
RGS
229package Pig;
230package Bodine;
231Bodine->isa('Pig');
232*isa = \&UNIVERSAL::isa;
233eval { isa({}, 'HASH') };
59e7186f
RGS
234::is($@, '', "*isa correctly found");
235
236package main;
237eval { UNIVERSAL::DOES([], "foo") };
238like( $@, qr/Can't call method "DOES" on unblessed reference/,
239 'DOES call error message says DOES, not isa' );
b91ba1f2
NC
240
241# Tests for can seem to be split between here and method.t
242# Add the verbatim perl code mentioned in the comments of
243# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
244# but never actually tested.
245is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
5782d502
NC
246
247@splatt::ISA = 'zlopp';
248ok (splatt->isa('zlopp'));
249ok (!splatt->isa('plop'));
250
251# This should reset the ->isa lookup cache
252@splatt::ISA = 'plop';
253# And here is the new truth.
254ok (!splatt->isa('zlopp'));
255ok (splatt->isa('plop'));
256
cd477a63
RGS
257use warnings "deprecated";
258{
259 my $m;
260 local $SIG{__WARN__} = sub { $m = $_[0] };
252143cd 261 eval "use UNIVERSAL 'can'";
cd477a63 262 like($m, qr/^UNIVERSAL->import is deprecated/,
252143cd
RS
263 "deprecation warning for UNIVERSAL->import('can')");
264
265 undef $m;
266 eval "use UNIVERSAL";
267 is($m, undef,
268 "no deprecation warning for UNIVERSAL->import");
cd477a63 269}
27889255
B
270
271# Test: [perl #66112]: change @ISA inside sub isa
272{
273 package RT66112::A;
274
275 package RT66112::B;
276
277 sub isa {
278 my $self = shift;
279 @ISA = qw/RT66112::A/;
280 return $self->SUPER::isa(@_);
281 }
282
283 package RT66112::C;
284
285 package RT66112::D;
286
287 sub isa {
288 my $self = shift;
289 @RT66112::E::ISA = qw/RT66112::A/;
290 return $self->SUPER::isa(@_);
291 }
292
293 package RT66112::E;
294
295 package main;
296
297 @RT66112::B::ISA = qw//;
298 @RT66112::C::ISA = qw/RT66112::B/;
299 @RT66112::T1::ISA = qw/RT66112::C/;
300 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
301
302 @RT66112::B::ISA = qw//;
303 @RT66112::C::ISA = qw/RT66112::B/;
304 @RT66112::T2::ISA = qw/RT66112::C/;
305 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
306
307 @RT66112::B::ISA = qw//;
308 @RT66112::C::ISA = qw/RT66112::B/;
309 @RT66112::T3::ISA = qw/RT66112::C/;
80ebaca2 310 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
311
312 @RT66112::E::ISA = qw/RT66112::D/;
313 @RT66112::T4::ISA = qw/RT66112::E/;
314 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
315
316 @RT66112::E::ISA = qw/RT66112::D/;
317 @RT66112::T5::ISA = qw/RT66112::E/;
318 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
319
320 @RT66112::E::ISA = qw/RT66112::D/;
321 @RT66112::T6::ISA = qw/RT66112::E/;
322 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
323}