This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldoc under OS/2
[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
11print "1..70\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");
68
69test (eval { $a->VERSION }) == 2.718;
70
71test ! (eval { $a->VERSION(2.719) }) &&
72 $@ =~ /^Alice version 2.719 required--this is only version 2.718 at /;
44a8e56a 73
e09f3e01 74test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 75
e09f3e01
G
76my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
77test $subs eq "VERSION can isa";
ff0cee69 78
e09f3e01 79test $a->isa("UNIVERSAL");
ff0cee69 80
e09f3e01 81eval "use UNIVERSAL";
ff0cee69 82
e09f3e01 83test $a->isa("UNIVERSAL");
44a8e56a 84
e09f3e01
G
85my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
86test $sub2 eq "VERSION can import isa";
44a8e56a 87
e09f3e01
G
88eval 'sub UNIVERSAL::sleep {}';
89test $a->can("sleep");
44a8e56a 90
e09f3e01 91test ! UNIVERSAL::can($b, "can");