Re: [PATCH] ...while $var = glob(...)
[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;
e09f3e01 10}
11
986114cf 12print "1..91\n";
44a8e56a 13
14$a = {};
15bless $a, "Bob";
ff0cee69 16print "not " unless $a->isa("Bob");
17print "ok 1\n";
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
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 59test $a->isa("Alice");
44a8e56a 60
e09f3e01 61test $a->isa("Bob");
62
63test $a->isa("Female");
64
65test $a->isa("Human");
66
67test ! $a->isa("Male");
68
46e4b22b 69test ! $a->isa('Programmer');
70
986114cf 71test $a->isa("HASH");
72
e09f3e01 73test $a->can("eat");
e09f3e01 74test ! $a->can("sleep");
39d11b7f 75test my $ref = $a->can("drink"); # returns a coderef
76test $a->$ref("tea") eq "drinking tea"; # ... which works
77test $ref = $a->can("sing");
444e39b5 78eval { $a->$ref() };
39d11b7f 79test $@; # ... but not if no actual subroutine
e09f3e01 80
46e4b22b 81test (!Cedric->isa('Programmer'));
82
83test (Cedric->isa('Human'));
84
85push(@Cedric::ISA,'Programmer');
86
87test (Cedric->isa('Programmer'));
88
89{
90 package Alice;
91 base::->import('Programmer');
92}
93
94test $a->isa('Programmer');
95test $a->isa("Female");
96
97@Cedric::ISA = qw(Bob);
98
99test (!Cedric->isa('Programmer'));
100
e09f3e01 101my $b = 'abc';
102my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
103my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
104for ($p=0; $p < @refs; $p++) {
105 for ($q=0; $q < @vals; $q++) {
106 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
107 };
108};
109
110test ! UNIVERSAL::can(23, "can");
111
112test $a->can("VERSION");
113
114test $a->can("can");
84902520 115test ! $a->can("export_tags"); # a method in Exporter
e09f3e01 116
117test (eval { $a->VERSION }) == 2.718;
118
119test ! (eval { $a->VERSION(2.719) }) &&
ae165b0c 120 $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
44a8e56a 121
e09f3e01 122test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 123
e09f3e01 124my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
ea8fae29 125## The test for import here is *not* because we want to ensure that UNIVERSAL
126## can always import; it is an historical accident that UNIVERSAL can import.
9d116dd7 127if ('a' lt 'A') {
ea8fae29 128 test $subs eq "can import isa VERSION";
9d116dd7 129} else {
ea8fae29 130 test $subs eq "VERSION can import isa";
9d116dd7 131}
ff0cee69 132
e09f3e01 133test $a->isa("UNIVERSAL");
ff0cee69 134
b4c2bf25 135test ! UNIVERSAL::isa([], "UNIVERSAL");
136
137test ! UNIVERSAL::can({}, "can");
138
139test UNIVERSAL::isa(Alice => "UNIVERSAL");
140
141test UNIVERSAL::can(Alice => "can") == \&UNIVERSAL::can;
142
84902520 143# now use UNIVERSAL.pm and see what changes
e09f3e01 144eval "use UNIVERSAL";
ff0cee69 145
e09f3e01 146test $a->isa("UNIVERSAL");
44a8e56a 147
46e4b22b 148my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 149# XXX import being here is really a bug
9d116dd7 150if ('a' lt 'A') {
151 test $sub2 eq "can import isa VERSION";
152} else {
153 test $sub2 eq "VERSION can import isa";
154}
44a8e56a 155
e09f3e01 156eval 'sub UNIVERSAL::sleep {}';
157test $a->can("sleep");
44a8e56a 158
e09f3e01 159test ! UNIVERSAL::can($b, "can");
84902520 160
161test ! $a->can("export_tags"); # a method in Exporter
83f7a2bc 162
163test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
ea8fae29 164
165{
166 package Pickup;
167 use UNIVERSAL qw( isa can VERSION );
168
169 main::test isa "Pickup", UNIVERSAL;
170 main::test can( "Pickup", "can" ) == \&UNIVERSAL::can;
171 main::test VERSION "UNIVERSAL" ;
172}