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