9 @INC = () unless is_miniperl();
10 unshift @INC, qw '../lib ../dist/base/lib';
30 sub drink { return "drinking " . $_[1] }
33 $Alice::VERSION = 2.718;
55 ok $a->isa("main::Alice"); # check that alternate class names work
57 ok(("main::Alice"->new)->isa("Alice"));
60 ok $a->isa("main::Bob");
64 ok ! $a->isa("Female\0NOT REALLY!"), "->isa is nul-clean.";
70 ok ! $a->isa('Programmer');
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");
81 ok $@; # ... but not if no actual subroutine
83 ok (!Cedric->isa('Programmer'));
85 ok (Cedric->isa('Human'));
87 push(@Cedric::ISA,'Programmer');
89 ok (Cedric->isa('Programmer'));
93 base::->import('Programmer');
96 ok $a->isa('Programmer');
99 @Cedric::ISA = qw(Bob);
101 ok (!Cedric->isa('Programmer'));
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);
112 ok UNIVERSAL::can(23, "can");
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';
121 ok $a->can("VERSION");
124 ok ! $a->can("export_tags"); # a method in Exporter
126 cmp_ok eval { $a->VERSION }, '==', 2.718;
128 ok ! (eval { $a->VERSION(2.719) });
129 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
131 ok (eval { $a->VERSION(2.718) });
134 ok ! (eval { $a->VERSION("version") });
135 like $@, qr/^Invalid version format/;
137 $aversion::VERSION = "version";
138 ok ! (eval { aversion->VERSION(2.719) });
139 like $@, qr/^Invalid version format/;
141 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
143 is $subs, "can isa DOES VERSION";
145 is $subs, "DOES VERSION can isa";
148 ok $a->isa("UNIVERSAL");
150 ok ! UNIVERSAL::isa([], "UNIVERSAL");
152 ok ! UNIVERSAL::can({}, "can");
154 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
156 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
158 # now use UNIVERSAL.pm and see what changes
159 eval "use UNIVERSAL";
161 ok $a->isa("UNIVERSAL");
163 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
164 # XXX import being here is really a bug
166 is $sub2, "can import isa DOES VERSION";
168 is $sub2, "DOES VERSION can import isa";
171 eval 'sub UNIVERSAL::sleep {}';
174 ok UNIVERSAL::can($b, "can");
176 ok ! $a->can("export_tags"); # a method in Exporter
178 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
181 # test isa() and can() on magic variables
186 sub TIESCALAR { bless {} }
187 sub FETCH { "Human" }
188 tie my($x), "HumanTie";
189 ::ok $x->isa("Human");
194 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
197 my $x = {}; bless $x, 'X';
198 ok $x->isa('UNIVERSAL');
199 ok $x->isa('UNIVERSAL');
202 # Check that the "historical accident" of UNIVERSAL having an import()
203 # method doesn't effect anyone else.
204 eval { Some::Package->import("bar") };
208 # This segfaulted in a blead.
209 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
212 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
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' );
231 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
232 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
237 *isa = \&UNIVERSAL::isa;
238 eval { isa({}, 'HASH') };
239 ::is($@, '', "*isa correctly found");
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' );
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);
252 @splatt::ISA = 'zlopp';
253 ok (splatt->isa('zlopp'));
254 ok (!splatt->isa('plop'));
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'));
262 use warnings "deprecated";
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')");
270 "no deprecation warning for UNIVERSAL->import('can')");
273 eval "use UNIVERSAL";
275 "no error for UNIVERSAL->import");
277 "no deprecation warning for UNIVERSAL->import");
280 # Test: [perl #66112]: change @ISA inside sub isa
288 @ISA = qw/RT66112::A/;
289 return $self->SUPER::isa(@_);
298 @RT66112::E::ISA = qw/RT66112::A/;
299 return $self->SUPER::isa(@_);
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)");
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)");
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')}";
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)");
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)");
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)");
334 ok(Undeclared->can("can"));
335 sub Undeclared::foo { }
336 ok(Undeclared->can("foo"));
337 ok(!Undeclared->can("something_else"));
339 ok(Undeclared->isa("UNIVERSAL"));
341 # keep this at the end to avoid messing up earlier tests, since it modifies
343 @UNIVERSAL::ISA = ('UniversalParent');
344 { package UniversalIsaTest1; }
345 ok(UniversalIsaTest1->isa('UniversalParent'));
346 ok(UniversalIsaTest2->isa('UniversalParent'));