Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / attributes / attribute_does.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
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 is( exception {
50     $foo->bar($bar);
51 }, undef, '... bar passed the type constraint okay' );
52
53 isnt( exception {
54     $foo->bar($foo);
55 }, undef, '... foo did not pass the type constraint okay' );
56
57 is( exception {
58     $foo->baz($bar);
59 }, undef, '... baz passed the type constraint okay' );
60
61 isnt( exception {
62     $foo->baz($foo);
63 }, undef, '... foo did not pass the type constraint okay' );
64
65 is( exception {
66     $bar->foo($foo);
67 }, undef, '... 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     ::isnt( ::exception {
80         has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class');
81     }, undef, '... 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     ::isnt( ::exception {
97         has 'foo' => (isa => 'Bling', does => 'Bar::Class');
98     }, undef, '... cannot have a isa() which is cannot does()' );
99 }
100
101 done_testing;