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';
24 use Moose::Util::TypeConstraints;
27 use Regexp::Common 'zip';
29 my $STATES = Locale::US->new;
34 (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
40 /^$RE{zip}{US}{-extended => 'allow'}$/
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');
48 __PACKAGE__->meta->make_immutable(debug => 0);
54 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
55 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
56 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
57 has 'address' => (is => 'rw', isa => 'Address');
61 return $self->first_name .
62 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
66 __PACKAGE__->meta->make_immutable(debug => 0);
74 has 'title' => (is => 'rw', isa => 'Str', required => 1);
75 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
77 override 'full_name' => sub {
79 super() . ', ' . $self->title
82 __PACKAGE__->meta->make_immutable(debug => 0);
88 has 'name' => (is => 'rw', isa => 'Str', required => 1);
89 has 'address' => (is => 'rw', isa => 'Address');
90 has 'employees' => (is => 'rw', isa => 'ArrayRef[Employee]');
93 my ($self, $params) = @_;
94 if ($params->{employees}) {
95 foreach my $employee (@{$params->{employees}}) {
96 $employee->company($self);
101 sub get_employee_count { scalar @{(shift)->employees} }
103 after 'employees' => sub {
104 my ($self, $employees) = @_;
105 # if employees is defined, it
106 # has already been type checked
107 if (defined $employees) {
108 # make sure each gets the
109 # weak ref to the company
110 foreach my $employee (@{$employees}) {
111 $employee->company($self);
116 __PACKAGE__->meta->make_immutable(debug => 0);
122 name => 'Infinity Interactive',
123 address => Address->new(
124 street => '565 Plandome Rd., Suite 307',
131 first_name => 'Jeremy',
133 title => 'President / Senior Consultant',
134 address => Address->new(city => 'Manhasset', state => 'NY')
137 first_name => 'Tommy',
139 title => 'Vice President / Senior Developer',
140 address => Address->new(city => 'New York', state => 'NY')
143 first_name => 'Stevan',
144 middle_initial => 'C',
145 last_name => 'Little',
146 title => 'Senior Developer',
147 address => Address->new(city => 'Madison', state => 'CT')
151 last_name => 'Kinyon',
152 title => 'Developer',
153 address => Address->new(city => 'Marysville', state => 'OH')
157 } '... created the entire company successfully';
158 isa_ok($ii, 'Company');
160 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
162 isa_ok($ii->address, 'Address');
163 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
164 is($ii->address->city, 'Manhasset', '... got the right city');
165 is($ii->address->state, 'NY', '... got the right state');
166 is($ii->address->zip_code, 11030, '... got the zip code');
168 is($ii->get_employee_count, 4, '... got the right employee count');
172 isa_ok($ii->employees->[0], 'Employee');
173 isa_ok($ii->employees->[0], 'Person');
175 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
176 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
177 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
178 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
179 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
180 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
181 is($ii->employees->[0]->company, $ii, '... got the right company');
182 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
184 isa_ok($ii->employees->[0]->address, 'Address');
185 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
186 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
190 isa_ok($ii->employees->[1], 'Employee');
191 isa_ok($ii->employees->[1], 'Person');
193 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
194 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
195 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
196 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
197 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
198 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
199 is($ii->employees->[1]->company, $ii, '... got the right company');
200 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
202 isa_ok($ii->employees->[1]->address, 'Address');
203 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
204 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
208 isa_ok($ii->employees->[2], 'Employee');
209 isa_ok($ii->employees->[2], 'Person');
211 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
212 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
213 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
214 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
215 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
216 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
217 is($ii->employees->[2]->company, $ii, '... got the right company');
218 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
220 isa_ok($ii->employees->[2]->address, 'Address');
221 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
222 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
226 isa_ok($ii->employees->[3], 'Employee');
227 isa_ok($ii->employees->[3], 'Person');
229 is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
230 is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
231 ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
232 is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
233 is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
234 is($ii->employees->[3]->title, 'Developer', '... got the right title');
235 is($ii->employees->[3]->company, $ii, '... got the right company');
236 ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
238 isa_ok($ii->employees->[3]->address, 'Address');
239 is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
240 is($ii->employees->[3]->address->state, 'OH', '... got the right state');
244 my $new_company = Company->new(name => 'Infinity Interactive International');
245 isa_ok($new_company, 'Company');
247 my $ii_employees = $ii->employees;
248 foreach my $employee (@$ii_employees) {
249 is($employee->company, $ii, '... has the ii company');
252 $new_company->employees($ii_employees);
254 foreach my $employee (@{$new_company->employees}) {
255 is($employee->company, $new_company, '... has the different company now');
258 ## check some error conditions for the subtypes
261 Address->new(street => {}),
262 } '... we die correctly with bad args';
265 Address->new(city => {}),
266 } '... we die correctly with bad args';
269 Address->new(state => 'British Columbia'),
270 } '... we die correctly with bad args';
273 Address->new(state => 'Connecticut'),
274 } '... we live correctly with good args';
277 Address->new(zip_code => 'AF5J6$'),
278 } '... we die correctly with bad args';
281 Address->new(zip_code => '06443'),
282 } '... we live correctly with good args';
286 } '... we die correctly without good args';
289 Company->new(name => 'Foo'),
290 } '... we live correctly without good args';
293 Company->new(name => 'Foo', employees => [ Person->new ]),
294 } '... we die correctly with good args';
297 Company->new(name => 'Foo', employees => [ Employee->new, Company->new ]),
298 } '... we die correctly with good args';
301 Company->new(name => 'Foo', employees => []),
302 } '... we live correctly with good args';