Commit | Line | Data |
a84066c7 |
1 | use strictures 1; |
2 | use Test::More; |
75967522 |
3 | use Test::Fatal; |
a84066c7 |
4 | |
5 | BEGIN { |
981402b8 |
6 | package Ker; |
7 | |
8 | use Moo::Role; |
502d2880 |
9 | |
10 | sub has_ker {} |
981402b8 |
11 | } |
12 | |
13 | BEGIN { |
a84066c7 |
14 | package Splat; |
15 | |
16 | use Moose::Role; |
17 | |
18 | requires 'monkey'; |
19 | |
20 | sub punch { 1 } |
21 | |
22 | sub jab { 0 } |
23 | |
24 | around monkey => sub { 'OW' }; |
25 | |
26 | has trap => (is => 'ro', default => sub { -1 }); |
502d2880 |
27 | |
28 | sub has_splat {} |
29 | } |
30 | |
31 | BEGIN { |
32 | package KerSplat; |
33 | use Moo::Role; |
34 | |
35 | with qw/ |
36 | Ker |
37 | Splat |
38 | /; |
a84066c7 |
39 | } |
40 | |
41 | BEGIN { |
c100c04c |
42 | package Splat2; |
43 | |
44 | use Mouse::Role; |
45 | |
46 | requires 'monkey'; |
47 | |
48 | sub punch { 1 } |
49 | |
50 | sub jab { 0 } |
51 | |
52 | around monkey => sub { 'OW' }; |
53 | |
54 | has trap => (is => 'ro', default => sub { -1 }); |
502d2880 |
55 | |
56 | sub has_splat {} |
57 | } |
58 | |
59 | BEGIN { |
60 | package KerSplat2; |
61 | use Moo::Role; |
62 | |
63 | with qw/ |
64 | Ker |
65 | Splat2 |
66 | /; |
c100c04c |
67 | } |
68 | |
69 | BEGIN { |
a84066c7 |
70 | package Splattered; |
71 | |
72 | use Moo; |
73 | |
74 | sub monkey { 'WHAT' } |
75 | |
76 | with 'Splat'; |
77 | |
78 | sub jab { 3 } |
79 | } |
80 | |
c100c04c |
81 | BEGIN { |
82 | package Splattered2; |
83 | |
84 | use Moo; |
85 | |
86 | sub monkey { 'WHAT' } |
87 | |
88 | with 'Splat2'; |
89 | |
90 | sub jab { 3 } |
91 | } |
a84066c7 |
92 | |
981402b8 |
93 | BEGIN { |
94 | package Ker::Splattered; |
95 | |
96 | use Moo; |
97 | |
98 | sub monkey { 'WHAT' } |
99 | |
100 | with qw/ Ker Splat /; |
101 | |
102 | sub jab { 3 } |
103 | } |
104 | |
105 | BEGIN { |
106 | package Ker::Splattered2; |
107 | |
108 | use Moo; |
109 | |
110 | sub monkey { 'WHAT' } |
111 | |
112 | with qw/ Ker Splat2 /; |
113 | |
114 | sub jab { 3 } |
115 | } |
116 | |
502d2880 |
117 | BEGIN { |
118 | package KerSplattered; |
119 | |
120 | use Moo; |
121 | |
122 | sub monkey { 'WHAT' } |
123 | |
124 | with qw/ KerSplat /; |
981402b8 |
125 | |
502d2880 |
126 | sub jab { 3 } |
127 | } |
128 | |
129 | BEGIN { |
130 | package KerSplattered2; |
131 | |
132 | use Moo; |
133 | |
134 | sub monkey { 'WHAT' } |
135 | |
136 | with qw/ KerSplat2 /; |
137 | |
138 | sub jab { 3 } |
139 | } |
140 | |
7887ffd0 |
141 | BEGIN { |
142 | package Plunk; |
143 | |
144 | use Moo::Role; |
145 | |
146 | has pp => (is => 'rw', moosify => sub { |
147 | my $spec = shift; |
148 | $spec->{documentation} = 'moosify'; |
149 | }); |
150 | } |
151 | |
152 | BEGIN { |
153 | package Plank; |
154 | |
155 | use Moo; |
156 | use Sub::Quote; |
157 | |
158 | has vv => (is => 'rw', moosify => [quote_sub(q| |
159 | $_[0]->{documentation} = 'moosify'; |
160 | |), sub { $_[0]->{documentation} = $_[0]->{documentation}.' foo'; }]); |
161 | } |
162 | |
163 | BEGIN { |
164 | package Plunker; |
165 | |
166 | use Moose; |
167 | |
168 | with 'Plunk'; |
169 | } |
170 | |
171 | BEGIN { |
172 | package Planker; |
173 | |
174 | use Moose; |
175 | |
176 | extends 'Plank'; |
177 | } |
178 | |
ef4ff8da |
179 | BEGIN { |
180 | package Plonk; |
181 | use Moo; |
182 | has kk => (is => 'rw', moosify => [sub { |
183 | $_[0]->{documentation} = 'parent'; |
184 | }]); |
185 | } |
186 | BEGIN { |
187 | package Plonker; |
188 | use Moo; |
189 | extends 'Plonk'; |
190 | has '+kk' => (moosify => sub { |
191 | my $spec = shift; |
192 | $spec->{documentation} .= 'child'; |
193 | }); |
194 | } |
b6ab6837 |
195 | BEGIN{ |
196 | local $SIG{__WARN__} = sub { fail "warning: $_[0]" }; |
197 | package SplatteredMoose; |
198 | use Moose; |
199 | extends 'Splattered'; |
200 | } |
ef4ff8da |
201 | |
502d2880 |
202 | foreach my $s ( |
203 | Splattered->new, |
204 | Splattered2->new, |
205 | Ker::Splattered->new, |
206 | Ker::Splattered2->new, |
207 | KerSplattered->new, |
208 | KerSplattered2->new, |
b6ab6837 |
209 | SplatteredMoose->new |
502d2880 |
210 | ) { |
b6ab6837 |
211 | can_ok($s, 'punch') |
502d2880 |
212 | and is($s->punch, 1, 'punch'); |
b6ab6837 |
213 | can_ok($s, 'jab') |
502d2880 |
214 | and is($s->jab, 3, 'jab'); |
b6ab6837 |
215 | can_ok($s, 'monkey') |
502d2880 |
216 | and is($s->monkey, 'OW', 'monkey'); |
b6ab6837 |
217 | can_ok($s, 'trap') |
502d2880 |
218 | and is($s->trap, -1, 'trap'); |
219 | } |
220 | |
221 | foreach my $c (qw/ |
222 | Ker::Splattered |
223 | Ker::Splattered2 |
224 | KerSplattered |
225 | KerSplattered2 |
226 | /) { |
b6ab6837 |
227 | can_ok($c, 'has_ker'); |
228 | can_ok($c, 'has_splat'); |
c100c04c |
229 | } |
a84066c7 |
230 | |
ef4ff8da |
231 | is(Plunker->meta->find_attribute_by_name('pp')->documentation, 'moosify', 'moosify modifies attr specs'); |
232 | is(Planker->meta->find_attribute_by_name('vv')->documentation, 'moosify foo', 'moosify modifies attr specs as array'); |
233 | |
234 | is( Plonker->meta->find_attribute_by_name('kk')->documentation, |
235 | 'parentchild', |
236 | 'moosify applies for overridden attributes with roles'); |
502d2880 |
237 | |
b826c41b |
238 | { |
239 | package MooseAttrTrait; |
240 | use Moose::Role; |
241 | |
242 | has 'extra_attr' => (is => 'ro'); |
1ca290ff |
243 | has 'extra_attr_noinit' => (is => 'ro', init_arg => undef); |
b826c41b |
244 | } |
245 | |
246 | { |
1ca290ff |
247 | local $SIG{__WARN__} = sub { fail "warning: $_[0]" }; |
b826c41b |
248 | package UsingMooseTrait; |
249 | use Moo; |
250 | |
1ca290ff |
251 | has one => ( |
252 | is => 'ro', |
253 | traits => ['MooseAttrTrait'], |
254 | extra_attr => 'one', |
255 | extra_attr_noinit => 'two', |
256 | ); |
b826c41b |
257 | } |
258 | |
1ca290ff |
259 | ok( UsingMooseTrait->meta |
260 | ->find_attribute_by_name('one')->can('extra_attr'), |
261 | 'trait was properly applied'); |
262 | is( UsingMooseTrait->meta->find_attribute_by_name('one') |
263 | ->extra_attr, |
264 | 'one', |
265 | 'trait attributes maintain values'); |
b826c41b |
266 | |
75967522 |
267 | { |
268 | package NeedTrap; |
269 | use Moo::Role; |
270 | |
271 | requires 'trap'; |
272 | } |
273 | |
274 | is exception { |
275 | package Splattrap; |
276 | use Moo; |
277 | sub monkey {} |
278 | |
279 | with qw(Splat NeedTrap); |
280 | }, undef, 'requires satisfied by Moose attribute composed at the same time'; |
281 | |
282 | |
7887ffd0 |
283 | done_testing; |