test array delegation edge cases
[gitmo/Moose.git] / t / 030_roles / 004_role_composition_errors.t
CommitLineData
e46edf94 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
e46edf94 7use Test::Exception;
8
7ff56534 9
e46edf94 10{
4c035b82 11
e46edf94 12 package Foo::Role;
e46edf94 13 use Moose::Role;
4c035b82 14
e46edf94 15 requires 'foo';
16}
17
fa1be058 18is_deeply(
19 [ sort Foo::Role->meta->get_required_method_list ],
4c035b82 20 ['foo'],
21 '... the Foo::Role has a required method (foo)'
22);
fa1be058 23
e46edf94 24# classes which does not implement required method
25{
4c035b82 26
e46edf94 27 package Foo::Class;
e46edf94 28 use Moose;
4c035b82 29
225f805f 30 ::dies_ok { with('Foo::Role') }
4c035b82 31 '... no foo method implemented by Foo::Class';
e46edf94 32}
33
34# class which does implement required method
35{
4c035b82 36
e46edf94 37 package Bar::Class;
e46edf94 38 use Moose;
4c035b82 39
225f805f 40 ::dies_ok { with('Foo::Class') }
4c035b82 41 '... cannot consume a class, it must be a role';
225f805f 42 ::lives_ok { with('Foo::Role') }
4c035b82 43 '... has a foo method implemented by Bar::Class';
44
45 sub foo {'Bar::Class::foo'}
e46edf94 46}
47
48# role which does implement required method
49{
4c035b82 50
e46edf94 51 package Bar::Role;
e46edf94 52 use Moose::Role;
4c035b82 53
225f805f 54 ::lives_ok { with('Foo::Role') }
4c035b82 55 '... has a foo method implemented by Bar::Role';
56
57 sub foo {'Bar::Role::foo'}
e46edf94 58}
59
fa1be058 60is_deeply(
61 [ sort Bar::Role->meta->get_required_method_list ],
62 [],
4c035b82 63 '... the Bar::Role has not inherited the required method from Foo::Role'
64);
fa1be058 65
e46edf94 66# role which does not implement required method
67{
4c035b82 68
e46edf94 69 package Baz::Role;
fa1be058 70 use Moose::Role;
4c035b82 71
225f805f 72 ::lives_ok { with('Foo::Role') }
4c035b82 73 '... no foo method implemented by Baz::Role';
e46edf94 74}
75
fa1be058 76is_deeply(
77 [ sort Baz::Role->meta->get_required_method_list ],
4c035b82 78 ['foo'],
79 '... the Baz::Role has inherited the required method from Foo::Role'
80);
81
fa1be058 82# classes which does not implement required method
83{
4c035b82 84
fa1be058 85 package Baz::Class;
fa1be058 86 use Moose;
87
225f805f 88 ::dies_ok { with('Baz::Role') }
4c035b82 89 '... no foo method implemented by Baz::Class2';
fa1be058 90}
91
92# class which does implement required method
93{
4c035b82 94
fa1be058 95 package Baz::Class2;
fa1be058 96 use Moose;
97
225f805f 98 ::lives_ok { with('Baz::Role') }
4c035b82 99 '... has a foo method implemented by Baz::Class2';
fa1be058 100
4c035b82 101 sub foo {'Baz::Class2::foo'}
102}
fa1be058 103
d939e016 104
105{
106 package Quux::Role;
107 use Moose::Role;
108
109 requires qw( meth1 meth2 meth3 meth4 );
110}
111
112# RT #41119
113{
114
115 package Quux::Class;
116 use Moose;
117
118 ::throws_ok { with('Quux::Role') }
119 qr/\Q'Quux::Role' requires the methods 'meth1', 'meth2', 'meth3', and 'meth4' to be implemented by 'Quux::Class'/,
120 'exception mentions all the missing required methods at once';
121}
122
123{
124 package Quux::Class2;
125 use Moose;
126
127 sub meth1 { }
128
129 ::throws_ok { with('Quux::Role') }
130 qr/'Quux::Role' requires the methods 'meth2', 'meth3', and 'meth4' to be implemented by 'Quux::Class2'/,
131 'exception mentions all the missing required methods at once, but not the one that exists';
132}
133
134{
135 package Quux::Class3;
136 use Moose;
137
138 has 'meth1' => ( is => 'ro' );
139 has 'meth2' => ( is => 'ro' );
140
141 ::throws_ok { with('Quux::Role') }
f3e76c8f 142 qr/'Quux::Role' requires the methods 'meth3' and 'meth4' to be implemented by 'Quux::Class3'/,
143 'exception mentions all the missing methods at once, but not the accessors';
d939e016 144}
145
146{
147 package Quux::Class4;
148 use Moose;
149
150 sub meth1 { }
151 has 'meth2' => ( is => 'ro' );
152
153 ::throws_ok { with('Quux::Role') }
f3e76c8f 154 qr/'Quux::Role' requires the methods 'meth3' and 'meth4' to be implemented by 'Quux::Class4'/,
d939e016 155 'exception mentions all the require methods that are accessors at once, as well as missing methods, but not the one that exists';
156}
a28e50e4 157
158done_testing;