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