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