Commit | Line | Data |
e6995dc6 |
1 | use strict; |
c03ede63 |
2 | use warnings; |
ff5366db |
3 | use Test::More tests => 38; |
e6995dc6 |
4 | |
5 | { package Foo; sub new { bless({}, $_[0]) } } |
3f268440 |
6 | { package Bar; our @ISA = qw(Foo); sub bar { $_[1] } } |
e6995dc6 |
7 | |
8 | my $foo = Foo->new; |
9 | my $bar = Bar->new; |
10 | my $blam = [ 42 ]; |
16ec0304 |
11 | my $undef; |
e6995dc6 |
12 | |
13 | # basic isa usage - |
14 | |
15 | ok($foo->isa('Foo'), 'foo isa Foo'); |
16 | ok($bar->isa('Foo'), 'bar isa Foo'); |
17 | ok(!eval { $blam->isa('Foo'); 1 }, 'blam goes blam'); |
16ec0304 |
18 | ok(!eval { $undef->isa('Foo'); 1 }, 'undef goes poof'); |
19 | |
e6995dc6 |
20 | |
21 | ok(!$foo->can('bar'), 'foo !can bar'); |
22 | ok($bar->can('bar'), 'bar can bar'); |
23 | ok(!eval { $blam->can('bar'); 1 }, 'blam goes blam'); |
16ec0304 |
24 | ok(!eval { $undef->can('bar'); 1 }, 'undef goes poof'); |
e6995dc6 |
25 | |
26 | use Safe::Isa; |
27 | |
28 | ok($foo->$_isa('Foo'), 'foo $_isa Foo'); |
29 | ok($bar->$_isa('Foo'), 'bar $_isa Foo'); |
b59e55e2 |
30 | ok(eval { is($blam->$_isa('Foo'), undef, 'blam isn\'t Foo'); 1 }, 'no boom today'); |
31 | ok(eval { is($undef->$_isa('Foo'), undef, 'undef isn\'t Foo either'); 1 }, 'and no boom tomorrow either'); |
e6995dc6 |
32 | |
33 | ok(!$foo->$_can('bar'), 'foo !$_can bar'); |
34 | ok($bar->$_can('bar'), 'bar $_can bar'); |
b59e55e2 |
35 | ok(eval { is($blam->$_can('bar'), undef, 'blam can\'t bar'); 1 }, 'no boom today'); |
36 | ok(eval { is($undef->$_can('bar'), undef, 'undef can\'t bar either'); 1 }, 'and no boom tomorrow either'); |
e6995dc6 |
37 | |
38 | ok($foo->$_call_if_object(isa => 'Foo'), 'foo $_call_if_object(isa => Foo)'); |
39 | ok($bar->$_call_if_object(isa => 'Foo'), 'bar $_call_if_object(isa => Foo)'); |
b5464a06 |
40 | is($bar->$_call_if_object(bar => ), undef, 'bar $_call_if_object(bar => )'); |
3f268440 |
41 | is($bar->$_call_if_object(bar => 2), 2, 'bar $_call_if_object(bar => 2)'); |
b59e55e2 |
42 | ok(eval { is($blam->$_call_if_object(isa => 'Foo'), undef, 'blam can\'t call anything'); 1 }, 'no boom today'); |
43 | ok(eval { is($undef->$_call_if_object(isa => 'Foo'), undef, 'undef can\'t call anything'); 1 }, 'and no boom tomorrow either'); |
ff5366db |
44 | |
45 | ok($foo->$_call_if_can(isa => 'Foo'), 'foo $_call_if_can(isa => Foo)'); |
46 | ok($bar->$_call_if_can(isa => 'Foo'), 'bar $_call_if_can(isa => Foo)'); |
47 | ok(eval { is($foo->$_call_if_can(bar => ), undef, 'foo can\'t call bar'); 1 }, 'no boom today'); |
b5464a06 |
48 | is($bar->$_call_if_can(bar => ), undef, 'bar $_call_if_can(bar => )'); |
49 | is($bar->$_call_if_can(bar => 2), 2, 'bar $_call_if_can(bar => 2)'); |
ff5366db |
50 | ok(eval { is($blam->$_call_if_can(isa => 'Foo'), undef, 'blam can\'t call anything'); 1 }, 'no boom today'); |
51 | ok(eval { is($undef->$_call_if_can(isa => 'Foo'), undef, 'undef can\'t call anything'); 1 }, 'and no boom tomorrow either'); |