Commit | Line | Data |
44a8e56a |
1 | #!./perl |
2 | # |
3 | # check UNIVERSAL |
4 | # |
5 | |
e09f3e01 |
6 | BEGIN { |
7 | chdir 't' if -d 't'; |
20822f61 |
8 | @INC = '../lib'; |
46e4b22b |
9 | $| = 1; |
e09f3e01 |
10 | } |
11 | |
a1d407e8 |
12 | print "1..100\n"; |
44a8e56a |
13 | |
14 | $a = {}; |
15 | bless $a, "Bob"; |
ff0cee69 |
16 | print "not " unless $a->isa("Bob"); |
17 | print "ok 1\n"; |
44a8e56a |
18 | |
ff0cee69 |
19 | package Human; |
20 | sub eat {} |
44a8e56a |
21 | |
ff0cee69 |
22 | package Female; |
23 | @ISA=qw(Human); |
44a8e56a |
24 | |
ff0cee69 |
25 | package Alice; |
26 | @ISA=qw(Bob Female); |
39d11b7f |
27 | sub sing; |
28 | sub drink { return "drinking " . $_[1] } |
ff0cee69 |
29 | sub 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 |
46 | package 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 |
59 | test $a->isa("Alice"); |
301daebc |
60 | test $a->isa("main::Alice"); # check that alternate class names work |
44a8e56a |
61 | |
178d71da |
62 | test(("main::Alice"->new)->isa("Alice")); |
63 | |
e09f3e01 |
64 | test $a->isa("Bob"); |
301daebc |
65 | test $a->isa("main::Bob"); |
e09f3e01 |
66 | |
67 | test $a->isa("Female"); |
68 | |
69 | test $a->isa("Human"); |
70 | |
71 | test ! $a->isa("Male"); |
72 | |
46e4b22b |
73 | test ! $a->isa('Programmer'); |
74 | |
986114cf |
75 | test $a->isa("HASH"); |
76 | |
e09f3e01 |
77 | test $a->can("eat"); |
e09f3e01 |
78 | test ! $a->can("sleep"); |
39d11b7f |
79 | test my $ref = $a->can("drink"); # returns a coderef |
80 | test $a->$ref("tea") eq "drinking tea"; # ... which works |
81 | test $ref = $a->can("sing"); |
444e39b5 |
82 | eval { $a->$ref() }; |
39d11b7f |
83 | test $@; # ... but not if no actual subroutine |
e09f3e01 |
84 | |
46e4b22b |
85 | test (!Cedric->isa('Programmer')); |
86 | |
87 | test (Cedric->isa('Human')); |
88 | |
89 | push(@Cedric::ISA,'Programmer'); |
90 | |
91 | test (Cedric->isa('Programmer')); |
92 | |
93 | { |
94 | package Alice; |
95 | base::->import('Programmer'); |
96 | } |
97 | |
98 | test $a->isa('Programmer'); |
99 | test $a->isa("Female"); |
100 | |
101 | @Cedric::ISA = qw(Bob); |
102 | |
103 | test (!Cedric->isa('Programmer')); |
104 | |
e09f3e01 |
105 | my $b = 'abc'; |
106 | my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE); |
107 | my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} ); |
108 | for ($p=0; $p < @refs; $p++) { |
109 | for ($q=0; $q < @vals; $q++) { |
110 | test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1); |
111 | }; |
112 | }; |
113 | |
114 | test ! UNIVERSAL::can(23, "can"); |
115 | |
116 | test $a->can("VERSION"); |
117 | |
118 | test $a->can("can"); |
84902520 |
119 | test ! $a->can("export_tags"); # a method in Exporter |
e09f3e01 |
120 | |
121 | test (eval { $a->VERSION }) == 2.718; |
122 | |
123 | test ! (eval { $a->VERSION(2.719) }) && |
ae165b0c |
124 | $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /; |
44a8e56a |
125 | |
e09f3e01 |
126 | test (eval { $a->VERSION(2.718) }) && ! $@; |
ff0cee69 |
127 | |
e09f3e01 |
128 | my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::; |
ea8fae29 |
129 | ## The test for import here is *not* because we want to ensure that UNIVERSAL |
130 | ## can always import; it is an historical accident that UNIVERSAL can import. |
9d116dd7 |
131 | if ('a' lt 'A') { |
ea8fae29 |
132 | test $subs eq "can import isa VERSION"; |
9d116dd7 |
133 | } else { |
ea8fae29 |
134 | test $subs eq "VERSION can import isa"; |
9d116dd7 |
135 | } |
ff0cee69 |
136 | |
e09f3e01 |
137 | test $a->isa("UNIVERSAL"); |
ff0cee69 |
138 | |
b4c2bf25 |
139 | test ! UNIVERSAL::isa([], "UNIVERSAL"); |
140 | |
141 | test ! UNIVERSAL::can({}, "can"); |
142 | |
143 | test UNIVERSAL::isa(Alice => "UNIVERSAL"); |
144 | |
145 | test UNIVERSAL::can(Alice => "can") == \&UNIVERSAL::can; |
146 | |
84902520 |
147 | # now use UNIVERSAL.pm and see what changes |
e09f3e01 |
148 | eval "use UNIVERSAL"; |
ff0cee69 |
149 | |
e09f3e01 |
150 | test $a->isa("UNIVERSAL"); |
44a8e56a |
151 | |
46e4b22b |
152 | my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::; |
84902520 |
153 | # XXX import being here is really a bug |
9d116dd7 |
154 | if ('a' lt 'A') { |
155 | test $sub2 eq "can import isa VERSION"; |
156 | } else { |
157 | test $sub2 eq "VERSION can import isa"; |
158 | } |
44a8e56a |
159 | |
e09f3e01 |
160 | eval 'sub UNIVERSAL::sleep {}'; |
161 | test $a->can("sleep"); |
44a8e56a |
162 | |
e09f3e01 |
163 | test ! UNIVERSAL::can($b, "can"); |
84902520 |
164 | |
165 | test ! $a->can("export_tags"); # a method in Exporter |
83f7a2bc |
166 | |
167 | test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH'); |
ea8fae29 |
168 | |
169 | { |
170 | package Pickup; |
171 | use UNIVERSAL qw( isa can VERSION ); |
172 | |
173 | main::test isa "Pickup", UNIVERSAL; |
174 | main::test can( "Pickup", "can" ) == \&UNIVERSAL::can; |
175 | main::test VERSION "UNIVERSAL" ; |
176 | } |
253ecd6d |
177 | |
178 | { |
179 | # test isa() and can() on magic variables |
180 | "Human" =~ /(.*)/; |
181 | test $1->isa("Human"); |
182 | test $1->can("eat"); |
183 | package HumanTie; |
184 | sub TIESCALAR { bless {} } |
185 | sub FETCH { "Human" } |
186 | tie my($x), "HumanTie"; |
187 | ::test $x->isa("Human"); |
188 | ::test $x->can("eat"); |
189 | } |
a1d407e8 |
190 | |
191 | # bugid 3284 |
192 | # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching |
193 | |
194 | @X::ISA=(); |
195 | my $x = {}; bless $x, 'X'; |
196 | test $x->isa('UNIVERSAL'); |
197 | test $x->isa('UNIVERSAL'); |