fix handling of does and DOES
[p5sagit/Safe-Isa.git] / t / safe_does.t
CommitLineData
de8d80ef 1use strict;
2use warnings;
9d3d6b49 3use Test::More tests => 20;
de8d80ef 4
5{ package Foo; sub new { bless({}, $_[0]) } }
6{ package Bar; our @ISA = qw(Foo); sub bar { 1 } sub does { $_[0]->isa($_[1]) } }
7
8my $foo = Foo->new;
9my $bar = Bar->new;
10my $blam = [ 42 ];
11my $undef;
12
13# basic does, DOES usage -
14# on perls >= 5.10.0, DOES falls back to isa.
15# does must always be manually provided
16
17if (UNIVERSAL->can('DOES')) {
18 ok($foo->DOES('Foo'), 'foo DOES Foo');
19 ok($bar->DOES('Foo'), 'bar DOES Foo');
20}
21else {
22 ok(!eval { $foo->DOES('Foo') }, 'DOES not available in UNIVERSAL');
23 ok(!eval { $bar->DOES('Foo') }, 'DOES not available in UNIVERSAL');
24}
25
26ok(!eval { $foo->does('Foo') }, 'does not implemented on Foo');
27ok($bar->does('Foo'), 'bar does Foo');
28ok(!eval { $blam->DOES('Foo'); 1 }, 'blam goes blam');
29ok(!eval { $undef->DOES('Foo'); 1 }, 'undef goes poof');
30
31
32use Safe::Isa;
33
34ok($foo->$_DOES('Foo'), 'foo $_DOES Foo');
35ok($bar->$_DOES('Foo'), 'bar $_DOES Foo');
36ok(eval { $blam->$_DOES('Foo'); 1 }, 'no boom today');
37ok(eval { $undef->$_DOES('Foo'); 1 }, 'nor tomorrow either');
38
9d3d6b49 39# does should not fall back to isa
40ok(!$foo->$_does('Foo'), 'foo !$_does Foo');
41ok($bar->$_does('Foo'), 'bar $_does Foo');
42ok(eval { $blam->$_does('Foo'); 1 }, 'no boom today');
43ok(eval { $undef->$_does('Foo'); 1 }, 'nor tomorrow either');
de8d80ef 44
45ok($foo->$_call_if_object(DOES => 'Foo'), 'foo $_call_if_object(DOES => Foo)');
46ok($bar->$_call_if_object(DOES => 'Foo'), 'bar $_call_if_object(DOES => Foo)');
47ok(eval { $blam->$_call_if_object(DOES => 'Foo'); 1 }, 'no boom today');
48ok(eval { $undef->$_call_if_object(DOES => 'Foo'); 1 }, 'nor tomorrow either');
49
9d3d6b49 50ok(!eval { $foo->$_call_if_object(does => 'Foo'); 1 }, 'no special DOES handling built into _call_if_object');
51ok(!eval { $foo->$_call_if_object(Does => 'Foo'); 1 }, 'and no handling for wrong case');