This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
0bd7bb6a8ade7305539da1d30c7db95f3869bc9f
[perl5.git] / t / op / universal.t
1 #!./perl
2 #
3 # check UNIVERSAL
4 #
5
6 BEGIN {
7     chdir 't' if -d 't';
8     require './test.pl';
9     @INC = () unless is_miniperl();
10     unshift @INC, qw '../lib ../dist/base/lib';
11     $| = 1;
12     require "./test.pl";
13 }
14
15 plan tests => 143;
16
17 $a = {};
18 bless $a, "Bob";
19 ok $a->isa("Bob");
20
21 package Human;
22 sub eat {}
23
24 package Female;
25 @ISA=qw(Human);
26
27 package Alice;
28 @ISA=qw(Bob Female);
29 sub sing;
30 sub drink { return "drinking " . $_[1]  }
31 sub new { bless {} }
32
33 $Alice::VERSION = 2.718;
34
35 {
36     package Cedric;
37     our @ISA;
38     use base qw(Human);
39 }
40
41 {
42     package Programmer;
43     our $VERSION = 1.667;
44
45     sub write_perl { 1 }
46 }
47
48 package main;
49
50
51
52 $a = new Alice;
53
54 ok $a->isa("Alice");
55 ok $a->isa("main::Alice");    # check that alternate class names work
56
57 ok(("main::Alice"->new)->isa("Alice"));
58
59 ok $a->isa("Bob");
60 ok $a->isa("main::Bob");
61
62 ok $a->isa("Female");
63
64 ok ! $a->isa("Female\0NOT REALLY!"), "->isa is nul-clean.";
65
66 ok $a->isa("Human");
67
68 ok ! $a->isa("Male");
69
70 ok ! $a->isa('Programmer');
71
72 ok $a->isa("HASH");
73
74 ok $a->can("eat");
75 ok ! $a->can("eat\0Except not!"), "->can is nul-clean.";
76 ok ! $a->can("sleep");
77 ok my $ref = $a->can("drink");        # returns a coderef
78 is $a->$ref("tea"), "drinking tea"; # ... which works
79 ok $ref = $a->can("sing");
80 eval { $a->$ref() };
81 ok $@;                                # ... but not if no actual subroutine
82
83 ok (!Cedric->isa('Programmer'));
84
85 ok (Cedric->isa('Human'));
86
87 push(@Cedric::ISA,'Programmer');
88
89 ok (Cedric->isa('Programmer'));
90
91 {
92     package Alice;
93     base::->import('Programmer');
94 }
95
96 ok $a->isa('Programmer');
97 ok $a->isa("Female");
98
99 @Cedric::ISA = qw(Bob);
100
101 ok (!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         is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
109     };
110 };
111
112 ok UNIVERSAL::can(23, "can");
113 ++${"23::foo"};
114 ok UNIVERSAL::can("23", "can"), '"23" can can when the pack exists';
115 ok UNIVERSAL::can(23, "can"), '23 can can when the pack exists';
116 sub IO::Handle::turn {}
117 ok UNIVERSAL::can(*STDOUT, 'turn'), 'globs with IOs can';
118 ok UNIVERSAL::can(\*STDOUT, 'turn'), 'globrefs with IOs can';
119 ok UNIVERSAL::can("STDOUT", 'turn'), 'IO barewords can';
120
121 ok $a->can("VERSION");
122
123 ok $a->can("can");
124 ok ! $a->can("export_tags");    # a method in Exporter
125
126 cmp_ok eval { $a->VERSION }, '==', 2.718;
127
128 ok ! (eval { $a->VERSION(2.719) });
129 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
130
131 ok (eval { $a->VERSION(2.718) });
132 is $@, '';
133
134 ok ! (eval { $a->VERSION("version") });
135 like $@, qr/^Invalid version format/;
136
137 $aversion::VERSION = "version";
138 ok ! (eval { aversion->VERSION(2.719) });
139 like $@, qr/^Invalid version format/;
140
141 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
142 if ('a' lt 'A') {
143     is $subs, "can isa DOES VERSION";
144 } else {
145     is $subs, "DOES VERSION can isa";
146 }
147
148 ok $a->isa("UNIVERSAL");
149
150 ok ! UNIVERSAL::isa([], "UNIVERSAL");
151
152 ok ! UNIVERSAL::can({}, "can");
153
154 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
155
156 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
157
158 # now use UNIVERSAL.pm and see what changes
159 eval "use UNIVERSAL";
160
161 ok $a->isa("UNIVERSAL");
162
163 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
164 # XXX import being here is really a bug
165 if ('a' lt 'A') {
166     is $sub2, "can import isa DOES VERSION";
167 } else {
168     is $sub2, "DOES VERSION can import isa";
169 }
170
171 eval 'sub UNIVERSAL::sleep {}';
172 ok $a->can("sleep");
173
174 ok UNIVERSAL::can($b, "can");
175
176 ok ! $a->can("export_tags");    # a method in Exporter
177
178 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
179
180 {
181     # test isa() and can() on magic variables
182     "Human" =~ /(.*)/;
183     ok $1->isa("Human");
184     ok $1->can("eat");
185     package HumanTie;
186     sub TIESCALAR { bless {} }
187     sub FETCH { "Human" }
188     tie my($x), "HumanTie";
189     ::ok $x->isa("Human");
190     ::ok $x->can("eat");
191 }
192
193 # bugid 3284
194 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
195
196 @X::ISA=();
197 my $x = {}; bless $x, 'X';
198 ok $x->isa('UNIVERSAL');
199 ok $x->isa('UNIVERSAL');
200
201
202 # Check that the "historical accident" of UNIVERSAL having an import()
203 # method doesn't effect anyone else.
204 eval { Some::Package->import("bar") };
205 is $@, '';
206
207
208 # This segfaulted in a blead.
209 fresh_perl_is('package Foo; Foo->VERSION;  print "ok"', 'ok');
210
211 # So did this.
212 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
213
214 package Foo;
215
216 sub DOES { 1 }
217
218 package Bar;
219
220 @Bar::ISA = 'Foo';
221
222 package Baz;
223
224 package main;
225 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
226 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
227 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
228 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
229 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
230
231 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
232 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
233
234 package Pig;
235 package Bodine;
236 Bodine->isa('Pig');
237 *isa = \&UNIVERSAL::isa;
238 eval { isa({}, 'HASH') };
239 ::is($@, '', "*isa correctly found");
240
241 package main;
242 eval { UNIVERSAL::DOES([], "foo") };
243 like( $@, qr/Can't call method "DOES" on unblessed reference/,
244     'DOES call error message says DOES, not isa' );
245
246 # Tests for can seem to be split between here and method.t
247 # Add the verbatim perl code mentioned in the comments of
248 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
249 # but never actually tested.
250 is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
251
252 @splatt::ISA = 'zlopp';
253 ok (splatt->isa('zlopp'));
254 ok (!splatt->isa('plop'));
255
256 # This should reset the ->isa lookup cache
257 @splatt::ISA = 'plop';
258 # And here is the new truth.
259 ok (!splatt->isa('zlopp'));
260 ok (splatt->isa('plop'));
261
262 use warnings "deprecated";
263 {
264     my $m;
265     local $SIG{__WARN__} = sub { $m = $_[0] };
266     eval "use UNIVERSAL 'can'";
267     like($@, qr/^UNIVERSAL does not export anything\b/,
268         "error for UNIVERSAL->import('can')");
269     is($m, undef,
270         "no deprecation warning for UNIVERSAL->import('can')");
271
272           undef $m;
273     eval "use UNIVERSAL";
274     is($@, "",
275         "no error for UNIVERSAL->import");
276     is($m, undef,
277         "no deprecation warning for UNIVERSAL->import");
278 }
279
280 # Test: [perl #66112]: change @ISA inside  sub isa
281 {
282     package RT66112::A;
283
284     package RT66112::B;
285
286     sub isa {
287         my $self = shift;
288         @ISA = qw/RT66112::A/;
289         return $self->SUPER::isa(@_);
290     }
291
292     package RT66112::C;
293
294     package RT66112::D;
295
296     sub isa {
297         my $self = shift;
298         @RT66112::E::ISA = qw/RT66112::A/;
299         return $self->SUPER::isa(@_);
300     }
301
302     package RT66112::E;
303
304     package main;
305
306     @RT66112::B::ISA = qw//;
307     @RT66112::C::ISA = qw/RT66112::B/;
308     @RT66112::T1::ISA = qw/RT66112::C/;
309     ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
310
311     @RT66112::B::ISA = qw//;
312     @RT66112::C::ISA = qw/RT66112::B/;
313     @RT66112::T2::ISA = qw/RT66112::C/;
314     ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
315
316     @RT66112::B::ISA = qw//;
317     @RT66112::C::ISA = qw/RT66112::B/;
318     @RT66112::T3::ISA = qw/RT66112::C/;
319     ok(RT66112::T3->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T3 isa RT66112::A)") or require mro, diag "@{mro::get_linear_isa('RT66112::T3')}";
320
321     @RT66112::E::ISA = qw/RT66112::D/;
322     @RT66112::T4::ISA = qw/RT66112::E/;
323     ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
324
325     @RT66112::E::ISA = qw/RT66112::D/;
326     @RT66112::T5::ISA = qw/RT66112::E/;
327     ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
328
329     @RT66112::E::ISA = qw/RT66112::D/;
330     @RT66112::T6::ISA = qw/RT66112::E/;
331     ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
332 }
333
334 ok(Undeclared->can("can"));
335 sub Undeclared::foo { }
336 ok(Undeclared->can("foo"));
337 ok(!Undeclared->can("something_else"));
338
339 ok(Undeclared->isa("UNIVERSAL"));
340
341 # keep this at the end to avoid messing up earlier tests, since it modifies
342 # @UNIVERSAL::ISA
343 @UNIVERSAL::ISA = ('UniversalParent');
344 { package UniversalIsaTest1; }
345 ok(UniversalIsaTest1->isa('UniversalParent'));
346 ok(UniversalIsaTest2->isa('UniversalParent'));