added default {} keyword
[gitmo/Moose.git] / t / 004_recipe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More; 
7
8 BEGIN {
9     eval "use Regexp::Common; use Locale::US;";
10     plan skip_all => "Regexp::Common & Locale::US required for this test" if $@;        
11     plan tests => 81;    
12 }
13
14 use Test::Exception;
15 use Scalar::Util 'isweak';
16
17 BEGIN {
18     use_ok('Moose');           
19 }
20
21 {
22     package Address;
23     use Moose;
24     use Moose::Util::TypeConstraints;
25     
26     use Locale::US;
27     use Regexp::Common 'zip';
28     
29     my $STATES = Locale::US->new;
30     
31     subtype USState 
32         => as Str
33         => where {
34             (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
35         };
36         
37     subtype USZipCode 
38         => as Value
39         => where {
40             /^$RE{zip}{US}{-extended => 'allow'}$/            
41         };
42     
43     has 'street'   => (is => 'rw', isa => 'Str');
44     has 'city'     => (is => 'rw', isa => 'Str');
45     has 'state'    => (is => 'rw', isa => 'USState');
46     has 'zip_code' => (is => 'rw', isa => 'USZipCode');   
47     
48     package Company;
49     use Moose;
50     use Moose::Util::TypeConstraints;    
51     
52     has 'name'      => (is => 'rw', isa => 'Str', required => 1);
53     has 'address'   => (is => 'rw', isa => 'Address'); 
54     has 'employees' => (is => 'rw', isa => subtype ArrayRef => where { 
55         (blessed($_) && $_->isa('Employee') || return) for @$_; 1 
56     });    
57     
58     sub BUILD {
59         my ($self, $params) = @_;
60         if ($params->{employees}) {
61             foreach my $employee (@{$params->{employees}}) {
62                 $employee->company($self);
63             }
64         }
65     }
66     
67     sub get_employee_count { scalar @{(shift)->employees} }
68     
69     after 'employees' => sub {
70         my ($self, $employees) = @_;
71         # if employees is defined, it 
72         # has already been type checked
73         if (defined $employees) {
74             # make sure each gets the 
75             # weak ref to the company
76             foreach my $employee (@{$employees}) {
77                 $employee->company($self);
78             }            
79         }
80     };
81     
82     package Person;
83     use Moose;
84     
85     has 'first_name'     => (is => 'rw', isa => 'Str', required => 1);
86     has 'last_name'      => (is => 'rw', isa => 'Str', required => 1);       
87     has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');  
88     has 'address'        => (is => 'rw', isa => 'Address');
89     
90     sub full_name {
91         my $self = shift;
92         return $self->first_name . 
93               ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
94                $self->last_name;
95     }
96       
97     package Employee;
98     use Moose;  
99     
100     extends 'Person';
101     
102     has 'title'   => (is => 'rw', isa => 'Str', required => 1);
103     has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);  
104     
105     override 'full_name' => sub {
106         my $self = shift;
107         super() . ', ' . $self->title
108     };
109 }
110
111 my $ii;
112 lives_ok {
113     $ii = Company->new({
114         name    => 'Infinity Interactive',
115         address => Address->new(
116             street   => '565 Plandome Rd., Suite 307',
117             city     => 'Manhasset',
118             state    => 'NY',
119             zip_code => '11030'
120         ),
121         employees => [
122             Employee->new(
123                 first_name     => 'Jeremy',
124                 last_name      => 'Shao',
125                 title          => 'President / Senior Consultant',
126                 address        => Address->new(city => 'Manhasset', state => 'NY')
127             ),
128             Employee->new(
129                 first_name     => 'Tommy',
130                 last_name      => 'Lee',
131                 title          => 'Vice President / Senior Developer',
132                 address        => Address->new(city => 'New York', state => 'NY')
133             ),        
134             Employee->new(
135                 first_name     => 'Stevan',
136                 middle_initial => 'C',
137                 last_name      => 'Little',
138                 title          => 'Senior Developer',            
139                 address        => Address->new(city => 'Madison', state => 'CT')
140             ),
141             Employee->new(
142                 first_name     => 'Rob',
143                 last_name      => 'Kinyon',
144                 title          => 'Developer',            
145                 address        => Address->new(city => 'Marysville', state => 'OH')
146             ),        
147         ]
148     });
149 } '... created the entire company successfully';
150 isa_ok($ii, 'Company');
151
152 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
153
154 isa_ok($ii->address, 'Address');
155 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
156 is($ii->address->city, 'Manhasset', '... got the right city');
157 is($ii->address->state, 'NY', '... got the right state');
158 is($ii->address->zip_code, 11030, '... got the zip code');
159
160 is($ii->get_employee_count, 4, '... got the right employee count');
161
162 # employee #1
163
164 isa_ok($ii->employees->[0], 'Employee');
165 isa_ok($ii->employees->[0], 'Person');
166
167 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
168 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
169 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
170 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
171 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
172 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
173 is($ii->employees->[0]->company, $ii, '... got the right company');
174 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
175
176 isa_ok($ii->employees->[0]->address, 'Address');
177 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
178 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
179
180 # employee #2
181
182 isa_ok($ii->employees->[1], 'Employee');
183 isa_ok($ii->employees->[1], 'Person');
184
185 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
186 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
187 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
188 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
189 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
190 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
191 is($ii->employees->[1]->company, $ii, '... got the right company');
192 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
193
194 isa_ok($ii->employees->[1]->address, 'Address');
195 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
196 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
197
198 # employee #3
199
200 isa_ok($ii->employees->[2], 'Employee');
201 isa_ok($ii->employees->[2], 'Person');
202
203 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
204 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
205 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
206 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
207 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
208 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
209 is($ii->employees->[2]->company, $ii, '... got the right company');
210 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
211
212 isa_ok($ii->employees->[2]->address, 'Address');
213 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
214 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
215
216 # employee #4
217
218 isa_ok($ii->employees->[3], 'Employee');
219 isa_ok($ii->employees->[3], 'Person');
220
221 is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
222 is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
223 ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
224 is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
225 is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
226 is($ii->employees->[3]->title, 'Developer', '... got the right title');
227 is($ii->employees->[3]->company, $ii, '... got the right company');
228 ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
229
230 isa_ok($ii->employees->[3]->address, 'Address');
231 is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
232 is($ii->employees->[3]->address->state, 'OH', '... got the right state');
233
234 # create new company
235
236 my $new_company = Company->new(name => 'Infinity Interactive International');
237 isa_ok($new_company, 'Company');
238
239 my $ii_employees = $ii->employees;
240 foreach my $employee (@$ii_employees) {
241     is($employee->company, $ii, '... has the ii company');
242 }
243
244 $new_company->employees($ii_employees);
245
246 foreach my $employee (@{$new_company->employees}) {
247     is($employee->company, $new_company, '... has the different company now');
248 }
249
250 ## check some error conditions for the subtypes
251
252 dies_ok {
253     Address->new(street => {}),    
254 } '... we die correctly with bad args';
255
256 dies_ok {
257     Address->new(city => {}),    
258 } '... we die correctly with bad args';
259
260 dies_ok {
261     Address->new(state => 'British Columbia'),    
262 } '... we die correctly with bad args';
263
264 lives_ok {
265     Address->new(state => 'Connecticut'),    
266 } '... we live correctly with good args';
267
268 dies_ok {
269     Address->new(zip_code => 'AF5J6$'),    
270 } '... we die correctly with bad args';
271
272 lives_ok {
273     Address->new(zip_code => '06443'),    
274 } '... we live correctly with good args';
275
276 dies_ok {
277     Company->new(),    
278 } '... we die correctly without good args';
279
280 lives_ok {
281     Company->new(name => 'Foo'),    
282 } '... we live correctly without good args';
283
284 dies_ok {
285     Company->new(name => 'Foo', employees => [ Person->new ]),    
286 } '... we die correctly with good args';
287
288 lives_ok {
289     Company->new(name => 'Foo', employees => []),    
290 } '... we live correctly with good args';
291