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);
53 use Moose::Util::TypeConstraints;
55 has 'name' => (is => 'rw', isa => 'Str', required => 1);
56 has 'address' => (is => 'rw', isa => 'Address');
57 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
58 (blessed($_) && $_->isa('Employee') || return) for @$_; 1
62 my ($self, $params) = @_;
63 if ($params->{employees}) {
64 foreach my $employee (@{$params->{employees}}) {
65 $employee->company($self);
70 sub get_employee_count { scalar @{(shift)->employees} }
72 after 'employees' => sub {
73 my ($self, $employees) = @_;
74 # if employees is defined, it
75 # has already been type checked
76 if (defined $employees) {
77 # make sure each gets the
78 # weak ref to the company
79 foreach my $employee (@{$employees}) {
80 $employee->company($self);
85 __PACKAGE__->meta->make_immutable(debug => 0);
91 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
92 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
93 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
94 has 'address' => (is => 'rw', isa => 'Address');
98 return $self->first_name .
99 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
103 __PACKAGE__->meta->make_immutable(debug => 0);
111 has 'title' => (is => 'rw', isa => 'Str', required => 1);
112 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
114 override 'full_name' => sub {
116 super() . ', ' . $self->title
119 __PACKAGE__->meta->make_immutable(debug => 0);
125 name => 'Infinity Interactive',
126 address => Address->new(
127 street => '565 Plandome Rd., Suite 307',
134 first_name => 'Jeremy',
136 title => 'President / Senior Consultant',
137 address => Address->new(city => 'Manhasset', state => 'NY')
140 first_name => 'Tommy',
142 title => 'Vice President / Senior Developer',
143 address => Address->new(city => 'New York', state => 'NY')
146 first_name => 'Stevan',
147 middle_initial => 'C',
148 last_name => 'Little',
149 title => 'Senior Developer',
150 address => Address->new(city => 'Madison', state => 'CT')
154 last_name => 'Kinyon',
155 title => 'Developer',
156 address => Address->new(city => 'Marysville', state => 'OH')
160 } '... created the entire company successfully';
161 isa_ok($ii, 'Company');
163 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
165 isa_ok($ii->address, 'Address');
166 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
167 is($ii->address->city, 'Manhasset', '... got the right city');
168 is($ii->address->state, 'NY', '... got the right state');
169 is($ii->address->zip_code, 11030, '... got the zip code');
171 is($ii->get_employee_count, 4, '... got the right employee count');
175 isa_ok($ii->employees->[0], 'Employee');
176 isa_ok($ii->employees->[0], 'Person');
178 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
179 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
180 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
181 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
182 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
183 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
184 is($ii->employees->[0]->company, $ii, '... got the right company');
185 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
187 isa_ok($ii->employees->[0]->address, 'Address');
188 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
189 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
193 isa_ok($ii->employees->[1], 'Employee');
194 isa_ok($ii->employees->[1], 'Person');
196 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
197 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
198 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
199 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
200 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
201 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
202 is($ii->employees->[1]->company, $ii, '... got the right company');
203 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
205 isa_ok($ii->employees->[1]->address, 'Address');
206 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
207 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
211 isa_ok($ii->employees->[2], 'Employee');
212 isa_ok($ii->employees->[2], 'Person');
214 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
215 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
216 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
217 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
218 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
219 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
220 is($ii->employees->[2]->company, $ii, '... got the right company');
221 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
223 isa_ok($ii->employees->[2]->address, 'Address');
224 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
225 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
229 isa_ok($ii->employees->[3], 'Employee');
230 isa_ok($ii->employees->[3], 'Person');
232 is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
233 is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
234 ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
235 is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
236 is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
237 is($ii->employees->[3]->title, 'Developer', '... got the right title');
238 is($ii->employees->[3]->company, $ii, '... got the right company');
239 ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
241 isa_ok($ii->employees->[3]->address, 'Address');
242 is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
243 is($ii->employees->[3]->address->state, 'OH', '... got the right state');
247 my $new_company = Company->new(name => 'Infinity Interactive International');
248 isa_ok($new_company, 'Company');
250 my $ii_employees = $ii->employees;
251 foreach my $employee (@$ii_employees) {
252 is($employee->company, $ii, '... has the ii company');
255 $new_company->employees($ii_employees);
257 foreach my $employee (@{$new_company->employees}) {
258 is($employee->company, $new_company, '... has the different company now');
261 ## check some error conditions for the subtypes
264 Address->new(street => {}),
265 } '... we die correctly with bad args';
268 Address->new(city => {}),
269 } '... we die correctly with bad args';
272 Address->new(state => 'British Columbia'),
273 } '... we die correctly with bad args';
276 Address->new(state => 'Connecticut'),
277 } '... we live correctly with good args';
280 Address->new(zip_code => 'AF5J6$'),
281 } '... we die correctly with bad args';
284 Address->new(zip_code => '06443'),
285 } '... we live correctly with good args';
289 } '... we die correctly without good args';
292 Company->new(name => 'Foo'),
293 } '... we live correctly without good args';
296 Company->new(name => 'Foo', employees => [ Person->new ]),
297 } '... we die correctly with good args';
300 Company->new(name => 'Foo', employees => []),
301 } '... we live correctly with good args';