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