0_04
[gitmo/Moose.git] / t / 034_does_attribute_option.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 {
14     package Foo::Role;
15     use strict;
16     use warnings;
17     use Moose::Role;
18
19     # if does() exists on its own, then 
20     # we create a type constraint for 
21     # it, just as we do for isa()
22     has 'bar' => (is => 'rw', does => 'Bar::Role'); 
23
24     package Bar::Role;
25     use strict;
26     use warnings;
27     use Moose::Role;
28
29     # if isa and does appear together, then see if Class->does(Role)
30     # if it does work... then the does() check is actually not needed 
31     # since the isa() check will imply the does() check    
32     has 'foo' => (is => 'rw', isa => 'Foo::Class', does => 'Foo::Role');    
33     
34     package Foo::Class;
35     use strict;
36     use warnings;
37     use Moose;
38     
39     with 'Foo::Role';
40
41     package Bar::Class;
42     use strict;
43     use warnings;
44     use Moose;
45
46     with 'Bar::Role';
47
48 }
49
50 my $foo = Foo::Class->new;
51 isa_ok($foo, 'Foo::Class');
52
53 my $bar = Bar::Class->new;
54 isa_ok($bar, 'Bar::Class');
55
56 lives_ok {
57     $foo->bar($bar);
58 } '... bar passed the type constraint okay';
59
60 dies_ok {
61     $foo->bar($foo);
62 } '... foo did not pass the type constraint okay';
63
64 lives_ok {
65     $bar->foo($foo);
66 } '... foo passed the type constraint okay';    
67
68 # some error conditions
69
70 {
71     package Baz::Class;
72     use strict;
73     use warnings;
74     use Moose;
75
76     # if isa and does appear together, then see if Class->does(Role)
77     # if it does not,.. we have a conflict... so we die loudly
78     ::dies_ok {
79         has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class');
80     } '... cannot have a does() which is not done by the isa()';
81 }    
82