Merged topic/metarole-distinguishes-role-meta (which includes topic/roles-have-real...
[gitmo/Moose.git] / t / 020_attributes / 005_attribute_does.t
CommitLineData
02a0fb52 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
02a0fb52 7use Test::Exception;
8
7ff56534 9
02a0fb52 10{
11 package Foo::Role;
02a0fb52 12 use Moose::Role;
d03bd989 13 use Moose::Util::TypeConstraints;
02a0fb52 14
d03bd989 15 # if does() exists on its own, then
16 # we create a type constraint for
02a0fb52 17 # it, just as we do for isa()
d03bd989 18 has 'bar' => (is => 'rw', does => 'Bar::Role');
238b424d 19 has 'baz' => (
d03bd989 20 is => 'rw',
4831e2de 21 does => role_type('Bar::Role')
d03bd989 22 );
02a0fb52 23
f785aad8 24 package Foo::Class;
25 use Moose;
26
27 with 'Foo::Role';
28
02a0fb52 29 package Bar::Role;
02a0fb52 30 use Moose::Role;
31
32 # if isa and does appear together, then see if Class->does(Role)
d03bd989 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
02a0fb52 37 package Bar::Class;
02a0fb52 38 use Moose;
39
40 with 'Bar::Role';
02a0fb52 41}
42
43my $foo = Foo::Class->new;
44isa_ok($foo, 'Foo::Class');
45
46my $bar = Bar::Class->new;
47isa_ok($bar, 'Bar::Class');
48
49lives_ok {
50 $foo->bar($bar);
51} '... bar passed the type constraint okay';
52
53dies_ok {
54 $foo->bar($foo);
55} '... foo did not pass the type constraint okay';
56
57lives_ok {
238b424d 58 $foo->baz($bar);
59} '... baz passed the type constraint okay';
60
61dies_ok {
62 $foo->baz($foo);
63} '... foo did not pass the type constraint okay';
64
65lives_ok {
02a0fb52 66 $bar->foo($foo);
238b424d 67} '... foo passed the type constraint okay';
68
d03bd989 69
02a0fb52 70
71# some error conditions
72
73{
74 package Baz::Class;
02a0fb52 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()';
d03bd989 82}
7eaef7ad 83
84{
85 package Bling;
86 use strict;
87 use warnings;
d03bd989 88
7eaef7ad 89 sub bling { 'Bling::bling' }
d03bd989 90
7eaef7ad 91 package Bling::Bling;
7eaef7ad 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
a28e50e4 101done_testing;