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