0_03
[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 $@;
11 plan tests => 70;
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
182134e8 54 has 'name' => (is => 'rw', isa => 'Str');
5569c072 55 has 'address' => (is => 'rw', isa => 'Address');
56 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
57 ($_->isa('Employee') || return) for @$_; 1
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
71 package Person;
72 use strict;
73 use warnings;
74 use Moose;
75
182134e8 76 has 'first_name' => (is => 'rw', isa => 'Str');
77 has 'last_name' => (is => 'rw', isa => 'Str');
78 has 'middle_initial' => (is => 'rw', isa => 'Str', predicate => 'has_middle_initial');
5569c072 79 has 'address' => (is => 'rw', isa => 'Address');
80
81 sub full_name {
82 my $self = shift;
83 return $self->first_name .
84 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
85 $self->last_name;
86 }
87
88 package Employee;
89 use strict;
90 use warnings;
91 use Moose;
92
93 extends 'Person';
94
182134e8 95 has 'title' => (is => 'rw', isa => 'Str');
5569c072 96 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
97}
98
99my $ii;
100lives_ok {
101 $ii = Company->new(
102 name => 'Infinity Interactive',
103 address => Address->new(
104 street => '565 Plandome Rd., Suite 307',
105 city => 'Manhasset',
106 state => 'NY',
107 zip_code => '11030'
108 ),
109 employees => [
110 Employee->new(
111 first_name => 'Jeremy',
112 last_name => 'Shao',
113 title => 'President / Senior Consultant',
114 address => Address->new(city => 'Manhasset', state => 'NY')
115 ),
116 Employee->new(
117 first_name => 'Tommy',
118 last_name => 'Lee',
119 title => 'Vice President / Senior Developer',
120 address => Address->new(city => 'New York', state => 'NY')
121 ),
122 Employee->new(
123 first_name => 'Stevan',
124 middle_initial => 'C',
125 last_name => 'Little',
126 title => 'Senior Developer',
127 address => Address->new(city => 'Madison', state => 'CT')
128 ),
129 Employee->new(
130 first_name => 'Rob',
131 last_name => 'Kinyon',
132 title => 'Developer',
133 address => Address->new(city => 'Marysville', state => 'OH')
134 ),
135 ]
136 );
137} '... created the entire company successfully';
138isa_ok($ii, 'Company');
139
140is($ii->name, 'Infinity Interactive', '... got the right name for the company');
141
142isa_ok($ii->address, 'Address');
143is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
144is($ii->address->city, 'Manhasset', '... got the right city');
145is($ii->address->state, 'NY', '... got the right state');
146is($ii->address->zip_code, 11030, '... got the zip code');
147
148is($ii->get_employee_count, 4, '... got the right employee count');
149
150# employee #1
151
152isa_ok($ii->employees->[0], 'Employee');
153isa_ok($ii->employees->[0], 'Person');
154
155is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
156is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
157ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
158is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
159is($ii->employees->[0]->full_name, 'Jeremy Shao', '... got the right full name');
160is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
161is($ii->employees->[0]->company, $ii, '... got the right company');
51c5e0fb 162ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
5569c072 163
164isa_ok($ii->employees->[0]->address, 'Address');
165is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
166is($ii->employees->[0]->address->state, 'NY', '... got the right state');
167
168# employee #2
169
170isa_ok($ii->employees->[1], 'Employee');
171isa_ok($ii->employees->[1], 'Person');
172
173is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
174is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
175ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
176is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
177is($ii->employees->[1]->full_name, 'Tommy Lee', '... got the right full name');
178is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
179is($ii->employees->[1]->company, $ii, '... got the right company');
51c5e0fb 180ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
5569c072 181
182isa_ok($ii->employees->[1]->address, 'Address');
183is($ii->employees->[1]->address->city, 'New York', '... got the right city');
184is($ii->employees->[1]->address->state, 'NY', '... got the right state');
185
186# employee #3
187
188isa_ok($ii->employees->[2], 'Employee');
189isa_ok($ii->employees->[2], 'Person');
190
191is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
192is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
193ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
194is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
195is($ii->employees->[2]->full_name, 'Stevan C. Little', '... got the right full name');
196is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
197is($ii->employees->[2]->company, $ii, '... got the right company');
51c5e0fb 198ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
5569c072 199
200isa_ok($ii->employees->[2]->address, 'Address');
201is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
202is($ii->employees->[2]->address->state, 'CT', '... got the right state');
203
204# employee #4
205
206isa_ok($ii->employees->[3], 'Employee');
207isa_ok($ii->employees->[3], 'Person');
208
209is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
210is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
211ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
212is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
213is($ii->employees->[3]->full_name, 'Rob Kinyon', '... got the right full name');
214is($ii->employees->[3]->title, 'Developer', '... got the right title');
215is($ii->employees->[3]->company, $ii, '... got the right company');
51c5e0fb 216ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
5569c072 217
218isa_ok($ii->employees->[3]->address, 'Address');
219is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
220is($ii->employees->[3]->address->state, 'OH', '... got the right state');
221
222## check some error conditions for the subtypes
223
224dies_ok {
51c5e0fb 225 Address->new(street => {}),
226} '... we die correctly with bad args';
227
228dies_ok {
229 Address->new(city => {}),
230} '... we die correctly with bad args';
231
232dies_ok {
5569c072 233 Address->new(state => 'British Columbia'),
234} '... we die correctly with bad args';
235
236lives_ok {
237 Address->new(state => 'Connecticut'),
238} '... we live correctly with good args';
239
240dies_ok {
241 Address->new(zip_code => 'AF5J6$'),
242} '... we die correctly with bad args';
243
244lives_ok {
245 Address->new(zip_code => '06443'),
246} '... we live correctly with good args';
247
248dies_ok {
249 Company->new(employees => [ Person->new ]),
250} '... we die correctly with good args';
251
252lives_ok {
253 Company->new(employees => []),
254} '... we live correctly with good args';
255