9 eval "use Regexp::Common; use Locale::US;";
10 plan skip_all => "Regexp::Common & Locale::US required for this test" if $@;
15 use Scalar::Util 'isweak';
28 use Regexp::Common 'zip';
30 my $STATES = Locale::US->new;
35 (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
41 /^$RE{zip}{US}{-extended => 'allow'}$/
44 has 'street' => (is => 'rw', isa => 'Str');
45 has 'city' => (is => 'rw', isa => 'Str');
46 has 'state' => (is => 'rw', isa => 'USState');
47 has 'zip_code' => (is => 'rw', isa => 'USZipCode');
54 has 'name' => (is => 'rw', isa => 'Str', required => 1);
55 has 'address' => (is => 'rw', isa => 'Address');
56 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
57 (blessed($_) && $_->isa('Employee') || return) for @$_; 1
61 my ($self, $params) = @_;
62 if ($params->{employees}) {
63 foreach my $employee (@{$params->{employees}}) {
64 $employee->company($self);
69 sub get_employee_count { scalar @{(shift)->employees} }
71 after 'employees' => sub {
72 my ($self, $employees) = @_;
73 # if employees is defined, it
74 # has already been type checked
75 if (defined $employees) {
76 # make sure each gets the
77 # weak ref to the company
78 foreach my $employee (@{$employees}) {
79 $employee->company($self);
89 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
90 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
91 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
92 has 'address' => (is => 'rw', isa => 'Address');
96 return $self->first_name .
97 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
108 has 'title' => (is => 'rw', isa => 'Str', required => 1);
109 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
111 override 'full_name' => sub {
113 super() . ', ' . $self->title
120 name => 'Infinity Interactive',
121 address => Address->new(
122 street => '565 Plandome Rd., Suite 307',
129 first_name => 'Jeremy',
131 title => 'President / Senior Consultant',
132 address => Address->new(city => 'Manhasset', state => 'NY')
135 first_name => 'Tommy',
137 title => 'Vice President / Senior Developer',
138 address => Address->new(city => 'New York', state => 'NY')
141 first_name => 'Stevan',
142 middle_initial => 'C',
143 last_name => 'Little',
144 title => 'Senior Developer',
145 address => Address->new(city => 'Madison', state => 'CT')
149 last_name => 'Kinyon',
150 title => 'Developer',
151 address => Address->new(city => 'Marysville', state => 'OH')
155 } '... created the entire company successfully';
156 isa_ok($ii, 'Company');
158 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
160 isa_ok($ii->address, 'Address');
161 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
162 is($ii->address->city, 'Manhasset', '... got the right city');
163 is($ii->address->state, 'NY', '... got the right state');
164 is($ii->address->zip_code, 11030, '... got the zip code');
166 is($ii->get_employee_count, 4, '... got the right employee count');
170 isa_ok($ii->employees->[0], 'Employee');
171 isa_ok($ii->employees->[0], 'Person');
173 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
174 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
175 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
176 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
177 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
178 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
179 is($ii->employees->[0]->company, $ii, '... got the right company');
180 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
182 isa_ok($ii->employees->[0]->address, 'Address');
183 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
184 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
188 isa_ok($ii->employees->[1], 'Employee');
189 isa_ok($ii->employees->[1], 'Person');
191 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
192 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
193 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
194 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
195 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
196 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
197 is($ii->employees->[1]->company, $ii, '... got the right company');
198 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
200 isa_ok($ii->employees->[1]->address, 'Address');
201 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
202 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
206 isa_ok($ii->employees->[2], 'Employee');
207 isa_ok($ii->employees->[2], 'Person');
209 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
210 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
211 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
212 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
213 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
214 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
215 is($ii->employees->[2]->company, $ii, '... got the right company');
216 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
218 isa_ok($ii->employees->[2]->address, 'Address');
219 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
220 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
224 isa_ok($ii->employees->[3], 'Employee');
225 isa_ok($ii->employees->[3], 'Person');
227 is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
228 is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
229 ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
230 is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
231 is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
232 is($ii->employees->[3]->title, 'Developer', '... got the right title');
233 is($ii->employees->[3]->company, $ii, '... got the right company');
234 ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
236 isa_ok($ii->employees->[3]->address, 'Address');
237 is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
238 is($ii->employees->[3]->address->state, 'OH', '... got the right state');
242 my $new_company = Company->new(name => 'Infinity Interactive International');
243 isa_ok($new_company, 'Company');
245 my $ii_employees = $ii->employees;
246 foreach my $employee (@$ii_employees) {
247 is($employee->company, $ii, '... has the ii company');
250 $new_company->employees($ii_employees);
252 foreach my $employee (@{$new_company->employees}) {
253 is($employee->company, $new_company, '... has the different company now');
256 ## check some error conditions for the subtypes
259 Address->new(street => {}),
260 } '... we die correctly with bad args';
263 Address->new(city => {}),
264 } '... we die correctly with bad args';
267 Address->new(state => 'British Columbia'),
268 } '... we die correctly with bad args';
271 Address->new(state => 'Connecticut'),
272 } '... we live correctly with good args';
275 Address->new(zip_code => 'AF5J6$'),
276 } '... we die correctly with bad args';
279 Address->new(zip_code => '06443'),
280 } '... we live correctly with good args';
284 } '... we die correctly without good args';
287 Company->new(name => 'Foo'),
288 } '... we live correctly without good args';
291 Company->new(name => 'Foo', employees => [ Person->new ]),
292 } '... we die correctly with good args';
295 Company->new(name => 'Foo', employees => []),
296 } '... we live correctly with good args';