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