xref: /openbsd/gnu/usr.bin/perl/t/uni/universal.t (revision 9f11ffb7)
1898184e3Ssthen#!./perl
2898184e3Ssthen#
3898184e3Ssthen# check UNIVERSAL
4898184e3Ssthen#
5898184e3Ssthen
6898184e3SsthenBEGIN {
7898184e3Ssthen    chdir 't' if -d 't';
8898184e3Ssthen    $| = 1;
9898184e3Ssthen    require "./test.pl";
10*9f11ffb7Safresh1    set_up_inc(qw '../lib ../dist/base/lib');
11898184e3Ssthen}
12898184e3Ssthen
13898184e3Ssthenuse utf8;
14898184e3Ssthenuse open qw( :utf8 :std );
15898184e3Ssthen
16b8851fccSafresh1plan tests => 90;
17898184e3Ssthen
18898184e3Ssthen$a = {};
19898184e3Ssthenbless $a, "Bòb";
20898184e3Ssthenok $a->isa("Bòb");
21898184e3Ssthen
22898184e3Ssthenpackage Hùmàn;
23898184e3Ssthensub èàt {}
24898184e3Ssthen
25898184e3Ssthenpackage Fèmàlè;
26898184e3Ssthen@ISA=qw(Hùmàn);
27898184e3Ssthen
28898184e3Ssthenpackage Àlìcè;
29898184e3Ssthen@ISA=qw(Bòb Fèmàlè);
30898184e3Ssthensub sìng;
31898184e3Ssthensub drìnk { return "drinking " . $_[1]  }
32898184e3Ssthensub nèw { bless {} }
33898184e3Ssthen
34898184e3Ssthenlìcè::VERSION = 2.718;
35898184e3Ssthen
36898184e3Ssthen{
37898184e3Ssthen    package Cèdrìc;
38898184e3Ssthen    our @ISA;
39898184e3Ssthen    use base qw(Hùmàn);
40898184e3Ssthen}
41898184e3Ssthen
42898184e3Ssthen{
43898184e3Ssthen    package Prògràmmèr;
44898184e3Ssthen    our $VERSION = 1.667;
45898184e3Ssthen
46898184e3Ssthen    sub wrìtè_perl { 1 }
47898184e3Ssthen}
48898184e3Ssthen
49898184e3Ssthenpackage main;
50898184e3Ssthen
51898184e3Ssthen$a = nèw Àlìcè;
52898184e3Ssthen
53898184e3Ssthenok $a->isa("Àlìcè");
54898184e3Ssthenok $a->isa("main::Àlìcè");    # check that alternate class names work
55898184e3Ssthenok(("main::Àlìcè"->nèw)->isa("Àlìcè"));
56898184e3Ssthen
57898184e3Ssthenok $a->isa("Bòb");
58898184e3Ssthenok $a->isa("main::Bòb");
59898184e3Ssthen
60898184e3Ssthenok $a->isa("Fèmàlè");
61898184e3Ssthen
62898184e3Ssthenok $a->isa("Hùmàn");
63898184e3Ssthen
64898184e3Ssthenok ! $a->isa("Màlè");
65898184e3Ssthen
66898184e3Ssthenok ! $a->isa('Prògràmmèr');
67898184e3Ssthen
68898184e3Ssthenok $a->isa("HASH");
69898184e3Ssthen
70898184e3Ssthenok $a->can("èàt");
71898184e3Ssthenok ! $a->can("sleep");
72898184e3Ssthenok my $ref = $a->can("drìnk");        # returns a coderef
73898184e3Ssthenis $a->$ref("tèà"), "drinking tèà"; # ... which works
74898184e3Ssthenok $ref = $a->can("sìng");
75898184e3Sstheneval { $a->$ref() };
76898184e3Ssthenok $@;                                # ... but not if no actual subroutine
77898184e3Ssthen
78898184e3Ssthenok $a->can("VERSION");
79898184e3Ssthencmp_ok eval { $a->VERSION }, '==', 2.718;
80898184e3Ssthenok ! (eval { $a->VERSION(2.719) });
81898184e3Ssthenlike $@, qr/^Àlìcè version 2.719 required--this is only version 2.718 at /u;
82898184e3Ssthen
83898184e3Ssthenok (!Cèdrìc->isa('Prògràmmèr'));
84898184e3Ssthen
85898184e3Ssthenok (Cèdrìc->isa('Hùmàn'));
86898184e3Ssthen
87898184e3Ssthenpush(@Cèdrìc::ISA,'Prògràmmèr');
88898184e3Ssthen
89898184e3Ssthenok (Cèdrìc->isa('Prògràmmèr'));
90898184e3Ssthen
91898184e3Ssthen{
92898184e3Ssthen    package Àlìcè;
93898184e3Ssthen    base::->import('Prògràmmèr');
94898184e3Ssthen}
95898184e3Ssthen
96898184e3Ssthenok $a->isa('Prògràmmèr');
97898184e3Ssthenok $a->isa("Fèmàlè");
98898184e3Ssthen
99898184e3Ssthen@Cèdrìc::ISA = qw(Bòb);
100898184e3Ssthen
101898184e3Ssthenok (!Cèdrìc->isa('Prògràmmèr'));
102898184e3Ssthen
103898184e3Ssthenmy $b = 'abc';
104898184e3Ssthenmy @refs = qw(SCALAR SCALAR     LVALUE      GLOB ARRAY HASH CODE);
105898184e3Ssthenmy @vals = (  \$b,   \3.14, \substr($b,1,1), \*b,  [],  {}, sub {} );
106898184e3Ssthenfor ($p=0; $p < @refs; $p++) {
107898184e3Ssthen    for ($q=0; $q < @vals; $q++) {
108898184e3Ssthen        is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
109898184e3Ssthen    };
110898184e3Ssthen};
111898184e3Ssthen
112898184e3Ssthen
113898184e3Ssthenok UNIVERSAL::isalìcè => "UNIVERSAL");
114898184e3Ssthen
115898184e3Ssthencmp_ok UNIVERSAL::canlìcè => "can"), '==', \&UNIVERSAL::can;
116898184e3Ssthen
117898184e3Sstheneval 'sub UNIVERSAL::slèèp {}';
118898184e3Ssthenok $a->can("slèèp");
119898184e3Ssthen
120898184e3Ssthenpackage Fòò;
121898184e3Ssthen
122898184e3Ssthensub DOES { 1 }
123898184e3Ssthen
124898184e3Ssthenpackage Bàr;
125898184e3Ssthen
126898184e3Ssthen@Bàr::ISA = 'Fòò';
127898184e3Ssthen
128898184e3Ssthenpackage Bàz;
129898184e3Ssthen
130898184e3Ssthenpackage main;
131898184e3Ssthenok( Fòò->DOES( 'bàr' ), 'DOES() should call DOES() on class' );
132898184e3Ssthenok( Bàr->DOES( 'Bàr' ), '... and should fall back to isa()' );
133898184e3Ssthenok( Bàr->DOES( 'Fòò' ), '... even when inherited' );
134898184e3Ssthenok( Bàz->DOES( 'Bàz' ), '... even without inheriting any other DOES()' );
135898184e3Ssthenok( ! Bàz->DOES( 'Fòò' ), '... returning true or false appropriately' );
136898184e3Ssthen
137898184e3Ssthenpackage Pìg;
138898184e3Ssthenpackage Bòdìnè;
139898184e3SsthenBòdìnè->isa('Pìg');
140898184e3Ssthen
141898184e3Ssthenpackage main;
142898184e3Sstheneval { UNIVERSAL::DOES([], "fòò") };
143898184e3Ssthenlike( $@, qr/Can't call method "DOES" on unblessed reference/,
144898184e3Ssthen    'DOES call error message says DOES, not isa' );
145898184e3Ssthen
146898184e3Ssthen# Tests for can seem to be split between here and method.t
147898184e3Ssthen# Add the verbatim perl code mentioned in the comments of
148898184e3Ssthen# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
149898184e3Ssthen# but never actually tested.
150898184e3Ssthenis(UNIVERSAL->can("NòSùchPàckàgè::fòò"), undef);
151898184e3Ssthen
152898184e3Ssthen@splàtt::ISA = 'zlòpp';
153898184e3Ssthenok (splàtt->isa('zlòpp'));
154898184e3Ssthenok (!splàtt->isa('plòp'));
155898184e3Ssthen
156898184e3Ssthen# This should reset the ->isa lookup cache
157898184e3Ssthen@splàtt::ISA = 'plòp';
158898184e3Ssthen# And here is the new truth.
159898184e3Ssthenok (!splàtt->isa('zlòpp'));
160898184e3Ssthenok (splàtt->isa('plòp'));
161898184e3Ssthen
162898184e3Ssthen
163