Commit | Line | Data |
ddd87d75 |
1 | package MooseClass::Tests; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
5cab7e05 |
6 | use Lingua::EN::Inflect qw( A PL_N ); |
ddd87d75 |
7 | use Test::More 'no_plan'; |
8 | |
9 | sub tests01 { |
5cab7e05 |
10 | my %p = ( |
11 | person_attr_count => 2, |
12 | employee_attr_count => 3, |
13 | @_, |
14 | ); |
15 | |
ddd87d75 |
16 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
17 | |
18 | has_meta('Person'); |
19 | |
20 | check_isa( 'Person', ['Moose::Object'] ); |
21 | |
5cab7e05 |
22 | count_attrs( 'Person', $p{person_attr_count} ); |
ddd87d75 |
23 | |
24 | has_rw_attr( 'Person', $_ ) for qw( first_name last_name ); |
25 | |
26 | has_method( 'Person', 'full_name' ); |
27 | |
28 | no_droppings('Person'); |
29 | is_immutable('Person'); |
30 | |
31 | person01(); |
32 | |
33 | has_meta('Employee'); |
34 | |
35 | check_isa( 'Employee', [ 'Person', 'Moose::Object' ] ); |
36 | |
5cab7e05 |
37 | count_attrs( 'Employee', $p{employee_attr_count} ); |
ddd87d75 |
38 | |
8d1ce1d7 |
39 | has_rw_attr( 'Employee', $_ ) for qw( title salary ); |
ddd87d75 |
40 | has_ro_attr( 'Employee', 'ssn' ); |
41 | |
42 | has_overridden_method( 'Employee', 'full_name' ); |
43 | |
44 | employee01(); |
45 | } |
46 | |
5cab7e05 |
47 | sub tests02 { |
8d1ce1d7 |
48 | tests01( person_attr_count => 3, @_ ); |
5cab7e05 |
49 | |
50 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
51 | |
52 | no_droppings($_) for qw( Printable HasAccount ); |
53 | |
54 | does_role( 'Person', $_ ) for qw( Printable HasAccount ); |
55 | has_method( 'Person', $_ ) for qw( as_string deposit withdraw ); |
56 | has_rw_attr( 'Person', 'balance' ); |
57 | |
58 | does_role( 'Employee', $_ ) for qw( Printable HasAccount ); |
59 | |
60 | person02(); |
61 | employee02(); |
62 | } |
63 | |
8d1ce1d7 |
64 | sub tests03 { |
65 | { |
66 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
67 | |
68 | has_meta('Person'); |
69 | has_meta('Employee'); |
70 | |
71 | has_rw_attr( 'Person', 'title' ); |
72 | |
73 | has_rw_attr( 'Employee', 'title' ); |
74 | has_rw_attr( 'Employee', 'salary_level' ); |
75 | has_ro_attr( 'Employee', 'salary' ); |
76 | |
77 | has_method( 'Employee', '_build_salary' ); |
78 | } |
79 | |
80 | ok( ! Employee->meta->has_method('full_name'), |
81 | 'Employee no longer implements a full_name method' ); |
82 | |
83 | my $person_title_attr = Person->meta->get_attribute('title'); |
84 | ok( !$person_title_attr->is_required, 'title is not required in Person' ); |
85 | is( $person_title_attr->predicate, 'has_title', |
86 | 'Person title attr has a has_title predicate' ); |
87 | is( $person_title_attr->clearer, 'clear_title', |
88 | 'Person title attr has a clear_title clearer' ); |
89 | |
90 | my $balance_attr = Person->meta->get_attribute('balance'); |
91 | is( $balance_attr->default, 100, 'balance defaults to 100' ); |
92 | |
93 | my $employee_title_attr = Employee->meta->get_attribute('title'); |
94 | is( $employee_title_attr->default, 'Worker', |
95 | 'title defaults to Worker in Employee' ); |
96 | |
97 | my $salary_level_attr = Employee->meta->get_attribute('salary_level'); |
98 | is( $salary_level_attr->default, 1, 'salary_level defaults to 1' ); |
99 | |
100 | my $salary_attr = Employee->meta->get_attribute('salary'); |
101 | ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' ); |
102 | ok( $salary_attr->has_builder, 'salary attr has a builder' ); |
103 | |
104 | person03(); |
105 | employee03(); |
106 | } |
107 | |
26164c8d |
108 | sub tests04 { |
109 | { |
110 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
111 | |
112 | no_droppings('OutputsXML'); |
113 | |
114 | does_role( 'Person', 'OutputsXML' ); |
115 | } |
116 | |
117 | ok( scalar OutputsXML->meta->get_around_method_modifiers('as_xml'), |
118 | 'OutputsXML has an around modifier for as_xml' ); |
119 | |
120 | isa_ok( Employee->meta->get_method('as_xml'), |
121 | 'Moose::Meta::Method::Augmented', 'as_xml is augmented in Employee' ); |
122 | |
123 | person04(); |
124 | employee04(); |
125 | } |
126 | |
66b226e5 |
127 | sub tests06 { |
128 | { |
129 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
130 | |
131 | has_meta('BankAccount'); |
132 | no_droppings('BankAccount'); |
133 | |
134 | has_rw_attr( 'BankAccount', 'balance' ); |
135 | has_rw_attr( 'BankAccount', 'owner' ); |
136 | has_ro_attr( 'BankAccount', 'history' ); |
137 | } |
138 | |
139 | my $person_meta = Person->meta; |
140 | ok( ! $person_meta->has_attribute('balance'), |
141 | 'Person class does not have a balance attribute' ); |
142 | |
143 | my $deposit_meth = $person_meta->get_method('deposit'); |
144 | isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' ); |
145 | |
146 | my $withdraw_meth = $person_meta->get_method('withdraw'); |
147 | isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' ); |
148 | |
149 | my $ba_meta = BankAccount->meta; |
150 | ok( $ba_meta->get_attribute('owner')->is_weak_ref, |
151 | 'owner attribute is a weak ref' ); |
152 | |
153 | person06(); |
154 | } |
155 | |
156 | |
ddd87d75 |
157 | sub has_meta { |
158 | my $class = shift; |
159 | |
160 | ok( $class->can('meta'), "$class has a meta() method" ) |
161 | or BAIL_OUT("Cannot run tests against a class without a meta!"); |
162 | } |
163 | |
164 | sub check_isa { |
165 | my $class = shift; |
166 | my $parents = shift; |
167 | |
168 | my @isa = $class->meta->linearized_isa; |
169 | shift @isa; # returns $class as the first entry |
170 | |
171 | my $count = scalar @{$parents}; |
172 | my $noun = PL_N( 'parent', $count ); |
173 | |
174 | is( scalar @isa, $count, "$class has $count $noun" ); |
175 | |
176 | for ( my $i = 0; $i < @{$parents}; $i++ ) { |
177 | is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" ); |
178 | } |
179 | } |
180 | |
181 | sub count_attrs { |
182 | my $class = shift; |
183 | my $count = shift; |
184 | |
185 | my $noun = PL_N( 'attribute', $count ); |
8d1ce1d7 |
186 | is( scalar $class->meta->get_attribute_list, $count, |
187 | "$class defines $count $noun" ); |
ddd87d75 |
188 | } |
189 | |
190 | sub has_rw_attr { |
191 | my $class = shift; |
192 | my $name = shift; |
193 | |
8d1ce1d7 |
194 | my $articled = A($name); |
5cab7e05 |
195 | ok( $class->meta->has_attribute($name), |
8d1ce1d7 |
196 | "$class has $articled attribute" ); |
ddd87d75 |
197 | |
198 | my $attr = $class->meta->get_attribute($name); |
199 | |
8d1ce1d7 |
200 | is( $attr->get_read_method, $name, |
201 | "$name attribute has a reader accessor - $name()" ); |
202 | is( $attr->get_write_method, $name, |
203 | "$name attribute has a writer accessor - $name()" ); |
ddd87d75 |
204 | } |
205 | |
206 | sub has_ro_attr { |
207 | my $class = shift; |
208 | my $name = shift; |
209 | |
8d1ce1d7 |
210 | my $articled = A($name); |
5cab7e05 |
211 | ok( $class->meta->has_attribute($name), |
8d1ce1d7 |
212 | "$class has $articled attribute" ); |
ddd87d75 |
213 | |
214 | my $attr = $class->meta->get_attribute($name); |
215 | |
8d1ce1d7 |
216 | is( $attr->get_read_method, $name, |
217 | "$name attribute has a reader accessor - $name()" ); |
218 | is( $attr->get_write_method, undef, |
219 | "$name attribute does not have a writer" ); |
ddd87d75 |
220 | } |
221 | |
222 | sub has_method { |
223 | my $class = shift; |
224 | my $name = shift; |
225 | |
8d1ce1d7 |
226 | my $articled = A($name); |
227 | ok( $class->meta->has_method($name), "$class has $articled method" ); |
ddd87d75 |
228 | } |
229 | |
230 | sub has_overridden_method { |
231 | my $class = shift; |
232 | my $name = shift; |
233 | |
8d1ce1d7 |
234 | my $articled = A($name); |
235 | ok( $class->meta->has_method($name), "$class has $articled method" ); |
ddd87d75 |
236 | |
237 | my $meth = $class->meta->get_method($name); |
238 | isa_ok( $meth, 'Moose::Meta::Method::Overridden' ); |
239 | } |
240 | |
241 | sub no_droppings { |
242 | my $class = shift; |
243 | |
244 | ok( !$class->can('has'), "no Moose droppings in $class" ); |
245 | } |
246 | |
247 | sub is_immutable { |
248 | my $class = shift; |
249 | |
250 | ok( $class->meta->is_immutable, "$class has been made immutable" ); |
251 | } |
252 | |
5cab7e05 |
253 | sub does_role { |
254 | my $class = shift; |
255 | my $role = shift; |
256 | |
257 | ok( $class->meta->does_role($role), "$class does the $role role" ); |
258 | } |
259 | |
ddd87d75 |
260 | sub person01 { |
261 | my $person = Person->new( |
262 | first_name => 'Bilbo', |
263 | last_name => 'Baggins', |
264 | ); |
265 | |
8d1ce1d7 |
266 | is( $person->full_name, 'Bilbo Baggins', |
267 | 'full_name() is correctly implemented' ); |
ddd87d75 |
268 | } |
269 | |
270 | sub employee01 { |
271 | my $employee = Employee->new( |
272 | first_name => 'Amanda', |
273 | last_name => 'Palmer', |
8d1ce1d7 |
274 | title => 'Singer', |
ddd87d75 |
275 | ); |
276 | |
8d1ce1d7 |
277 | is( $employee->full_name, 'Amanda Palmer (Singer)', 'full_name() is properly overriden in Employee' ); |
ddd87d75 |
278 | } |
279 | |
5cab7e05 |
280 | sub person02 { |
281 | my $person = Person->new( |
282 | first_name => 'Bilbo', |
283 | last_name => 'Baggins', |
284 | balance => 0, |
285 | ); |
286 | |
8d1ce1d7 |
287 | is( $person->as_string, 'Bilbo Baggins', |
288 | 'as_string() is correctly implemented' ); |
5cab7e05 |
289 | |
290 | account_tests($person); |
291 | } |
292 | |
293 | sub employee02 { |
294 | my $employee = Employee->new( |
295 | first_name => 'Amanda', |
296 | last_name => 'Palmer', |
8d1ce1d7 |
297 | title => 'Singer', |
5cab7e05 |
298 | balance => 0, |
299 | ); |
300 | |
8d1ce1d7 |
301 | is( $employee->as_string, 'Amanda Palmer (Singer)', |
302 | 'as_string() uses overridden full_name method in Employee' ); |
5cab7e05 |
303 | |
304 | account_tests($employee); |
305 | } |
306 | |
8d1ce1d7 |
307 | sub person03 { |
308 | my $person = Person->new( |
309 | first_name => 'Bilbo', |
310 | last_name => 'Baggins', |
311 | ); |
312 | |
313 | is( $person->full_name, 'Bilbo Baggins', |
314 | 'full_name() is correctly implemented for a Person without a title' ); |
315 | ok( !$person->has_title, |
316 | 'Person has_title predicate is working correctly' ); |
317 | |
318 | $person->title('Ringbearer'); |
319 | ok( $person->has_title, 'Person has_title predicate is working correctly' ); |
320 | is( $person->full_name, 'Bilbo Baggins (Ringbearer)', |
321 | 'full_name() is correctly implemented for a Person with a title' ); |
322 | |
323 | $person->clear_title; |
324 | ok( !$person->has_title, 'Person clear_title method cleared the title' ); |
325 | |
326 | account_tests( $person, 100 ); |
327 | } |
328 | |
329 | sub employee03 { |
330 | my $employee = Employee->new( |
331 | first_name => 'Jimmy', |
332 | last_name => 'Foo', |
333 | salary_level => 3, |
334 | salary => 42, |
335 | ); |
336 | |
337 | is( $employee->salary, 30000, |
338 | 'salary is calculated from salary_level, and salary passed to constructor is ignored' ); |
339 | } |
340 | |
26164c8d |
341 | |
342 | sub person04 { |
343 | my $person = Person->new( |
344 | first_name => 'Bilbo', |
345 | last_name => 'Baggins', |
346 | ); |
347 | |
348 | my $xml = <<'EOF'; |
349 | <?xml version="1.0" encoding="UTF-8"?> |
350 | <Person> |
351 | <first_name>Bilbo</first_name> |
352 | <last_name>Baggins</last_name> |
353 | <title></title> |
354 | </Person> |
355 | EOF |
356 | |
357 | is( $person->as_xml, $xml, 'Person outputs expected XML' ); |
358 | } |
359 | |
360 | sub employee04 { |
361 | my $employee = Employee->new( |
362 | first_name => 'Jimmy', |
363 | last_name => 'Foo', |
364 | ssn => '123-99-4567', |
365 | salary_level => 3, |
366 | ); |
367 | |
368 | my $xml = <<'EOF'; |
369 | <?xml version="1.0" encoding="UTF-8"?> |
370 | <Employee> |
371 | <first_name>Jimmy</first_name> |
372 | <last_name>Foo</last_name> |
373 | <title>Worker</title> |
374 | <salary>30000</salary> |
375 | <salary_level>3</salary_level> |
376 | <ssn>123-99-4567</ssn> |
377 | </Employee> |
378 | EOF |
379 | |
380 | is( $employee->as_xml, $xml, 'Employee outputs expected XML' ); |
381 | } |
382 | |
66b226e5 |
383 | sub person06 { |
384 | my $person = Person->new( |
385 | first_name => 'Bilbo', |
386 | last_name => 'Baggins', |
387 | ); |
388 | |
389 | isa_ok( $person->account, 'BankAccount' ); |
390 | is( $person->account->owner, $person, |
391 | 'owner of bank account is person that created account' ); |
392 | |
393 | $person->deposit(10); |
394 | is_deeply( $person->account->history, [ 100, 10 ], |
395 | 'deposit was recorded in account history' ); |
396 | |
397 | $person->withdraw(15); |
398 | is_deeply( $person->account->history, [ 100, 10, -15 ], |
399 | 'withdrawal was recorded in account history' ); |
400 | } |
401 | |
5cab7e05 |
402 | sub account_tests { |
403 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
404 | |
405 | my $person = shift; |
8d1ce1d7 |
406 | my $base_amount = shift || 0; |
5cab7e05 |
407 | |
408 | $person->deposit(50); |
8d1ce1d7 |
409 | eval { $person->withdraw( 75 + $base_amount ) }; |
410 | like( $@, qr/\QBalance cannot be negative/, |
411 | 'cannot withdraw more than is in our balance' ); |
5cab7e05 |
412 | |
8d1ce1d7 |
413 | $person->withdraw( 23 ); |
5cab7e05 |
414 | |
8d1ce1d7 |
415 | is( $person->balance, 27 + $base_amount, |
416 | 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23' ); |
5cab7e05 |
417 | } |
ddd87d75 |
418 | |
419 | 1; |