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');
50 use Moose::Util::TypeConstraints;
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
59 my ($self, $params) = @_;
60 if ($params->{employees}) {
61 foreach my $employee (@{$params->{employees}}) {
62 $employee->company($self);
67 sub get_employee_count { scalar @{(shift)->employees} }
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);
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');
92 return $self->first_name .
93 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
102 has 'title' => (is => 'rw', isa => 'Str', required => 1);
103 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
105 override 'full_name' => sub {
107 super() . ', ' . $self->title
114 name => 'Infinity Interactive',
115 address => Address->new(
116 street => '565 Plandome Rd., Suite 307',
123 first_name => 'Jeremy',
125 title => 'President / Senior Consultant',
126 address => Address->new(city => 'Manhasset', state => 'NY')
129 first_name => 'Tommy',
131 title => 'Vice President / Senior Developer',
132 address => Address->new(city => 'New York', state => 'NY')
135 first_name => 'Stevan',
136 middle_initial => 'C',
137 last_name => 'Little',
138 title => 'Senior Developer',
139 address => Address->new(city => 'Madison', state => 'CT')
143 last_name => 'Kinyon',
144 title => 'Developer',
145 address => Address->new(city => 'Marysville', state => 'OH')
149 } '... created the entire company successfully';
150 isa_ok($ii, 'Company');
152 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
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');
160 is($ii->get_employee_count, 4, '... got the right employee count');
164 isa_ok($ii->employees->[0], 'Employee');
165 isa_ok($ii->employees->[0], 'Person');
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');
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');
182 isa_ok($ii->employees->[1], 'Employee');
183 isa_ok($ii->employees->[1], 'Person');
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');
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');
200 isa_ok($ii->employees->[2], 'Employee');
201 isa_ok($ii->employees->[2], 'Person');
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');
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');
218 isa_ok($ii->employees->[3], 'Employee');
219 isa_ok($ii->employees->[3], 'Person');
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');
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');
236 my $new_company = Company->new(name => 'Infinity Interactive International');
237 isa_ok($new_company, 'Company');
239 my $ii_employees = $ii->employees;
240 foreach my $employee (@$ii_employees) {
241 is($employee->company, $ii, '... has the ii company');
244 $new_company->employees($ii_employees);
246 foreach my $employee (@{$new_company->employees}) {
247 is($employee->company, $new_company, '... has the different company now');
250 ## check some error conditions for the subtypes
253 Address->new(street => {}),
254 } '... we die correctly with bad args';
257 Address->new(city => {}),
258 } '... we die correctly with bad args';
261 Address->new(state => 'British Columbia'),
262 } '... we die correctly with bad args';
265 Address->new(state => 'Connecticut'),
266 } '... we live correctly with good args';
269 Address->new(zip_code => 'AF5J6$'),
270 } '... we die correctly with bad args';
273 Address->new(zip_code => '06443'),
274 } '... we live correctly with good args';
278 } '... we die correctly without good args';
281 Company->new(name => 'Foo'),
282 } '... we live correctly without good args';
285 Company->new(name => 'Foo', employees => [ Person->new ]),
286 } '... we die correctly with good args';
289 Company->new(name => 'Foo', employees => []),
290 } '... we live correctly with good args';