6 eval 'use Regexp::Common; use Locale::US;';
8 plan skip_all => 'Regexp::Common & Locale::US required for this test';
20 # =begin testing SETUP
22 eval 'use Regexp::Common; use Locale::US;';
24 plan skip_all => 'Regexp::Common & Locale::US required for this test';
30 # =begin testing SETUP
35 use Mouse::Util::TypeConstraints;
38 use Regexp::Common 'zip';
40 my $STATES = Locale::US->new;
44 ( exists $STATES->{code2state}{ uc($_) }
45 || exists $STATES->{state2code}{ uc($_) } );
51 /^$RE{zip}{US}{-extended => 'allow'}$/;
54 has 'street' => ( is => 'rw', isa => 'Str' );
55 has 'city' => ( is => 'rw', isa => 'Str' );
56 has 'state' => ( is => 'rw', isa => 'USState' );
57 has 'zip_code' => ( is => 'rw', isa => 'USZipCode' );
61 use Mouse::Util::TypeConstraints;
63 has 'name' => ( is => 'rw', isa => 'Str', required => 1 );
64 has 'address' => ( is => 'rw', isa => 'Address' );
65 has 'employees' => ( is => 'rw', isa => 'ArrayRef[Employee]' );
68 my ( $self, $params ) = @_;
69 if ( @{ $self->employees || [] } ) {
70 foreach my $employee ( @{ $self->employees } ) {
71 $employee->employer($self);
76 after 'employees' => sub {
77 my ( $self, $employees ) = @_;
79 foreach my $employee ( @{$employees} ) {
80 $employee->employer($self);
88 has 'first_name' => ( is => 'rw', isa => 'Str', required => 1 );
89 has 'last_name' => ( is => 'rw', isa => 'Str', required => 1 );
90 has 'middle_initial' => (
91 is => 'rw', isa => 'Str',
92 predicate => 'has_middle_initial'
94 has 'address' => ( is => 'rw', isa => 'Address' );
98 return $self->first_name
100 $self->has_middle_initial
101 ? ' ' . $self->middle_initial . '. '
103 ) . $self->last_name;
111 has 'title' => ( is => 'rw', isa => 'Str', required => 1 );
112 has 'employer' => ( is => 'rw', isa => 'Company', weak_ref => 1 );
114 override 'full_name' => sub {
116 super() . ', ' . $self->title;
127 sub get_employee_count { scalar @{(shift)->employees} }
130 use Scalar::Util 'isweak';
136 name => 'Infinity Interactive',
137 address => Address->new(
138 street => '565 Plandome Rd., Suite 307',
145 first_name => 'Jeremy',
147 title => 'President / Senior Consultant',
149 Address->new( city => 'Manhasset', state => 'NY' )
152 first_name => 'Tommy',
154 title => 'Vice President / Senior Developer',
156 Address->new( city => 'New York', state => 'NY' )
159 first_name => 'Stevan',
160 middle_initial => 'C',
161 last_name => 'Little',
162 title => 'Senior Developer',
164 Address->new( city => 'Madison', state => 'CT' )
170 '... created the entire company successfully';
171 isa_ok( $ii, 'Company' );
173 is( $ii->name, 'Infinity Interactive',
174 '... got the right name for the company' );
176 isa_ok( $ii->address, 'Address' );
177 is( $ii->address->street, '565 Plandome Rd., Suite 307',
178 '... got the right street address' );
179 is( $ii->address->city, 'Manhasset', '... got the right city' );
180 is( $ii->address->state, 'NY', '... got the right state' );
181 is( $ii->address->zip_code, 11030, '... got the zip code' );
183 is( $ii->get_employee_count, 3, '... got the right employee count' );
187 isa_ok( $ii->employees->[0], 'Employee' );
188 isa_ok( $ii->employees->[0], 'Person' );
190 is( $ii->employees->[0]->first_name, 'Jeremy',
191 '... got the right first name' );
192 is( $ii->employees->[0]->last_name, 'Shao', '... got the right last name' );
193 ok( !$ii->employees->[0]->has_middle_initial, '... no middle initial' );
194 is( $ii->employees->[0]->middle_initial, undef,
195 '... got the right middle initial value' );
196 is( $ii->employees->[0]->full_name,
197 'Jeremy Shao, President / Senior Consultant',
198 '... got the right full name' );
199 is( $ii->employees->[0]->title, 'President / Senior Consultant',
200 '... got the right title' );
201 is( $ii->employees->[0]->employer, $ii, '... got the right company' );
202 ok( isweak( $ii->employees->[0]->{employer} ),
203 '... the company is a weak-ref' );
205 isa_ok( $ii->employees->[0]->address, 'Address' );
206 is( $ii->employees->[0]->address->city, 'Manhasset',
207 '... got the right city' );
208 is( $ii->employees->[0]->address->state, 'NY', '... got the right state' );
212 isa_ok( $ii->employees->[1], 'Employee' );
213 isa_ok( $ii->employees->[1], 'Person' );
215 is( $ii->employees->[1]->first_name, 'Tommy',
216 '... got the right first name' );
217 is( $ii->employees->[1]->last_name, 'Lee', '... got the right last name' );
218 ok( !$ii->employees->[1]->has_middle_initial, '... no middle initial' );
219 is( $ii->employees->[1]->middle_initial, undef,
220 '... got the right middle initial value' );
221 is( $ii->employees->[1]->full_name,
222 'Tommy Lee, Vice President / Senior Developer',
223 '... got the right full name' );
224 is( $ii->employees->[1]->title, 'Vice President / Senior Developer',
225 '... got the right title' );
226 is( $ii->employees->[1]->employer, $ii, '... got the right company' );
227 ok( isweak( $ii->employees->[1]->{employer} ),
228 '... the company is a weak-ref' );
230 isa_ok( $ii->employees->[1]->address, 'Address' );
231 is( $ii->employees->[1]->address->city, 'New York',
232 '... got the right city' );
233 is( $ii->employees->[1]->address->state, 'NY', '... got the right state' );
237 isa_ok( $ii->employees->[2], 'Employee' );
238 isa_ok( $ii->employees->[2], 'Person' );
240 is( $ii->employees->[2]->first_name, 'Stevan',
241 '... got the right first name' );
242 is( $ii->employees->[2]->last_name, 'Little', '... got the right last name' );
243 ok( $ii->employees->[2]->has_middle_initial, '... got middle initial' );
244 is( $ii->employees->[2]->middle_initial, 'C',
245 '... got the right middle initial value' );
246 is( $ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer',
247 '... got the right full name' );
248 is( $ii->employees->[2]->title, 'Senior Developer',
249 '... got the right title' );
250 is( $ii->employees->[2]->employer, $ii, '... got the right company' );
251 ok( isweak( $ii->employees->[2]->{employer} ),
252 '... the company is a weak-ref' );
254 isa_ok( $ii->employees->[2]->address, 'Address' );
255 is( $ii->employees->[2]->address->city, 'Madison', '... got the right city' );
256 is( $ii->employees->[2]->address->state, 'CT', '... got the right state' );
261 = Company->new( name => 'Infinity Interactive International' );
262 isa_ok( $new_company, 'Company' );
264 my $ii_employees = $ii->employees;
265 foreach my $employee (@$ii_employees) {
266 is( $employee->employer, $ii, '... has the ii company' );
269 $new_company->employees($ii_employees);
271 foreach my $employee ( @{ $new_company->employees } ) {
272 is( $employee->employer, $new_company,
273 '... has the different company now' );
276 ## check some error conditions for the subtypes
279 Address->new( street => {} ),;
281 '... we die correctly with bad args';
284 Address->new( city => {} ),;
286 '... we die correctly with bad args';
289 Address->new( state => 'British Columbia' ),;
291 '... we die correctly with bad args';
294 Address->new( state => 'Connecticut' ),;
296 '... we live correctly with good args';
299 Address->new( zip_code => 'AF5J6$' ),;
301 '... we die correctly with bad args';
304 Address->new( zip_code => '06443' ),;
306 '... we live correctly with good args';
311 '... we die correctly without good args';
314 Company->new( name => 'Foo' ),;
316 '... we live correctly without good args';
319 Company->new( name => 'Foo', employees => [ Person->new ] ),;
321 '... we die correctly with good args';
324 Company->new( name => 'Foo', employees => [] ),;
326 '... we live correctly with good args';