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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
package Foo::Role;
use Moose::Role;
use Moose::Util::TypeConstraints;
# if does() exists on its own, then
# we create a type constraint for
# it, just as we do for isa()
has 'bar' => (is => 'rw', does => 'Bar::Role');
has 'baz' => (
is => 'rw',
does => role_type('Bar::Role')
);
package Foo::Class;
use Moose;
with 'Foo::Role';
package Bar::Role;
use Moose::Role;
# if isa and does appear together, then see if Class->does(Role)
# if it does work... then the does() check is actually not needed
# since the isa() check will imply the does() check
has 'foo' => (is => 'rw', isa => 'Foo::Class', does => 'Foo::Role');
package Bar::Class;
use Moose;
with 'Bar::Role';
}
my $foo = Foo::Class->new;
isa_ok($foo, 'Foo::Class');
my $bar = Bar::Class->new;
isa_ok($bar, 'Bar::Class');
is( exception {
$foo->bar($bar);
}, undef, '... bar passed the type constraint okay' );
isnt( exception {
$foo->bar($foo);
}, undef, '... foo did not pass the type constraint okay' );
is( exception {
$foo->baz($bar);
}, undef, '... baz passed the type constraint okay' );
isnt( exception {
$foo->baz($foo);
}, undef, '... foo did not pass the type constraint okay' );
is( exception {
$bar->foo($foo);
}, undef, '... foo passed the type constraint okay' );
# some error conditions
{
package Baz::Class;
use Moose;
# if isa and does appear together, then see if Class->does(Role)
# if it does not,.. we have a conflict... so we die loudly
::isnt( ::exception {
has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class');
}, undef, '... cannot have a does() which is not done by the isa()' );
}
{
package Bling;
use strict;
use warnings;
sub bling { 'Bling::bling' }
package Bling::Bling;
use Moose;
# if isa and does appear together, then see if Class->does(Role)
# if it does not,.. we have a conflict... so we die loudly
::isnt( ::exception {
has 'foo' => (isa => 'Bling', does => 'Bar::Class');
}, undef, '... cannot have a isa() which is cannot does()' );
}
done_testing;
|