source: trunk/essentials/dev-lang/perl/t/op/universal.t

Last change on this file was 3181, checked in by bird, 18 years ago

perl 5.8.8

File size: 3.7 KB
Line 
1#!./perl
2#
3# check UNIVERSAL
4#
5
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib';
9 $| = 1;
10 require "./test.pl";
11}
12
13print "1..102\n";
14
15$a = {};
16bless $a, "Bob";
17ok $a->isa("Bob");
18
19package Human;
20sub eat {}
21
22package Female;
23@ISA=qw(Human);
24
25package Alice;
26@ISA=qw(Bob Female);
27sub sing;
28sub drink { return "drinking " . $_[1] }
29sub new { bless {} }
30
31$Alice::VERSION = 2.718;
32
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
46package main;
47
48
49
50$a = new Alice;
51
52ok $a->isa("Alice");
53ok $a->isa("main::Alice"); # check that alternate class names work
54
55ok(("main::Alice"->new)->isa("Alice"));
56
57ok $a->isa("Bob");
58ok $a->isa("main::Bob");
59
60ok $a->isa("Female");
61
62ok $a->isa("Human");
63
64ok ! $a->isa("Male");
65
66ok ! $a->isa('Programmer');
67
68ok $a->isa("HASH");
69
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");
75eval { $a->$ref() };
76ok $@; # ... but not if no actual subroutine
77
78ok (!Cedric->isa('Programmer'));
79
80ok (Cedric->isa('Human'));
81
82push(@Cedric::ISA,'Programmer');
83
84ok (Cedric->isa('Programmer'));
85
86{
87 package Alice;
88 base::->import('Programmer');
89}
90
91ok $a->isa('Programmer');
92ok $a->isa("Female");
93
94@Cedric::ISA = qw(Bob);
95
96ok (!Cedric->isa('Programmer'));
97
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++) {
103 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
104 };
105};
106
107ok ! UNIVERSAL::can(23, "can");
108
109ok $a->can("VERSION");
110
111ok $a->can("can");
112ok ! $a->can("export_tags"); # a method in Exporter
113
114cmp_ok eval { $a->VERSION }, '==', 2.718;
115
116ok ! (eval { $a->VERSION(2.719) });
117like $@, qr/^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
118
119ok (eval { $a->VERSION(2.718) });
120is $@, '';
121
122my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
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.
125if ('a' lt 'A') {
126 is $subs, "can import isa VERSION";
127} else {
128 is $subs, "VERSION can import isa";
129}
130
131ok $a->isa("UNIVERSAL");
132
133ok ! UNIVERSAL::isa([], "UNIVERSAL");
134
135ok ! UNIVERSAL::can({}, "can");
136
137ok UNIVERSAL::isa(Alice => "UNIVERSAL");
138
139cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
140
141# now use UNIVERSAL.pm and see what changes
142eval "use UNIVERSAL";
143
144ok $a->isa("UNIVERSAL");
145
146my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
147# XXX import being here is really a bug
148if ('a' lt 'A') {
149 is $sub2, "can import isa VERSION";
150} else {
151 is $sub2, "VERSION can import isa";
152}
153
154eval 'sub UNIVERSAL::sleep {}';
155ok $a->can("sleep");
156
157ok ! UNIVERSAL::can($b, "can");
158
159ok ! $a->can("export_tags"); # a method in Exporter
160
161ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
162
163{
164 package Pickup;
165 use UNIVERSAL qw( isa can VERSION );
166
167 ::ok isa "Pickup", UNIVERSAL;
168 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
169 ::ok VERSION "UNIVERSAL" ;
170}
171
172{
173 # test isa() and can() on magic variables
174 "Human" =~ /(.*)/;
175 ok $1->isa("Human");
176 ok $1->can("eat");
177 package HumanTie;
178 sub TIESCALAR { bless {} }
179 sub FETCH { "Human" }
180 tie my($x), "HumanTie";
181 ::ok $x->isa("Human");
182 ::ok $x->can("eat");
183}
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';
190ok $x->isa('UNIVERSAL');
191ok $x->isa('UNIVERSAL');
Note: See TracBrowser for help on using the repository browser.