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