File: isa.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (54 lines) | stat: -rw-r--r-- 1,637 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
    require Config;
}

use strict;
use feature 'isa';

plan 14;

package BaseClass {}
package DerivedClass { our @ISA = qw(BaseClass) }
package CustomClass {
   sub isa { length($_[1]) == 9; }
}

my $baseobj = bless {}, "BaseClass";
my $derivedobj = bless {}, "DerivedClass";
my $customobj = bless {}, "CustomClass";

# Bareword package name
ok($baseobj isa BaseClass, '$baseobj isa BaseClass');
ok(not($baseobj isa Another::Class), '$baseobj is not Another::Class');

# String package name
ok($baseobj isa "BaseClass",         '$baseobj isa BaseClass');
ok(not($baseobj isa "DerivedClass"), '$baseobj is not DerivedClass');

ok($derivedobj isa "DerivedClass", '$derivedobj isa DerivedClass');
ok($derivedobj isa "BaseClass",    '$derivedobj isa BaseClass');

# Expression giving a package name
my $classname = "DerivedClass";
ok($derivedobj isa $classname, '$derivedobj isa DerivedClass via SV');

# Invoked on instance which overrides ->isa
ok($customobj isa "Something",          '$customobj isa Something');
ok(not($customobj isa "SomethingElse"), '$customobj isa SomethingElse');

ok(not(undef isa "BaseClass"), 'undef is not BaseClass');
ok(not([] isa "BaseClass"),    'ARRAYref is not BaseClass');

# Test that isa object method still works.

ok($baseobj->isa('BaseClass'), '$baseobj isa BaseClass using object method');
ok(not($baseobj->isa('DerivedClass')), '$baseobj is not BaseClass using object method');
ok($derivedobj->isa('BaseClass'), '$derivedobj isa BaseClass using object method');

# TODO: Consider 
#    LHS = other class