6 if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
7 eval 'use Regexp::Common; use Locale::US;';
9 plan skip_all => 'Regexp::Common & Locale::US required for this test';
16 plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
25 # =begin testing SETUP
27 eval 'use Regexp::Common; use Locale::US;';
29 plan skip_all => 'Regexp::Common & Locale::US required for this test';
35 # =begin testing SETUP
40 use Mouse::Util::TypeConstraints;
43 use Regexp::Common 'zip';
45 my $STATES = Locale::US->new;
49 ( exists $STATES->{code2state}{ uc($_) }
50 || exists $STATES->{state2code}{ uc($_) } );
56 /^$RE{zip}{US}{-extended => 'allow'}$/;
59 has 'street' => ( is => 'rw', isa => 'Str' );
60 has 'city' => ( is => 'rw', isa => 'Str' );
61 has 'state' => ( is => 'rw', isa => 'USState' );
62 has 'zip_code' => ( is => 'rw', isa => 'USZipCode' );
66 use Mouse::Util::TypeConstraints;
68 has 'name' => ( is => 'rw', isa => 'Str', required => 1 );
69 has 'address' => ( is => 'rw', isa => 'Address' );
70 has 'employees' => ( is => 'rw', isa => 'ArrayRef[Employee]' );
73 my ( $self, $params ) = @_;
74 if ( @{ $self->employees || [] } ) {
75 foreach my $employee ( @{ $self->employees } ) {
76 $employee->employer($self);
81 after 'employees' => sub {
82 my ( $self, $employees ) = @_;
84 foreach my $employee ( @{$employees} ) {
85 $employee->employer($self);
93 has 'first_name' => ( is => 'rw', isa => 'Str', required => 1 );
94 has 'last_name' => ( is => 'rw', isa => 'Str', required => 1 );
95 has 'middle_initial' => (
96 is => 'rw', isa => 'Str',
97 predicate => 'has_middle_initial'
99 has 'address' => ( is => 'rw', isa => 'Address' );
103 return $self->first_name
105 $self->has_middle_initial
106 ? ' ' . $self->middle_initial . '. '
108 ) . $self->last_name;
116 has 'title' => ( is => 'rw', isa => 'Str', required => 1 );
117 has 'employer' => ( is => 'rw', isa => 'Company', weak_ref => 1 );
119 override 'full_name' => sub {
121 super() . ', ' . $self->title;
132 sub get_employee_count { scalar @{(shift)->employees} }
135 use Scalar::Util 'isweak';
141 name => 'Infinity Interactive',
142 address => Address->new(
143 street => '565 Plandome Rd., Suite 307',
150 first_name => 'Jeremy',
152 title => 'President / Senior Consultant',
154 Address->new( city => 'Manhasset', state => 'NY' )
157 first_name => 'Tommy',
159 title => 'Vice President / Senior Developer',
161 Address->new( city => 'New York', state => 'NY' )
164 first_name => 'Stevan',
165 middle_initial => 'C',
166 last_name => 'Little',
167 title => 'Senior Developer',
169 Address->new( city => 'Madison', state => 'CT' )
175 '... created the entire company successfully';
176 isa_ok( $ii, 'Company' );
178 is( $ii->name, 'Infinity Interactive',
179 '... got the right name for the company' );
181 isa_ok( $ii->address, 'Address' );
182 is( $ii->address->street, '565 Plandome Rd., Suite 307',
183 '... got the right street address' );
184 is( $ii->address->city, 'Manhasset', '... got the right city' );
185 is( $ii->address->state, 'NY', '... got the right state' );
186 is( $ii->address->zip_code, 11030, '... got the zip code' );
188 is( $ii->get_employee_count, 3, '... got the right employee count' );
192 isa_ok( $ii->employees->[0], 'Employee' );
193 isa_ok( $ii->employees->[0], 'Person' );
195 is( $ii->employees->[0]->first_name, 'Jeremy',
196 '... got the right first name' );
197 is( $ii->employees->[0]->last_name, 'Shao', '... got the right last name' );
198 ok( !$ii->employees->[0]->has_middle_initial, '... no middle initial' );
199 is( $ii->employees->[0]->middle_initial, undef,
200 '... got the right middle initial value' );
201 is( $ii->employees->[0]->full_name,
202 'Jeremy Shao, President / Senior Consultant',
203 '... got the right full name' );
204 is( $ii->employees->[0]->title, 'President / Senior Consultant',
205 '... got the right title' );
206 is( $ii->employees->[0]->employer, $ii, '... got the right company' );
207 ok( isweak( $ii->employees->[0]->{employer} ),
208 '... the company is a weak-ref' );
210 isa_ok( $ii->employees->[0]->address, 'Address' );
211 is( $ii->employees->[0]->address->city, 'Manhasset',
212 '... got the right city' );
213 is( $ii->employees->[0]->address->state, 'NY', '... got the right state' );
217 isa_ok( $ii->employees->[1], 'Employee' );
218 isa_ok( $ii->employees->[1], 'Person' );
220 is( $ii->employees->[1]->first_name, 'Tommy',
221 '... got the right first name' );
222 is( $ii->employees->[1]->last_name, 'Lee', '... got the right last name' );
223 ok( !$ii->employees->[1]->has_middle_initial, '... no middle initial' );
224 is( $ii->employees->[1]->middle_initial, undef,
225 '... got the right middle initial value' );
226 is( $ii->employees->[1]->full_name,
227 'Tommy Lee, Vice President / Senior Developer',
228 '... got the right full name' );
229 is( $ii->employees->[1]->title, 'Vice President / Senior Developer',
230 '... got the right title' );
231 is( $ii->employees->[1]->employer, $ii, '... got the right company' );
232 ok( isweak( $ii->employees->[1]->{employer} ),
233 '... the company is a weak-ref' );
235 isa_ok( $ii->employees->[1]->address, 'Address' );
236 is( $ii->employees->[1]->address->city, 'New York',
237 '... got the right city' );
238 is( $ii->employees->[1]->address->state, 'NY', '... got the right state' );
242 isa_ok( $ii->employees->[2], 'Employee' );
243 isa_ok( $ii->employees->[2], 'Person' );
245 is( $ii->employees->[2]->first_name, 'Stevan',
246 '... got the right first name' );
247 is( $ii->employees->[2]->last_name, 'Little', '... got the right last name' );
248 ok( $ii->employees->[2]->has_middle_initial, '... got middle initial' );
249 is( $ii->employees->[2]->middle_initial, 'C',
250 '... got the right middle initial value' );
251 is( $ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer',
252 '... got the right full name' );
253 is( $ii->employees->[2]->title, 'Senior Developer',
254 '... got the right title' );
255 is( $ii->employees->[2]->employer, $ii, '... got the right company' );
256 ok( isweak( $ii->employees->[2]->{employer} ),
257 '... the company is a weak-ref' );
259 isa_ok( $ii->employees->[2]->address, 'Address' );
260 is( $ii->employees->[2]->address->city, 'Madison', '... got the right city' );
261 is( $ii->employees->[2]->address->state, 'CT', '... got the right state' );
266 = Company->new( name => 'Infinity Interactive International' );
267 isa_ok( $new_company, 'Company' );
269 my $ii_employees = $ii->employees;
270 foreach my $employee (@$ii_employees) {
271 is( $employee->employer, $ii, '... has the ii company' );
274 $new_company->employees($ii_employees);
276 foreach my $employee ( @{ $new_company->employees } ) {
277 is( $employee->employer, $new_company,
278 '... has the different company now' );
281 ## check some error conditions for the subtypes
284 Address->new( street => {} ),;
286 '... we die correctly with bad args';
289 Address->new( city => {} ),;
291 '... we die correctly with bad args';
294 Address->new( state => 'British Columbia' ),;
296 '... we die correctly with bad args';
299 Address->new( state => 'Connecticut' ),;
301 '... we live correctly with good args';
304 Address->new( zip_code => 'AF5J6$' ),;
306 '... we die correctly with bad args';
309 Address->new( zip_code => '06443' ),;
311 '... we live correctly with good args';
316 '... we die correctly without good args';
319 Company->new( name => 'Foo' ),;
321 '... we live correctly without good args';
324 Company->new( name => 'Foo', employees => [ Person->new ] ),;
326 '... we die correctly with good args';
329 Company->new( name => 'Foo', employees => [] ),;
331 '... we live correctly with good args';