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';
22 use Moose::Util::TypeConstraints;
25 use Regexp::Common 'zip';
27 my $STATES = Locale::US->new;
32 (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
38 /^$RE{zip}{US}{-extended => 'allow'}$/
41 has 'street' => (is => 'rw', isa => 'Str');
42 has 'city' => (is => 'rw', isa => 'Str');
43 has 'state' => (is => 'rw', isa => 'USState');
44 has 'zip_code' => (is => 'rw', isa => 'USZipCode');
46 __PACKAGE__->meta->make_immutable(debug => 0);
51 use Moose::Util::TypeConstraints;
53 has 'name' => (is => 'rw', isa => 'Str', required => 1);
54 has 'address' => (is => 'rw', isa => 'Address');
55 has 'employees' => (is => 'rw', isa => 'ArrayRef[Employee]');
58 my ($self, $params) = @_;
59 if ($params->{employees}) {
60 foreach my $employee (@{$params->{employees}}) {
61 $employee->company($self);
66 sub get_employee_count { scalar @{(shift)->employees} }
68 after 'employees' => sub {
69 my ($self, $employees) = @_;
70 # if employees is defined, it
71 # has already been type checked
72 if (defined $employees) {
73 # make sure each gets the
74 # weak ref to the company
75 foreach my $employee (@{$employees}) {
76 $employee->company($self);
81 __PACKAGE__->meta->make_immutable(debug => 0);
87 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
88 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
89 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
90 has 'address' => (is => 'rw', isa => 'Address');
94 return $self->first_name .
95 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
99 __PACKAGE__->meta->make_immutable(debug => 0);
107 has 'title' => (is => 'rw', isa => 'Str', required => 1);
108 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
110 override 'full_name' => sub {
112 super() . ', ' . $self->title
115 __PACKAGE__->meta->make_immutable(debug => 0);
121 name => 'Infinity Interactive',
122 address => Address->new(
123 street => '565 Plandome Rd., Suite 307',
130 first_name => 'Jeremy',
132 title => 'President / Senior Consultant',
133 address => Address->new(city => 'Manhasset', state => 'NY')
136 first_name => 'Tommy',
138 title => 'Vice President / Senior Developer',
139 address => Address->new(city => 'New York', state => 'NY')
142 first_name => 'Stevan',
143 middle_initial => 'C',
144 last_name => 'Little',
145 title => 'Senior Developer',
146 address => Address->new(city => 'Madison', state => 'CT')
150 } '... created the entire company successfully';
151 isa_ok($ii, 'Company');
153 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
155 isa_ok($ii->address, 'Address');
156 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
157 is($ii->address->city, 'Manhasset', '... got the right city');
158 is($ii->address->state, 'NY', '... got the right state');
159 is($ii->address->zip_code, 11030, '... got the zip code');
161 is($ii->get_employee_count, 3, '... got the right employee count');
165 isa_ok($ii->employees->[0], 'Employee');
166 isa_ok($ii->employees->[0], 'Person');
168 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
169 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
170 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
171 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
172 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
173 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
174 is($ii->employees->[0]->company, $ii, '... got the right company');
175 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
177 isa_ok($ii->employees->[0]->address, 'Address');
178 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
179 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
183 isa_ok($ii->employees->[1], 'Employee');
184 isa_ok($ii->employees->[1], 'Person');
186 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
187 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
188 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
189 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
190 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
191 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
192 is($ii->employees->[1]->company, $ii, '... got the right company');
193 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
195 isa_ok($ii->employees->[1]->address, 'Address');
196 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
197 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
201 isa_ok($ii->employees->[2], 'Employee');
202 isa_ok($ii->employees->[2], 'Person');
204 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
205 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
206 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
207 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
208 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
209 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
210 is($ii->employees->[2]->company, $ii, '... got the right company');
211 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
213 isa_ok($ii->employees->[2]->address, 'Address');
214 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
215 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
219 my $new_company = Company->new(name => 'Infinity Interactive International');
220 isa_ok($new_company, 'Company');
222 my $ii_employees = $ii->employees;
223 foreach my $employee (@$ii_employees) {
224 is($employee->company, $ii, '... has the ii company');
227 $new_company->employees($ii_employees);
229 foreach my $employee (@{$new_company->employees}) {
230 is($employee->company, $new_company, '... has the different company now');
233 ## check some error conditions for the subtypes
236 Address->new(street => {}),
237 } '... we die correctly with bad args';
240 Address->new(city => {}),
241 } '... we die correctly with bad args';
244 Address->new(state => 'British Columbia'),
245 } '... we die correctly with bad args';
248 Address->new(state => 'Connecticut'),
249 } '... we live correctly with good args';
252 Address->new(zip_code => 'AF5J6$'),
253 } '... we die correctly with bad args';
256 Address->new(zip_code => '06443'),
257 } '... we live correctly with good args';
261 } '... we die correctly without good args';
264 Company->new(name => 'Foo'),
265 } '... we live correctly without good args';
268 Company->new(name => 'Foo', employees => [ Person->new ]),
269 } '... we die correctly with good args';
272 Company->new(name => 'Foo', employees => []),
273 } '... we live correctly with good args';