MOOOOOOOOOOOOOOSE
[gitmo/Moose.git] / t / 004_basic.t
CommitLineData
5569c072 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 64;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11}
12
13{
14 package Address;
15 use strict;
16 use warnings;
17 use Moose;
18
19 use Locale::US;
20 use Regexp::Common 'zip';
21
22 my $STATES = Locale::US->new;
23
24 subtype USState
25 => as Str
26 => where {
27 (exists $STATES->{code2state}{uc($_)} || exists $STATES->{state2code}{uc($_)})
28 };
29
30 subtype USZipCode
31 => as Value
32 => where {
33 /^$RE{zip}{US}{-extended => 'allow'}$/
34 };
35
36 has 'street' => (is => 'rw', isa => Str());
37 has 'city' => (is => 'rw', isa => Str());
38 has 'state' => (is => 'rw', isa => USState());
39 has 'zip_code' => (is => 'rw', isa => USZipCode());
40
41 package Company;
42 use strict;
43 use warnings;
44 use Moose;
45
46 has 'name' => (is => 'rw', isa => Str());
47 has 'address' => (is => 'rw', isa => 'Address');
48 has 'employees' => (is => 'rw', isa => subtype ArrayRef => where {
49 ($_->isa('Employee') || return) for @$_; 1
50 });
51
52 sub BUILD {
53 my ($self, %params) = @_;
54 if ($params{employees}) {
55 foreach my $employee (@{$params{employees}}) {
56 $employee->company($self);
57 }
58 }
59 }
60
61 sub get_employee_count { scalar @{(shift)->employees} }
62
63 package Person;
64 use strict;
65 use warnings;
66 use Moose;
67
68 has 'first_name' => (is => 'rw', isa => Str());
69 has 'last_name' => (is => 'rw', isa => Str());
70 has 'middle_initial' => (is => 'rw', isa => Str(), predicate => 'has_middle_initial');
71 has 'address' => (is => 'rw', isa => 'Address');
72
73 sub full_name {
74 my $self = shift;
75 return $self->first_name .
76 ($self->has_middle_initial ? ' ' . $self->middle_initial . '. ' : ' ') .
77 $self->last_name;
78 }
79
80 package Employee;
81 use strict;
82 use warnings;
83 use Moose;
84
85 extends 'Person';
86
87 has 'title' => (is => 'rw', isa => Str());
88 has 'company' => (is => 'rw', isa => 'Company', weak_ref => 1);
89}
90
91my $ii;
92lives_ok {
93 $ii = Company->new(
94 name => 'Infinity Interactive',
95 address => Address->new(
96 street => '565 Plandome Rd., Suite 307',
97 city => 'Manhasset',
98 state => 'NY',
99 zip_code => '11030'
100 ),
101 employees => [
102 Employee->new(
103 first_name => 'Jeremy',
104 last_name => 'Shao',
105 title => 'President / Senior Consultant',
106 address => Address->new(city => 'Manhasset', state => 'NY')
107 ),
108 Employee->new(
109 first_name => 'Tommy',
110 last_name => 'Lee',
111 title => 'Vice President / Senior Developer',
112 address => Address->new(city => 'New York', state => 'NY')
113 ),
114 Employee->new(
115 first_name => 'Stevan',
116 middle_initial => 'C',
117 last_name => 'Little',
118 title => 'Senior Developer',
119 address => Address->new(city => 'Madison', state => 'CT')
120 ),
121 Employee->new(
122 first_name => 'Rob',
123 last_name => 'Kinyon',
124 title => 'Developer',
125 address => Address->new(city => 'Marysville', state => 'OH')
126 ),
127 ]
128 );
129} '... created the entire company successfully';
130isa_ok($ii, 'Company');
131
132is($ii->name, 'Infinity Interactive', '... got the right name for the company');
133
134isa_ok($ii->address, 'Address');
135is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
136is($ii->address->city, 'Manhasset', '... got the right city');
137is($ii->address->state, 'NY', '... got the right state');
138is($ii->address->zip_code, 11030, '... got the zip code');
139
140is($ii->get_employee_count, 4, '... got the right employee count');
141
142# employee #1
143
144isa_ok($ii->employees->[0], 'Employee');
145isa_ok($ii->employees->[0], 'Person');
146
147is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
148is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
149ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
150is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
151is($ii->employees->[0]->full_name, 'Jeremy Shao', '... got the right full name');
152is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
153is($ii->employees->[0]->company, $ii, '... got the right company');
154
155isa_ok($ii->employees->[0]->address, 'Address');
156is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
157is($ii->employees->[0]->address->state, 'NY', '... got the right state');
158
159# employee #2
160
161isa_ok($ii->employees->[1], 'Employee');
162isa_ok($ii->employees->[1], 'Person');
163
164is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
165is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
166ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
167is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
168is($ii->employees->[1]->full_name, 'Tommy Lee', '... got the right full name');
169is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
170is($ii->employees->[1]->company, $ii, '... got the right company');
171
172isa_ok($ii->employees->[1]->address, 'Address');
173is($ii->employees->[1]->address->city, 'New York', '... got the right city');
174is($ii->employees->[1]->address->state, 'NY', '... got the right state');
175
176# employee #3
177
178isa_ok($ii->employees->[2], 'Employee');
179isa_ok($ii->employees->[2], 'Person');
180
181is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
182is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
183ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
184is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
185is($ii->employees->[2]->full_name, 'Stevan C. Little', '... got the right full name');
186is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
187is($ii->employees->[2]->company, $ii, '... got the right company');
188
189isa_ok($ii->employees->[2]->address, 'Address');
190is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
191is($ii->employees->[2]->address->state, 'CT', '... got the right state');
192
193# employee #4
194
195isa_ok($ii->employees->[3], 'Employee');
196isa_ok($ii->employees->[3], 'Person');
197
198is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
199is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
200ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
201is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
202is($ii->employees->[3]->full_name, 'Rob Kinyon', '... got the right full name');
203is($ii->employees->[3]->title, 'Developer', '... got the right title');
204is($ii->employees->[3]->company, $ii, '... got the right company');
205
206isa_ok($ii->employees->[3]->address, 'Address');
207is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
208is($ii->employees->[3]->address->state, 'OH', '... got the right state');
209
210## check some error conditions for the subtypes
211
212dies_ok {
213 Address->new(state => 'British Columbia'),
214} '... we die correctly with bad args';
215
216lives_ok {
217 Address->new(state => 'Connecticut'),
218} '... we live correctly with good args';
219
220dies_ok {
221 Address->new(zip_code => 'AF5J6$'),
222} '... we die correctly with bad args';
223
224lives_ok {
225 Address->new(zip_code => '06443'),
226} '... we live correctly with good args';
227
228dies_ok {
229 Company->new(employees => [ Person->new ]),
230} '... we die correctly with good args';
231
232lives_ok {
233 Company->new(employees => []),
234} '... we live correctly with good args';
235