Put in section titles for all parts
[gitmo/moose-presentations.git] / moose-class / exercises / t / lib / MooseClass / Tests.pm
CommitLineData
ddd87d75 1package MooseClass::Tests;
2
3use strict;
4use warnings;
5
5cab7e05 6use Lingua::EN::Inflect qw( A PL_N );
ddd87d75 7use Test::More 'no_plan';
8
9sub 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 47sub 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 64sub 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 108sub 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
ddd87d75 127sub has_meta {
128 my $class = shift;
129
130 ok( $class->can('meta'), "$class has a meta() method" )
131 or BAIL_OUT("Cannot run tests against a class without a meta!");
132}
133
134sub check_isa {
135 my $class = shift;
136 my $parents = shift;
137
138 my @isa = $class->meta->linearized_isa;
139 shift @isa; # returns $class as the first entry
140
141 my $count = scalar @{$parents};
142 my $noun = PL_N( 'parent', $count );
143
144 is( scalar @isa, $count, "$class has $count $noun" );
145
146 for ( my $i = 0; $i < @{$parents}; $i++ ) {
147 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
148 }
149}
150
151sub count_attrs {
152 my $class = shift;
153 my $count = shift;
154
155 my $noun = PL_N( 'attribute', $count );
8d1ce1d7 156 is( scalar $class->meta->get_attribute_list, $count,
157 "$class defines $count $noun" );
ddd87d75 158}
159
160sub has_rw_attr {
161 my $class = shift;
162 my $name = shift;
163
8d1ce1d7 164 my $articled = A($name);
5cab7e05 165 ok( $class->meta->has_attribute($name),
8d1ce1d7 166 "$class has $articled attribute" );
ddd87d75 167
168 my $attr = $class->meta->get_attribute($name);
169
8d1ce1d7 170 is( $attr->get_read_method, $name,
171 "$name attribute has a reader accessor - $name()" );
172 is( $attr->get_write_method, $name,
173 "$name attribute has a writer accessor - $name()" );
ddd87d75 174}
175
176sub has_ro_attr {
177 my $class = shift;
178 my $name = shift;
179
8d1ce1d7 180 my $articled = A($name);
5cab7e05 181 ok( $class->meta->has_attribute($name),
8d1ce1d7 182 "$class has $articled attribute" );
ddd87d75 183
184 my $attr = $class->meta->get_attribute($name);
185
8d1ce1d7 186 is( $attr->get_read_method, $name,
187 "$name attribute has a reader accessor - $name()" );
188 is( $attr->get_write_method, undef,
189 "$name attribute does not have a writer" );
ddd87d75 190}
191
192sub has_method {
193 my $class = shift;
194 my $name = shift;
195
8d1ce1d7 196 my $articled = A($name);
197 ok( $class->meta->has_method($name), "$class has $articled method" );
ddd87d75 198}
199
200sub has_overridden_method {
201 my $class = shift;
202 my $name = shift;
203
8d1ce1d7 204 my $articled = A($name);
205 ok( $class->meta->has_method($name), "$class has $articled method" );
ddd87d75 206
207 my $meth = $class->meta->get_method($name);
208 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
209}
210
211sub no_droppings {
212 my $class = shift;
213
214 ok( !$class->can('has'), "no Moose droppings in $class" );
215}
216
217sub is_immutable {
218 my $class = shift;
219
220 ok( $class->meta->is_immutable, "$class has been made immutable" );
221}
222
5cab7e05 223sub does_role {
224 my $class = shift;
225 my $role = shift;
226
227 ok( $class->meta->does_role($role), "$class does the $role role" );
228}
229
ddd87d75 230sub person01 {
231 my $person = Person->new(
232 first_name => 'Bilbo',
233 last_name => 'Baggins',
234 );
235
8d1ce1d7 236 is( $person->full_name, 'Bilbo Baggins',
237 'full_name() is correctly implemented' );
ddd87d75 238}
239
240sub employee01 {
241 my $employee = Employee->new(
242 first_name => 'Amanda',
243 last_name => 'Palmer',
8d1ce1d7 244 title => 'Singer',
ddd87d75 245 );
246
8d1ce1d7 247 is( $employee->full_name, 'Amanda Palmer (Singer)', 'full_name() is properly overriden in Employee' );
ddd87d75 248}
249
5cab7e05 250sub person02 {
251 my $person = Person->new(
252 first_name => 'Bilbo',
253 last_name => 'Baggins',
254 balance => 0,
255 );
256
8d1ce1d7 257 is( $person->as_string, 'Bilbo Baggins',
258 'as_string() is correctly implemented' );
5cab7e05 259
260 account_tests($person);
261}
262
263sub employee02 {
264 my $employee = Employee->new(
265 first_name => 'Amanda',
266 last_name => 'Palmer',
8d1ce1d7 267 title => 'Singer',
5cab7e05 268 balance => 0,
269 );
270
8d1ce1d7 271 is( $employee->as_string, 'Amanda Palmer (Singer)',
272 'as_string() uses overridden full_name method in Employee' );
5cab7e05 273
274 account_tests($employee);
275}
276
8d1ce1d7 277sub person03 {
278 my $person = Person->new(
279 first_name => 'Bilbo',
280 last_name => 'Baggins',
281 );
282
283 is( $person->full_name, 'Bilbo Baggins',
284 'full_name() is correctly implemented for a Person without a title' );
285 ok( !$person->has_title,
286 'Person has_title predicate is working correctly' );
287
288 $person->title('Ringbearer');
289 ok( $person->has_title, 'Person has_title predicate is working correctly' );
290 is( $person->full_name, 'Bilbo Baggins (Ringbearer)',
291 'full_name() is correctly implemented for a Person with a title' );
292
293 $person->clear_title;
294 ok( !$person->has_title, 'Person clear_title method cleared the title' );
295
296 account_tests( $person, 100 );
297}
298
299sub employee03 {
300 my $employee = Employee->new(
301 first_name => 'Jimmy',
302 last_name => 'Foo',
303 salary_level => 3,
304 salary => 42,
305 );
306
307 is( $employee->salary, 30000,
308 'salary is calculated from salary_level, and salary passed to constructor is ignored' );
309}
310
26164c8d 311
312sub person04 {
313 my $person = Person->new(
314 first_name => 'Bilbo',
315 last_name => 'Baggins',
316 );
317
318 my $xml = <<'EOF';
319<?xml version="1.0" encoding="UTF-8"?>
320<Person>
321<first_name>Bilbo</first_name>
322<last_name>Baggins</last_name>
323<title></title>
324</Person>
325EOF
326
327 is( $person->as_xml, $xml, 'Person outputs expected XML' );
328}
329
330sub employee04 {
331 my $employee = Employee->new(
332 first_name => 'Jimmy',
333 last_name => 'Foo',
334 ssn => '123-99-4567',
335 salary_level => 3,
336 );
337
338 my $xml = <<'EOF';
339<?xml version="1.0" encoding="UTF-8"?>
340<Employee>
341<first_name>Jimmy</first_name>
342<last_name>Foo</last_name>
343<title>Worker</title>
344<salary>30000</salary>
345<salary_level>3</salary_level>
346<ssn>123-99-4567</ssn>
347</Employee>
348EOF
349
350 is( $employee->as_xml, $xml, 'Employee outputs expected XML' );
351}
352
5cab7e05 353sub account_tests {
354 local $Test::Builder::Level = $Test::Builder::Level + 1;
355
356 my $person = shift;
8d1ce1d7 357 my $base_amount = shift || 0;
5cab7e05 358
359 $person->deposit(50);
8d1ce1d7 360 eval { $person->withdraw( 75 + $base_amount ) };
361 like( $@, qr/\QBalance cannot be negative/,
362 'cannot withdraw more than is in our balance' );
5cab7e05 363
8d1ce1d7 364 $person->withdraw( 23 );
5cab7e05 365
8d1ce1d7 366 is( $person->balance, 27 + $base_amount,
367 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23' );
5cab7e05 368}
ddd87d75 369
3701;