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 => 'ArrayRef[Employee]');
60 my ($self, $params) = @_;
61 if ($params->{employees}) {
62 foreach my $employee (@{$params->{employees}}) {
63 $employee->company($self);
68 sub get_employee_count { scalar @{(shift)->employees} }
70 after 'employees' => sub {
71 my ($self, $employees) = @_;
72 # if employees is defined, it
73 # has already been type checked
74 if (defined $employees) {
75 # make sure each gets the
76 # weak ref to the company
77 foreach my $employee (@{$employees}) {
78 $employee->company($self);
83 __PACKAGE__->meta->make_immutable(debug => 0);
89 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
90 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
91 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
92 has 'address' => (is => 'rw', isa => 'Address');
96 return $self->first_name .
97 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
101 __PACKAGE__->meta->make_immutable(debug => 0);
109 has 'title' => (is => 'rw', isa => 'Str', required => 1);
110 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
112 override 'full_name' => sub {
114 super() . ', ' . $self->title
117 __PACKAGE__->meta->make_immutable(debug => 0);
123 name => 'Infinity Interactive',
124 address => Address->new(
125 street => '565 Plandome Rd., Suite 307',
132 first_name => 'Jeremy',
134 title => 'President / Senior Consultant',
135 address => Address->new(city => 'Manhasset', state => 'NY')
138 first_name => 'Tommy',
140 title => 'Vice President / Senior Developer',
141 address => Address->new(city => 'New York', state => 'NY')
144 first_name => 'Stevan',
145 middle_initial => 'C',
146 last_name => 'Little',
147 title => 'Senior Developer',
148 address => Address->new(city => 'Madison', state => 'CT')
152 } '... created the entire company successfully';
153 isa_ok($ii, 'Company');
155 is($ii->name, 'Infinity Interactive', '... got the right name for the company');
157 isa_ok($ii->address, 'Address');
158 is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
159 is($ii->address->city, 'Manhasset', '... got the right city');
160 is($ii->address->state, 'NY', '... got the right state');
161 is($ii->address->zip_code, 11030, '... got the zip code');
163 is($ii->get_employee_count, 3, '... got the right employee count');
167 isa_ok($ii->employees->[0], 'Employee');
168 isa_ok($ii->employees->[0], 'Person');
170 is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
171 is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
172 ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
173 is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
174 is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
175 is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
176 is($ii->employees->[0]->company, $ii, '... got the right company');
177 ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
179 isa_ok($ii->employees->[0]->address, 'Address');
180 is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
181 is($ii->employees->[0]->address->state, 'NY', '... got the right state');
185 isa_ok($ii->employees->[1], 'Employee');
186 isa_ok($ii->employees->[1], 'Person');
188 is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
189 is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
190 ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
191 is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
192 is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
193 is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
194 is($ii->employees->[1]->company, $ii, '... got the right company');
195 ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
197 isa_ok($ii->employees->[1]->address, 'Address');
198 is($ii->employees->[1]->address->city, 'New York', '... got the right city');
199 is($ii->employees->[1]->address->state, 'NY', '... got the right state');
203 isa_ok($ii->employees->[2], 'Employee');
204 isa_ok($ii->employees->[2], 'Person');
206 is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
207 is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
208 ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
209 is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
210 is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
211 is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
212 is($ii->employees->[2]->company, $ii, '... got the right company');
213 ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
215 isa_ok($ii->employees->[2]->address, 'Address');
216 is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
217 is($ii->employees->[2]->address->state, 'CT', '... got the right state');
221 my $new_company = Company->new(name => 'Infinity Interactive International');
222 isa_ok($new_company, 'Company');
224 my $ii_employees = $ii->employees;
225 foreach my $employee (@$ii_employees) {
226 is($employee->company, $ii, '... has the ii company');
229 $new_company->employees($ii_employees);
231 foreach my $employee (@{$new_company->employees}) {
232 is($employee->company, $new_company, '... has the different company now');
235 ## check some error conditions for the subtypes
238 Address->new(street => {}),
239 } '... we die correctly with bad args';
242 Address->new(city => {}),
243 } '... we die correctly with bad args';
246 Address->new(state => 'British Columbia'),
247 } '... we die correctly with bad args';
250 Address->new(state => 'Connecticut'),
251 } '... we live correctly with good args';
254 Address->new(zip_code => 'AF5J6$'),
255 } '... we die correctly with bad args';
258 Address->new(zip_code => '06443'),
259 } '... we live correctly with good args';
263 } '... we die correctly without good args';
266 Company->new(name => 'Foo'),
267 } '... we live correctly without good args';
270 Company->new(name => 'Foo', employees => [ Person->new ]),
271 } '... we die correctly with good args';
274 Company->new(name => 'Foo', employees => []),
275 } '... we live correctly with good args';