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