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