Add a new warning, "State variable %s will be reinitialized"
[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;
3e44d7c6 10 require "./test.pl";
e09f3e01 11}
12
cbc021f9 13plan tests => 109;
44a8e56a 14
15$a = {};
16bless $a, "Bob";
3e44d7c6 17ok $a->isa("Bob");
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
3e44d7c6 48
e09f3e01 49
ff0cee69 50$a = new Alice;
44a8e56a 51
3e44d7c6 52ok $a->isa("Alice");
53ok $a->isa("main::Alice"); # check that alternate class names work
44a8e56a 54
3e44d7c6 55ok(("main::Alice"->new)->isa("Alice"));
178d71da 56
3e44d7c6 57ok $a->isa("Bob");
58ok $a->isa("main::Bob");
e09f3e01 59
3e44d7c6 60ok $a->isa("Female");
e09f3e01 61
3e44d7c6 62ok $a->isa("Human");
e09f3e01 63
3e44d7c6 64ok ! $a->isa("Male");
e09f3e01 65
3e44d7c6 66ok ! $a->isa('Programmer');
46e4b22b 67
3e44d7c6 68ok $a->isa("HASH");
986114cf 69
3e44d7c6 70ok $a->can("eat");
71ok ! $a->can("sleep");
72ok my $ref = $a->can("drink"); # returns a coderef
73is $a->$ref("tea"), "drinking tea"; # ... which works
74ok $ref = $a->can("sing");
444e39b5 75eval { $a->$ref() };
3e44d7c6 76ok $@; # ... but not if no actual subroutine
e09f3e01 77
3e44d7c6 78ok (!Cedric->isa('Programmer'));
46e4b22b 79
3e44d7c6 80ok (Cedric->isa('Human'));
46e4b22b 81
82push(@Cedric::ISA,'Programmer');
83
3e44d7c6 84ok (Cedric->isa('Programmer'));
46e4b22b 85
86{
87 package Alice;
88 base::->import('Programmer');
89}
90
3e44d7c6 91ok $a->isa('Programmer');
92ok $a->isa("Female");
46e4b22b 93
94@Cedric::ISA = qw(Bob);
95
3e44d7c6 96ok (!Cedric->isa('Programmer'));
46e4b22b 97
e09f3e01 98my $b = 'abc';
99my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
100my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
101for ($p=0; $p < @refs; $p++) {
102 for ($q=0; $q < @vals; $q++) {
3e44d7c6 103 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
e09f3e01 104 };
105};
106
3e44d7c6 107ok ! UNIVERSAL::can(23, "can");
e09f3e01 108
3e44d7c6 109ok $a->can("VERSION");
e09f3e01 110
3e44d7c6 111ok $a->can("can");
112ok ! $a->can("export_tags"); # a method in Exporter
e09f3e01 113
3e44d7c6 114cmp_ok eval { $a->VERSION }, '==', 2.718;
e09f3e01 115
3e44d7c6 116ok ! (eval { $a->VERSION(2.719) });
117like $@, qr/^Alice version 2.719 \(v2\.719\.0\) required--this is only version 2.718 \(v2\.718\.0\) at /;
44a8e56a 118
3e44d7c6 119ok (eval { $a->VERSION(2.718) });
120is $@, '';
ff0cee69 121
e09f3e01 122my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
ea8fae29 123## The test for import here is *not* because we want to ensure that UNIVERSAL
124## can always import; it is an historical accident that UNIVERSAL can import.
9d116dd7 125if ('a' lt 'A') {
4bf88892 126 is $subs, "can import isa DOES VERSION";
9d116dd7 127} else {
4bf88892 128 is $subs, "DOES VERSION can import isa";
9d116dd7 129}
ff0cee69 130
3e44d7c6 131ok $a->isa("UNIVERSAL");
ff0cee69 132
3e44d7c6 133ok ! UNIVERSAL::isa([], "UNIVERSAL");
b4c2bf25 134
3e44d7c6 135ok ! UNIVERSAL::can({}, "can");
b4c2bf25 136
3e44d7c6 137ok UNIVERSAL::isa(Alice => "UNIVERSAL");
b4c2bf25 138
3e44d7c6 139cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
b4c2bf25 140
84902520 141# now use UNIVERSAL.pm and see what changes
e09f3e01 142eval "use UNIVERSAL";
ff0cee69 143
3e44d7c6 144ok $a->isa("UNIVERSAL");
44a8e56a 145
46e4b22b 146my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 147# XXX import being here is really a bug
9d116dd7 148if ('a' lt 'A') {
4bf88892 149 is $sub2, "can import isa DOES VERSION";
9d116dd7 150} else {
4bf88892 151 is $sub2, "DOES VERSION can import isa";
9d116dd7 152}
44a8e56a 153
e09f3e01 154eval 'sub UNIVERSAL::sleep {}';
3e44d7c6 155ok $a->can("sleep");
44a8e56a 156
3e44d7c6 157ok ! UNIVERSAL::can($b, "can");
84902520 158
3e44d7c6 159ok ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 160
3e44d7c6 161ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29 162
163{
164 package Pickup;
165 use UNIVERSAL qw( isa can VERSION );
166
3e44d7c6 167 ::ok isa "Pickup", UNIVERSAL;
168 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
169 ::ok VERSION "UNIVERSAL" ;
ea8fae29 170}
253ecd6d 171
172{
173 # test isa() and can() on magic variables
174 "Human" =~ /(.*)/;
3e44d7c6 175 ok $1->isa("Human");
176 ok $1->can("eat");
253ecd6d 177 package HumanTie;
178 sub TIESCALAR { bless {} }
179 sub FETCH { "Human" }
180 tie my($x), "HumanTie";
3e44d7c6 181 ::ok $x->isa("Human");
182 ::ok $x->can("eat");
253ecd6d 183}
a1d407e8 184
185# bugid 3284
186# a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
187
188@X::ISA=();
189my $x = {}; bless $x, 'X';
3e44d7c6 190ok $x->isa('UNIVERSAL');
191ok $x->isa('UNIVERSAL');
2bfd5681 192
193
194# Check that the "historical accident" of UNIVERSAL having an import()
195# method doesn't effect anyone else.
196eval { Some::Package->import("bar") };
3e44d7c6 197is $@, '';
198
199
200# This segfaulted in a blead.
201fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
202
cbc021f9 203package Foo;
204
4bf88892 205sub DOES { 1 }
cbc021f9 206
207package Bar;
208
209@Bar::ISA = 'Foo';
210
211package Baz;
212
213package main;
4bf88892 214ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
215ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
216ok( Bar->DOES( 'Foo' ), '... even when inherited' );
217ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
218ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );