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');
55 has 'address' => (is => 'rw', isa => 'Address');
56 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
57 ($_->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} }
76 has 'first_name' => (is => 'rw', isa => 'Str');
77 has 'last_name' => (is => 'rw', isa => 'Str');
78 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
79 has 'address' => (is => 'rw', isa => 'Address');
83 return $self->first_name .
84 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
95 has 'title' => (is => 'rw', isa => 'Str');
96 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
98 override 'full_name' => sub {
100 super() . ', ' . $self->title
107 name => 'Infinity Interactive',
108 address => Address->new(
109 street => '565 Plandome Rd., Suite 307',
116 first_name => 'Jeremy',
118 title => 'President / Senior Consultant',
119 address => Address->new(city => 'Manhasset', state => 'NY')
122 first_name => 'Tommy',
124 title => 'Vice President / Senior Developer',
125 address => Address->new(city => 'New York', state => 'NY')
128 first_name => 'Stevan',
129 middle_initial => 'C',
130 last_name => 'Little',
131 title => 'Senior Developer',
132 address => Address->new(city => 'Madison', state => 'CT')
136 last_name => 'Kinyon',
137 title => 'Developer',
138 address => Address->new(city => 'Marysville', state => 'OH')
142 } '... created the entire company successfully';
143 isa_ok($ii, 'Company');
145 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
147 isa_ok($ii->address, 'Address');
148 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
149 is($ii->address->city, 'Manhasset', '... got the right city');
150 is($ii->address->state, 'NY', '... got the right state');
151 is($ii->address->zip_code, 11030, '... got the zip code');
153 is($ii->get_employee_count, 4, '... got the right employee count');
157 isa_ok($ii->employees->[0], 'Employee');
158 isa_ok($ii->employees->[0], 'Person');
160 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
161 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
162 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
163 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
164 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
165 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
166 is($ii->employees->[0]->company, $ii, '... got the right company');
167 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
169 isa_ok($ii->employees->[0]->address, 'Address');
170 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
171 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
175 isa_ok($ii->employees->[1], 'Employee');
176 isa_ok($ii->employees->[1], 'Person');
178 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
179 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
180 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
181 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
182 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
183 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
184 is($ii->employees->[1]->company, $ii, '... got the right company');
185 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
187 isa_ok($ii->employees->[1]->address, 'Address');
188 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
189 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
193 isa_ok($ii->employees->[2], 'Employee');
194 isa_ok($ii->employees->[2], 'Person');
196 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
197 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
198 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
199 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
200 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
201 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
202 is($ii->employees->[2]->company, $ii, '... got the right company');
203 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
205 isa_ok($ii->employees->[2]->address, 'Address');
206 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
207 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
211 isa_ok($ii->employees->[3], 'Employee');
212 isa_ok($ii->employees->[3], 'Person');
214 is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
215 is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
216 ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
217 is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
218 is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
219 is($ii->employees->[3]->title, 'Developer', '... got the right title');
220 is($ii->employees->[3]->company, $ii, '... got the right company');
221 ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
223 isa_ok($ii->employees->[3]->address, 'Address');
224 is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
225 is($ii->employees->[3]->address->state, 'OH', '... got the right state');
227 ## check some error conditions for the subtypes
230 Address->new(street => {}),
231 } '... we die correctly with bad args';
234 Address->new(city => {}),
235 } '... we die correctly with bad args';
238 Address->new(state => 'British Columbia'),
239 } '... we die correctly with bad args';
242 Address->new(state => 'Connecticut'),
243 } '... we live correctly with good args';
246 Address->new(zip_code => 'AF5J6$'),
247 } '... we die correctly with bad args';
250 Address->new(zip_code => '06443'),
251 } '... we live correctly with good args';
254 Company->new(employees => [ Person->new ]),
255 } '... we die correctly with good args';
258 Company->new(employees => []),
259 } '... we live correctly with good args';