Reorder changes to put most interesting ones first
[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
24 package Bar::Role;
02a0fb52 25 use Moose::Role;
26
27 # if isa and does appear together, then see if Class->does(Role)
d03bd989 28 # if it does work... then the does() check is actually not needed
29 # since the isa() check will imply the does() check
30 has 'foo' => (is => 'rw', isa => 'Foo::Class', does => 'Foo::Role');
31
02a0fb52 32 package Foo::Class;
02a0fb52 33 use Moose;
d03bd989 34
02a0fb52 35 with 'Foo::Role';
36
37 package Bar::Class;
02a0fb52 38 use Moose;
39
40 with 'Bar::Role';
41
42}
43
44my $foo = Foo::Class->new;
45isa_ok($foo, 'Foo::Class');
46
47my $bar = Bar::Class->new;
48isa_ok($bar, 'Bar::Class');
49
50lives_ok {
51 $foo->bar($bar);
52} '... bar passed the type constraint okay';
53
54dies_ok {
55 $foo->bar($foo);
56} '... foo did not pass the type constraint okay';
57
58lives_ok {
238b424d 59 $foo->baz($bar);
60} '... baz passed the type constraint okay';
61
62dies_ok {
63 $foo->baz($foo);
64} '... foo did not pass the type constraint okay';
65
66lives_ok {
02a0fb52 67 $bar->foo($foo);
238b424d 68} '... foo passed the type constraint okay';
69
d03bd989 70
02a0fb52 71
72# some error conditions
73
74{
75 package Baz::Class;
02a0fb52 76 use Moose;
77
78 # if isa and does appear together, then see if Class->does(Role)
79 # if it does not,.. we have a conflict... so we die loudly
80 ::dies_ok {
81 has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class');
82 } '... cannot have a does() which is not done by the isa()';
d03bd989 83}
7eaef7ad 84
85{
86 package Bling;
87 use strict;
88 use warnings;
d03bd989 89
7eaef7ad 90 sub bling { 'Bling::bling' }
d03bd989 91
7eaef7ad 92 package Bling::Bling;
7eaef7ad 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
a28e50e4 102done_testing;