This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
VMS patches
[perl5.git] / t / op / universal.t
CommitLineData
44a8e56a
PP
1#!./perl
2#
3# check UNIVERSAL
4#
5
e09f3e01
G
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib' if -d '../lib';
9}
10
84902520 11print "1..72\n";
44a8e56a
PP
12
13$a = {};
14bless $a, "Bob";
ff0cee69
PP
15print "not " unless $a->isa("Bob");
16print "ok 1\n";
44a8e56a 17
ff0cee69
PP
18package Human;
19sub eat {}
44a8e56a 20
ff0cee69
PP
21package Female;
22@ISA=qw(Human);
44a8e56a 23
ff0cee69
PP
24package Alice;
25@ISA=qw(Bob Female);
26sub drink {}
27sub new { bless {} }
44a8e56a 28
e09f3e01
G
29$Alice::VERSION = 2.718;
30
44a8e56a 31package main;
e09f3e01
G
32
33my $i = 2;
34sub test { print "not " unless shift; print "ok $i\n"; $i++; }
35
ff0cee69 36$a = new Alice;
44a8e56a 37
e09f3e01 38test $a->isa("Alice");
44a8e56a 39
e09f3e01
G
40test $a->isa("Bob");
41
42test $a->isa("Female");
43
44test $a->isa("Human");
45
46test ! $a->isa("Male");
47
48test $a->can("drink");
49
50test $a->can("eat");
51
52test ! $a->can("sleep");
53
54my $b = 'abc';
55my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
56my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
57for ($p=0; $p < @refs; $p++) {
58 for ($q=0; $q < @vals; $q++) {
59 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
60 };
61};
62
63test ! UNIVERSAL::can(23, "can");
64
65test $a->can("VERSION");
66
67test $a->can("can");
84902520 68test ! $a->can("export_tags"); # a method in Exporter
e09f3e01
G
69
70test (eval { $a->VERSION }) == 2.718;
71
72test ! (eval { $a->VERSION(2.719) }) &&
73 $@ =~ /^Alice version 2.719 required--this is only version 2.718 at /;
44a8e56a 74
e09f3e01 75test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 76
e09f3e01
G
77my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
78test $subs eq "VERSION can isa";
ff0cee69 79
e09f3e01 80test $a->isa("UNIVERSAL");
ff0cee69 81
84902520 82# now use UNIVERSAL.pm and see what changes
e09f3e01 83eval "use UNIVERSAL";
ff0cee69 84
e09f3e01 85test $a->isa("UNIVERSAL");
44a8e56a 86
e09f3e01 87my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 88# XXX import being here is really a bug
e09f3e01 89test $sub2 eq "VERSION can import isa";
44a8e56a 90
e09f3e01
G
91eval 'sub UNIVERSAL::sleep {}';
92test $a->can("sleep");
44a8e56a 93
e09f3e01 94test ! UNIVERSAL::can($b, "can");
84902520
TB
95
96test ! $a->can("export_tags"); # a method in Exporter