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