This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix totally broken caching in UNIVERSAL::isa() (from
[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';
93430cb4 8 unshift @INC, '../lib' if -d '../lib';
46e4b22b 9 $| = 1;
e09f3e01
G
10}
11
46e4b22b 12print "1..80\n";
44a8e56a
PP
13
14$a = {};
15bless $a, "Bob";
ff0cee69
PP
16print "not " unless $a->isa("Bob");
17print "ok 1\n";
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);
27sub drink {}
28sub new { bless {} }
44a8e56a 29
e09f3e01
G
30$Alice::VERSION = 2.718;
31
46e4b22b
GS
32{
33 package Cedric;
34 our @ISA;
35 use base qw(Human);
36}
37
38{
39 package Programmer;
40 our $VERSION = 1.667;
41
42 sub write_perl { 1 }
43}
44
44a8e56a 45package main;
e09f3e01
G
46
47my $i = 2;
48sub test { print "not " unless shift; print "ok $i\n"; $i++; }
49
ff0cee69 50$a = new Alice;
44a8e56a 51
e09f3e01 52test $a->isa("Alice");
44a8e56a 53
e09f3e01
G
54test $a->isa("Bob");
55
56test $a->isa("Female");
57
58test $a->isa("Human");
59
60test ! $a->isa("Male");
61
46e4b22b
GS
62test ! $a->isa('Programmer');
63
e09f3e01
G
64test $a->can("drink");
65
66test $a->can("eat");
67
68test ! $a->can("sleep");
69
46e4b22b
GS
70test (!Cedric->isa('Programmer'));
71
72test (Cedric->isa('Human'));
73
74push(@Cedric::ISA,'Programmer');
75
76test (Cedric->isa('Programmer'));
77
78{
79 package Alice;
80 base::->import('Programmer');
81}
82
83test $a->isa('Programmer');
84test $a->isa("Female");
85
86@Cedric::ISA = qw(Bob);
87
88test (!Cedric->isa('Programmer'));
89
e09f3e01
G
90my $b = 'abc';
91my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
92my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
93for ($p=0; $p < @refs; $p++) {
94 for ($q=0; $q < @vals; $q++) {
95 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
96 };
97};
98
99test ! UNIVERSAL::can(23, "can");
100
101test $a->can("VERSION");
102
103test $a->can("can");
84902520 104test ! $a->can("export_tags"); # a method in Exporter
e09f3e01
G
105
106test (eval { $a->VERSION }) == 2.718;
107
108test ! (eval { $a->VERSION(2.719) }) &&
ae165b0c 109 $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
44a8e56a 110
e09f3e01 111test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 112
e09f3e01 113my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
9d116dd7
JH
114if ('a' lt 'A') {
115 test $subs eq "can isa VERSION";
116} else {
117 test $subs eq "VERSION can isa";
118}
ff0cee69 119
e09f3e01 120test $a->isa("UNIVERSAL");
ff0cee69 121
84902520 122# now use UNIVERSAL.pm and see what changes
e09f3e01 123eval "use UNIVERSAL";
ff0cee69 124
e09f3e01 125test $a->isa("UNIVERSAL");
44a8e56a 126
46e4b22b 127my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 128# XXX import being here is really a bug
9d116dd7
JH
129if ('a' lt 'A') {
130 test $sub2 eq "can import isa VERSION";
131} else {
132 test $sub2 eq "VERSION can import isa";
133}
44a8e56a 134
e09f3e01
G
135eval 'sub UNIVERSAL::sleep {}';
136test $a->can("sleep");
44a8e56a 137
e09f3e01 138test ! UNIVERSAL::can($b, "can");
84902520
TB
139
140test ! $a->can("export_tags"); # a method in Exporter
83f7a2bc
GS
141
142test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');