Commit | Line | Data |
02a0fb52 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7ff56534 |
6 | use Test::More tests => 9; |
02a0fb52 |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | |
02a0fb52 |
10 | |
11 | { |
12 | package Foo::Role; |
02a0fb52 |
13 | use Moose::Role; |
d03bd989 |
14 | use Moose::Util::TypeConstraints; |
02a0fb52 |
15 | |
d03bd989 |
16 | # if does() exists on its own, then |
17 | # we create a type constraint for |
02a0fb52 |
18 | # it, just as we do for isa() |
d03bd989 |
19 | has 'bar' => (is => 'rw', does => 'Bar::Role'); |
238b424d |
20 | has 'baz' => ( |
d03bd989 |
21 | is => 'rw', |
4831e2de |
22 | does => role_type('Bar::Role') |
d03bd989 |
23 | ); |
02a0fb52 |
24 | |
25 | package Bar::Role; |
02a0fb52 |
26 | use Moose::Role; |
27 | |
28 | # if isa and does appear together, then see if Class->does(Role) |
d03bd989 |
29 | # if it does work... then the does() check is actually not needed |
30 | # since the isa() check will imply the does() check |
31 | has 'foo' => (is => 'rw', isa => 'Foo::Class', does => 'Foo::Role'); |
32 | |
02a0fb52 |
33 | package Foo::Class; |
02a0fb52 |
34 | use Moose; |
d03bd989 |
35 | |
02a0fb52 |
36 | with 'Foo::Role'; |
37 | |
38 | package Bar::Class; |
02a0fb52 |
39 | use Moose; |
40 | |
41 | with 'Bar::Role'; |
42 | |
43 | } |
44 | |
45 | my $foo = Foo::Class->new; |
46 | isa_ok($foo, 'Foo::Class'); |
47 | |
48 | my $bar = Bar::Class->new; |
49 | isa_ok($bar, 'Bar::Class'); |
50 | |
51 | lives_ok { |
52 | $foo->bar($bar); |
53 | } '... bar passed the type constraint okay'; |
54 | |
55 | dies_ok { |
56 | $foo->bar($foo); |
57 | } '... foo did not pass the type constraint okay'; |
58 | |
59 | lives_ok { |
238b424d |
60 | $foo->baz($bar); |
61 | } '... baz passed the type constraint okay'; |
62 | |
63 | dies_ok { |
64 | $foo->baz($foo); |
65 | } '... foo did not pass the type constraint okay'; |
66 | |
67 | lives_ok { |
02a0fb52 |
68 | $bar->foo($foo); |
238b424d |
69 | } '... foo passed the type constraint okay'; |
70 | |
d03bd989 |
71 | |
02a0fb52 |
72 | |
73 | # some error conditions |
74 | |
75 | { |
76 | package Baz::Class; |
02a0fb52 |
77 | use Moose; |
78 | |
79 | # if isa and does appear together, then see if Class->does(Role) |
80 | # if it does not,.. we have a conflict... so we die loudly |
81 | ::dies_ok { |
82 | has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class'); |
83 | } '... cannot have a does() which is not done by the isa()'; |
d03bd989 |
84 | } |
7eaef7ad |
85 | |
86 | { |
87 | package Bling; |
88 | use strict; |
89 | use warnings; |
d03bd989 |
90 | |
7eaef7ad |
91 | sub bling { 'Bling::bling' } |
d03bd989 |
92 | |
7eaef7ad |
93 | package Bling::Bling; |
7eaef7ad |
94 | use Moose; |
95 | |
96 | # if isa and does appear together, then see if Class->does(Role) |
97 | # if it does not,.. we have a conflict... so we die loudly |
98 | ::dies_ok { |
99 | has 'foo' => (isa => 'Bling', does => 'Bar::Class'); |
100 | } '... cannot have a isa() which is cannot does()'; |
101 | } |
102 | |
103 | |
d03bd989 |
104 | |