Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 020_attributes / 005_attribute_does.t
CommitLineData
4060c871 1#!/usr/bin/perl
f1a8bff3 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
4060c871 5
6use strict;
7use warnings;
8
f1a8bff3 9use Test::More;
4060c871 10use Test::Exception;
11
12
4060c871 13{
14 package Foo::Role;
15 use Mouse::Role;
16 use Mouse::Util::TypeConstraints;
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');
22 has 'baz' => (
23 is => 'rw',
f1a8bff3 24 does => role_type('Bar::Role')
4060c871 25 );
26
f1a8bff3 27 package Foo::Class;
28 use Mouse;
29
30 with 'Foo::Role';
31
4060c871 32 package Bar::Role;
33 use Mouse::Role;
34
35 # if isa and does appear together, then see if Class->does(Role)
36 # if it does work... then the does() check is actually not needed
37 # since the isa() check will imply the does() check
f1a8bff3 38 has 'foo' => (is => 'rw', isa => 'Foo::Class', does => 'Foo::Role');
4060c871 39
40 package Bar::Class;
41 use Mouse;
42
43 with 'Bar::Role';
4060c871 44}
45
46my $foo = Foo::Class->new;
47isa_ok($foo, 'Foo::Class');
48
49my $bar = Bar::Class->new;
50isa_ok($bar, 'Bar::Class');
51
52lives_ok {
53 $foo->bar($bar);
54} '... bar passed the type constraint okay';
55
56dies_ok {
57 $foo->bar($foo);
58} '... foo did not pass the type constraint okay';
59
60lives_ok {
61 $foo->baz($bar);
62} '... baz passed the type constraint okay';
63
64dies_ok {
65 $foo->baz($foo);
66} '... foo did not pass the type constraint okay';
67
68lives_ok {
69 $bar->foo($foo);
70} '... foo passed the type constraint okay';
71
72
73
74# some error conditions
75
76{
77 package Baz::Class;
4060c871 78 use Mouse;
79
4060c871 80 # if isa and does appear together, then see if Class->does(Role)
81 # if it does not,.. we have a conflict... so we die loudly
82 ::dies_ok {
f1a8bff3 83 has 'foo' => (isa => 'Foo::Class', does => 'Bar::Class');
4060c871 84 } '... cannot have a does() which is not done by the isa()';
85}
86
87{
88 package Bling;
89 use strict;
90 use warnings;
91
92 sub bling { 'Bling::bling' }
93
94 package Bling::Bling;
4060c871 95 use Mouse;
96
4060c871 97 # if isa and does appear together, then see if Class->does(Role)
98 # if it does not,.. we have a conflict... so we die loudly
99 ::dies_ok {
f1a8bff3 100 has 'foo' => (isa => 'Bling', does => 'Bar::Class');
4060c871 101 } '... cannot have a isa() which is cannot does()';
102}
103
f1a8bff3 104done_testing;