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