This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Replace the use Test::More in t/{op,io,run} with t/test.pl.
[perl5.git] / t / op / universal.t
... / ...
CommitLineData
1#!./perl
2#
3# check UNIVERSAL
4#
5
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib';
9 $| = 1;
10}
11
12print "1..87\n";
13
14$a = {};
15bless $a, "Bob";
16print "not " unless $a->isa("Bob");
17print "ok 1\n";
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{ my $i = 2;
49 sub test { print "not " unless shift; print "ok $i\n"; $i++; }
50}
51
52$a = new Alice;
53
54test $a->isa("Alice");
55
56test $a->isa("Bob");
57
58test $a->isa("Female");
59
60test $a->isa("Human");
61
62test ! $a->isa("Male");
63
64test ! $a->isa('Programmer');
65
66test $a->can("eat");
67test ! $a->can("sleep");
68test my $ref = $a->can("drink"); # returns a coderef
69test $a->$ref("tea") eq "drinking tea"; # ... which works
70test $ref = $a->can("sing");
71eval { $a->$ref() };
72test $@; # ... but not if no actual subroutine
73
74test (!Cedric->isa('Programmer'));
75
76test (Cedric->isa('Human'));
77
78push(@Cedric::ISA,'Programmer');
79
80test (Cedric->isa('Programmer'));
81
82{
83 package Alice;
84 base::->import('Programmer');
85}
86
87test $a->isa('Programmer');
88test $a->isa("Female");
89
90@Cedric::ISA = qw(Bob);
91
92test (!Cedric->isa('Programmer'));
93
94my $b = 'abc';
95my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
96my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
97for ($p=0; $p < @refs; $p++) {
98 for ($q=0; $q < @vals; $q++) {
99 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
100 };
101};
102
103test ! UNIVERSAL::can(23, "can");
104
105test $a->can("VERSION");
106
107test $a->can("can");
108test ! $a->can("export_tags"); # a method in Exporter
109
110test (eval { $a->VERSION }) == 2.718;
111
112test ! (eval { $a->VERSION(2.719) }) &&
113 $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
114
115test (eval { $a->VERSION(2.718) }) && ! $@;
116
117my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
118if ('a' lt 'A') {
119 test $subs eq "can isa VERSION";
120} else {
121 test $subs eq "VERSION can isa";
122}
123
124test $a->isa("UNIVERSAL");
125
126test ! UNIVERSAL::isa([], "UNIVERSAL");
127
128test ! UNIVERSAL::can({}, "can");
129
130test UNIVERSAL::isa(Alice => "UNIVERSAL");
131
132test UNIVERSAL::can(Alice => "can") == \&UNIVERSAL::can;
133
134# now use UNIVERSAL.pm and see what changes
135eval "use UNIVERSAL";
136
137test $a->isa("UNIVERSAL");
138
139my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
140# XXX import being here is really a bug
141if ('a' lt 'A') {
142 test $sub2 eq "can import isa VERSION";
143} else {
144 test $sub2 eq "VERSION can import isa";
145}
146
147eval 'sub UNIVERSAL::sleep {}';
148test $a->can("sleep");
149
150test ! UNIVERSAL::can($b, "can");
151
152test ! $a->can("export_tags"); # a method in Exporter
153
154test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');