updatin
[gitmo/Moose.git] / t / 004_basic.t
CommitLineData
5569c072 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
51c5e0fb 6use Test::More;
7
8BEGIN {
9 eval "use Regexp::Common; use Locale::US;";
10 plan skip_all => "Regexp::Common & Locale::US required for this test" if $@;
ad5ed80c 11 plan tests => 81;
51c5e0fb 12}
13
5569c072 14use Test::Exception;
51c5e0fb 15use Scalar::Util 'isweak';
16
5569c072 17BEGIN {
18 use_ok('Moose');
19}
20
21{
22 package Address;
23 use strict;
24 use warnings;
25 use Moose;
05d9eaf6 26 use Moose::Util::TypeConstraints;
5569c072 27
28 use Locale::US;
29 use Regexp::Common 'zip';
30
31 my $STATES = Locale::US->new;
32
33 subtype USState
34 => as Str
35 => where {
36 (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
37 };
38
39 subtype USZipCode
40 => as Value
41 => where {
42 /^$RE{zip}{US}{-extended => 'allow'}$/
43 };
44
182134e8 45 has 'street' => (is => 'rw', isa => 'Str');
46 has 'city' => (is => 'rw', isa => 'Str');
47 has 'state' => (is => 'rw', isa => 'USState');
48 has 'zip_code' => (is => 'rw', isa => 'USZipCode');
5569c072 49
50 package Company;
51 use strict;
52 use warnings;
53 use Moose;
05d9eaf6 54 use Moose::Util::TypeConstraints;
5569c072 55
7c6cacb4 56 has 'name' => (is => 'rw', isa => 'Str', required => 1);
5569c072 57 has 'address' => (is => 'rw', isa => 'Address');
58 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
ad5ed80c 59 (blessed($_) && $_->isa('Employee') || return) for @$_; 1
5569c072 60 });
61
62 sub BUILD {
d7f17ebb 63 my ($self, $params) = @_;
64 if ($params->{employees}) {
65 foreach my $employee (@{$params->{employees}}) {
5569c072 66 $employee->company($self);
67 }
68 }
69 }
70
71 sub get_employee_count { scalar @{(shift)->employees} }
72
ad5ed80c 73 after 'employees' => sub {
74 my ($self, $employees) = @_;
75 # if employees is defined, it
76 # has already been type checked
77 if (defined $employees) {
78 # make sure each gets the
79 # weak ref to the company
80 foreach my $employee (@{$employees}) {
81 $employee->company($self);
82 }
83 }
84 };
85
5569c072 86 package Person;
87 use strict;
88 use warnings;
89 use Moose;
90
7c6cacb4 91 has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
92 has 'last_name' => (is => 'rw', isa => 'Str', required => 1);
182134e8 93 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
5569c072 94 has 'address' => (is => 'rw', isa => 'Address');
95
96 sub full_name {
97 my $self = shift;
98 return $self->first_name .
99 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
100 $self->last_name;
101 }
102
103 package Employee;
104 use strict;
105 use warnings;
106 use Moose;
107
108 extends 'Person';
109
7c6cacb4 110 has 'title' => (is => 'rw', isa => 'Str', required => 1);
5569c072 111 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
b6fe348f 112
113 override 'full_name' => sub {
114 my $self = shift;
115 super() . ', ' . $self->title
116 };
5569c072 117}
118
119my $ii;
120lives_ok {
05d9eaf6 121 $ii = Company->new({
5569c072 122 name => 'Infinity Interactive',
123 address => Address->new(
124 street => '565 Plandome Rd., Suite 307',
125 city => 'Manhasset',
126 state => 'NY',
127 zip_code => '11030'
128 ),
129 employees => [
130 Employee->new(
131 first_name => 'Jeremy',
132 last_name => 'Shao',
133 title => 'President / Senior Consultant',
134 address => Address->new(city => 'Manhasset', state => 'NY')
135 ),
136 Employee->new(
137 first_name => 'Tommy',
138 last_name => 'Lee',
139 title => 'Vice President / Senior Developer',
140 address => Address->new(city => 'New York', state => 'NY')
141 ),
142 Employee->new(
143 first_name => 'Stevan',
144 middle_initial => 'C',
145 last_name => 'Little',
146 title => 'Senior Developer',
147 address => Address->new(city => 'Madison', state => 'CT')
148 ),
149 Employee->new(
150 first_name => 'Rob',
151 last_name => 'Kinyon',
152 title => 'Developer',
153 address => Address->new(city => 'Marysville', state => 'OH')
154 ),
155 ]
05d9eaf6 156 });
5569c072 157} '... created the entire company successfully';
158isa_ok($ii, 'Company');
159
160is($ii->name, 'Infinity Interactive', '... got the right name for the company');
161
162isa_ok($ii->address, 'Address');
163is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
164is($ii->address->city, 'Manhasset', '... got the right city');
165is($ii->address->state, 'NY', '... got the right state');
166is($ii->address->zip_code, 11030, '... got the zip code');
167
168is($ii->get_employee_count, 4, '... got the right employee count');
169
170# employee #1
171
172isa_ok($ii->employees->[0], 'Employee');
173isa_ok($ii->employees->[0], 'Person');
174
175is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
176is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
177ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
178is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 179is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
5569c072 180is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
181is($ii->employees->[0]->company, $ii, '... got the right company');
51c5e0fb 182ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
5569c072 183
184isa_ok($ii->employees->[0]->address, 'Address');
185is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
186is($ii->employees->[0]->address->state, 'NY', '... got the right state');
187
188# employee #2
189
190isa_ok($ii->employees->[1], 'Employee');
191isa_ok($ii->employees->[1], 'Person');
192
193is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
194is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
195ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
196is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 197is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
5569c072 198is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
199is($ii->employees->[1]->company, $ii, '... got the right company');
51c5e0fb 200ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
5569c072 201
202isa_ok($ii->employees->[1]->address, 'Address');
203is($ii->employees->[1]->address->city, 'New York', '... got the right city');
204is($ii->employees->[1]->address->state, 'NY', '... got the right state');
205
206# employee #3
207
208isa_ok($ii->employees->[2], 'Employee');
209isa_ok($ii->employees->[2], 'Person');
210
211is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
212is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
213ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
214is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
b6fe348f 215is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
5569c072 216is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
217is($ii->employees->[2]->company, $ii, '... got the right company');
51c5e0fb 218ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
5569c072 219
220isa_ok($ii->employees->[2]->address, 'Address');
221is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
222is($ii->employees->[2]->address->state, 'CT', '... got the right state');
223
224# employee #4
225
226isa_ok($ii->employees->[3], 'Employee');
227isa_ok($ii->employees->[3], 'Person');
228
229is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
230is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
231ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
232is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 233is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
5569c072 234is($ii->employees->[3]->title, 'Developer', '... got the right title');
235is($ii->employees->[3]->company, $ii, '... got the right company');
51c5e0fb 236ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
5569c072 237
238isa_ok($ii->employees->[3]->address, 'Address');
239is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
240is($ii->employees->[3]->address->state, 'OH', '... got the right state');
241
ad5ed80c 242# create new company
243
244my $new_company = Company->new(name => 'Infinity Interactive International');
245isa_ok($new_company, 'Company');
246
247my $ii_employees = $ii->employees;
248foreach my $employee (@$ii_employees) {
249 is($employee->company, $ii, '... has the ii company');
250}
251
252$new_company->employees($ii_employees);
253
254foreach my $employee (@{$new_company->employees}) {
255 is($employee->company, $new_company, '... has the different company now');
256}
257
5569c072 258## check some error conditions for the subtypes
259
260dies_ok {
51c5e0fb 261 Address->new(street => {}),
262} '... we die correctly with bad args';
263
264dies_ok {
265 Address->new(city => {}),
266} '... we die correctly with bad args';
267
268dies_ok {
5569c072 269 Address->new(state => 'British Columbia'),
270} '... we die correctly with bad args';
271
272lives_ok {
273 Address->new(state => 'Connecticut'),
274} '... we live correctly with good args';
275
276dies_ok {
277 Address->new(zip_code => 'AF5J6$'),
278} '... we die correctly with bad args';
279
280lives_ok {
281 Address->new(zip_code => '06443'),
282} '... we live correctly with good args';
283
284dies_ok {
7c6cacb4 285 Company->new(),
286} '... we die correctly without good args';
287
288lives_ok {
289 Company->new(name => 'Foo'),
290} '... we live correctly without good args';
291
292dies_ok {
293 Company->new(name => 'Foo', employees => [ Person->new ]),
5569c072 294} '... we die correctly with good args';
295
296lives_ok {
7c6cacb4 297 Company->new(name => 'Foo', employees => []),
5569c072 298} '... we live correctly with good args';
299