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