more-tests
[gitmo/Moose.git] / t / 034_attribute_does.t
CommitLineData
02a0fb52 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7eaef7ad 6use Test::More tests => 8;
02a0fb52 7use Test::Exception;
8
9BEGIN {
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
50my $foo = Foo::Class->new;
51isa_ok($foo, 'Foo::Class');
52
53my $bar = Bar::Class->new;
54isa_ok($bar, 'Bar::Class');
55
56lives_ok {
57 $foo->bar($bar);
58} '... bar passed the type constraint okay';
59
60dies_ok {
61 $foo->bar($foo);
62} '... foo did not pass the type constraint okay';
63
64lives_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}
7eaef7ad 82
83{
84 package Bling;
85 use strict;
86 use warnings;
87
88 sub bling { 'Bling::bling' }
89
90 package Bling::Bling;
91 use strict;
92 use warnings;
93 use Moose;
94
95 # if isa and does appear together, then see if Class->does(Role)
96 # if it does not,.. we have a conflict... so we die loudly
97 ::dies_ok {
98 has 'foo' => (isa => 'Bling', does => 'Bar::Class');
99 } '... cannot have a isa() which is cannot does()';
100}
101
102
02a0fb52 103