Re: Why *not* use UNIVERSAL qw( isa can ) ; ??
[p5sagit/p5-mst-13.2.git] / t / op / universal.t
CommitLineData
44a8e56a 1#!./perl
2#
3# check UNIVERSAL
4#
5
e09f3e01 6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
46e4b22b 9 $| = 1;
e09f3e01 10}
11
ea8fae29 12print "1..90\n";
44a8e56a 13
14$a = {};
15bless $a, "Bob";
ff0cee69 16print "not " unless $a->isa("Bob");
17print "ok 1\n";
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 27sub sing;
28sub drink { return "drinking " . $_[1] }
ff0cee69 29sub new { bless {} }
44a8e56a 30
e09f3e01 31$Alice::VERSION = 2.718;
32
46e4b22b 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
39d11b7f 48{ my $i = 2;
ea8fae29 49 sub test {
50 print "not " unless $_[0];
51 print "ok ", $i++;
52 print "# at ", (caller)[1], ", line ", (caller)[2] unless $_[0];
53 print "\n";
54 }
39d11b7f 55}
e09f3e01 56
ff0cee69 57$a = new Alice;
44a8e56a 58
e09f3e01 59test $a->isa("Alice");
44a8e56a 60
e09f3e01 61test $a->isa("Bob");
62
63test $a->isa("Female");
64
65test $a->isa("Human");
66
67test ! $a->isa("Male");
68
46e4b22b 69test ! $a->isa('Programmer');
70
e09f3e01 71test $a->can("eat");
e09f3e01 72test ! $a->can("sleep");
39d11b7f 73test my $ref = $a->can("drink"); # returns a coderef
74test $a->$ref("tea") eq "drinking tea"; # ... which works
75test $ref = $a->can("sing");
444e39b5 76eval { $a->$ref() };
39d11b7f 77test $@; # ... but not if no actual subroutine
e09f3e01 78
46e4b22b 79test (!Cedric->isa('Programmer'));
80
81test (Cedric->isa('Human'));
82
83push(@Cedric::ISA,'Programmer');
84
85test (Cedric->isa('Programmer'));
86
87{
88 package Alice;
89 base::->import('Programmer');
90}
91
92test $a->isa('Programmer');
93test $a->isa("Female");
94
95@Cedric::ISA = qw(Bob);
96
97test (!Cedric->isa('Programmer'));
98
e09f3e01 99my $b = 'abc';
100my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
101my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
102for ($p=0; $p < @refs; $p++) {
103 for ($q=0; $q < @vals; $q++) {
104 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
105 };
106};
107
108test ! UNIVERSAL::can(23, "can");
109
110test $a->can("VERSION");
111
112test $a->can("can");
84902520 113test ! $a->can("export_tags"); # a method in Exporter
e09f3e01 114
115test (eval { $a->VERSION }) == 2.718;
116
117test ! (eval { $a->VERSION(2.719) }) &&
ae165b0c 118 $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
44a8e56a 119
e09f3e01 120test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 121
e09f3e01 122my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
ea8fae29 123## The test for import here is *not* because we want to ensure that UNIVERSAL
124## can always import; it is an historical accident that UNIVERSAL can import.
9d116dd7 125if ('a' lt 'A') {
ea8fae29 126 test $subs eq "can import isa VERSION";
9d116dd7 127} else {
ea8fae29 128 test $subs eq "VERSION can import isa";
9d116dd7 129}
ff0cee69 130
e09f3e01 131test $a->isa("UNIVERSAL");
ff0cee69 132
b4c2bf25 133test ! UNIVERSAL::isa([], "UNIVERSAL");
134
135test ! UNIVERSAL::can({}, "can");
136
137test UNIVERSAL::isa(Alice => "UNIVERSAL");
138
139test UNIVERSAL::can(Alice => "can") == \&UNIVERSAL::can;
140
84902520 141# now use UNIVERSAL.pm and see what changes
e09f3e01 142eval "use UNIVERSAL";
ff0cee69 143
e09f3e01 144test $a->isa("UNIVERSAL");
44a8e56a 145
46e4b22b 146my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 147# XXX import being here is really a bug
9d116dd7 148if ('a' lt 'A') {
149 test $sub2 eq "can import isa VERSION";
150} else {
151 test $sub2 eq "VERSION can import isa";
152}
44a8e56a 153
e09f3e01 154eval 'sub UNIVERSAL::sleep {}';
155test $a->can("sleep");
44a8e56a 156
e09f3e01 157test ! UNIVERSAL::can($b, "can");
84902520 158
159test ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 160
161test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29 162
163{
164 package Pickup;
165 use UNIVERSAL qw( isa can VERSION );
166
167 main::test isa "Pickup", UNIVERSAL;
168 main::test can( "Pickup", "can" ) == \&UNIVERSAL::can;
169 main::test VERSION "UNIVERSAL" ;
170}