Finished slides & exercises for section 3on basic attributes.
[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
ddd87d75 108sub has_meta {
109 my $class = shift;
110
111 ok( $class->can('meta'), "$class has a meta() method" )
112 or BAIL_OUT("Cannot run tests against a class without a meta!");
113}
114
115sub check_isa {
116 my $class = shift;
117 my $parents = shift;
118
119 my @isa = $class->meta->linearized_isa;
120 shift @isa; # returns $class as the first entry
121
122 my $count = scalar @{$parents};
123 my $noun = PL_N( 'parent', $count );
124
125 is( scalar @isa, $count, "$class has $count $noun" );
126
127 for ( my $i = 0; $i < @{$parents}; $i++ ) {
128 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
129 }
130}
131
132sub count_attrs {
133 my $class = shift;
134 my $count = shift;
135
136 my $noun = PL_N( 'attribute', $count );
8d1ce1d7 137 is( scalar $class->meta->get_attribute_list, $count,
138 "$class defines $count $noun" );
ddd87d75 139}
140
141sub has_rw_attr {
142 my $class = shift;
143 my $name = shift;
144
8d1ce1d7 145 my $articled = A($name);
5cab7e05 146 ok( $class->meta->has_attribute($name),
8d1ce1d7 147 "$class has $articled attribute" );
ddd87d75 148
149 my $attr = $class->meta->get_attribute($name);
150
8d1ce1d7 151 is( $attr->get_read_method, $name,
152 "$name attribute has a reader accessor - $name()" );
153 is( $attr->get_write_method, $name,
154 "$name attribute has a writer accessor - $name()" );
ddd87d75 155}
156
157sub has_ro_attr {
158 my $class = shift;
159 my $name = shift;
160
8d1ce1d7 161 my $articled = A($name);
5cab7e05 162 ok( $class->meta->has_attribute($name),
8d1ce1d7 163 "$class has $articled attribute" );
ddd87d75 164
165 my $attr = $class->meta->get_attribute($name);
166
8d1ce1d7 167 is( $attr->get_read_method, $name,
168 "$name attribute has a reader accessor - $name()" );
169 is( $attr->get_write_method, undef,
170 "$name attribute does not have a writer" );
ddd87d75 171}
172
173sub has_method {
174 my $class = shift;
175 my $name = shift;
176
8d1ce1d7 177 my $articled = A($name);
178 ok( $class->meta->has_method($name), "$class has $articled method" );
ddd87d75 179}
180
181sub has_overridden_method {
182 my $class = shift;
183 my $name = shift;
184
8d1ce1d7 185 my $articled = A($name);
186 ok( $class->meta->has_method($name), "$class has $articled method" );
ddd87d75 187
188 my $meth = $class->meta->get_method($name);
189 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
190}
191
192sub no_droppings {
193 my $class = shift;
194
195 ok( !$class->can('has'), "no Moose droppings in $class" );
196}
197
198sub is_immutable {
199 my $class = shift;
200
201 ok( $class->meta->is_immutable, "$class has been made immutable" );
202}
203
5cab7e05 204sub does_role {
205 my $class = shift;
206 my $role = shift;
207
208 ok( $class->meta->does_role($role), "$class does the $role role" );
209}
210
ddd87d75 211sub person01 {
212 my $person = Person->new(
213 first_name => 'Bilbo',
214 last_name => 'Baggins',
215 );
216
8d1ce1d7 217 is( $person->full_name, 'Bilbo Baggins',
218 'full_name() is correctly implemented' );
ddd87d75 219}
220
221sub employee01 {
222 my $employee = Employee->new(
223 first_name => 'Amanda',
224 last_name => 'Palmer',
8d1ce1d7 225 title => 'Singer',
ddd87d75 226 );
227
8d1ce1d7 228 is( $employee->full_name, 'Amanda Palmer (Singer)', 'full_name() is properly overriden in Employee' );
ddd87d75 229}
230
5cab7e05 231sub person02 {
232 my $person = Person->new(
233 first_name => 'Bilbo',
234 last_name => 'Baggins',
235 balance => 0,
236 );
237
8d1ce1d7 238 is( $person->as_string, 'Bilbo Baggins',
239 'as_string() is correctly implemented' );
5cab7e05 240
241 account_tests($person);
242}
243
244sub employee02 {
245 my $employee = Employee->new(
246 first_name => 'Amanda',
247 last_name => 'Palmer',
8d1ce1d7 248 title => 'Singer',
5cab7e05 249 balance => 0,
250 );
251
8d1ce1d7 252 is( $employee->as_string, 'Amanda Palmer (Singer)',
253 'as_string() uses overridden full_name method in Employee' );
5cab7e05 254
255 account_tests($employee);
256}
257
8d1ce1d7 258sub person03 {
259 my $person = Person->new(
260 first_name => 'Bilbo',
261 last_name => 'Baggins',
262 );
263
264 is( $person->full_name, 'Bilbo Baggins',
265 'full_name() is correctly implemented for a Person without a title' );
266 ok( !$person->has_title,
267 'Person has_title predicate is working correctly' );
268
269 $person->title('Ringbearer');
270 ok( $person->has_title, 'Person has_title predicate is working correctly' );
271 is( $person->full_name, 'Bilbo Baggins (Ringbearer)',
272 'full_name() is correctly implemented for a Person with a title' );
273
274 $person->clear_title;
275 ok( !$person->has_title, 'Person clear_title method cleared the title' );
276
277 account_tests( $person, 100 );
278}
279
280sub employee03 {
281 my $employee = Employee->new(
282 first_name => 'Jimmy',
283 last_name => 'Foo',
284 salary_level => 3,
285 salary => 42,
286 );
287
288 is( $employee->salary, 30000,
289 'salary is calculated from salary_level, and salary passed to constructor is ignored' );
290}
291
5cab7e05 292sub account_tests {
293 local $Test::Builder::Level = $Test::Builder::Level + 1;
294
295 my $person = shift;
8d1ce1d7 296 my $base_amount = shift || 0;
5cab7e05 297
298 $person->deposit(50);
8d1ce1d7 299 eval { $person->withdraw( 75 + $base_amount ) };
300 like( $@, qr/\QBalance cannot be negative/,
301 'cannot withdraw more than is in our balance' );
5cab7e05 302
8d1ce1d7 303 $person->withdraw( 23 );
5cab7e05 304
8d1ce1d7 305 is( $person->balance, 27 + $base_amount,
306 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23' );
5cab7e05 307}
ddd87d75 308
3091;