Commit | Line | Data |
5569c072 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 64; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
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 | |
91 | my $ii; |
92 | lives_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'; |
130 | isa_ok($ii, 'Company'); |
131 | |
132 | is($ii->name, 'Infinity Interactive', '... got the right name for the company'); |
133 | |
134 | isa_ok($ii->address, 'Address'); |
135 | is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address'); |
136 | is($ii->address->city, 'Manhasset', '... got the right city'); |
137 | is($ii->address->state, 'NY', '... got the right state'); |
138 | is($ii->address->zip_code, 11030, '... got the zip code'); |
139 | |
140 | is($ii->get_employee_count, 4, '... got the right employee count'); |
141 | |
142 | # employee #1 |
143 | |
144 | isa_ok($ii->employees->[0], 'Employee'); |
145 | isa_ok($ii->employees->[0], 'Person'); |
146 | |
147 | is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name'); |
148 | is($ii->employees->[0]->last_name, 'Shao', '... got the right last name'); |
149 | ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial'); |
150 | is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value'); |
151 | is($ii->employees->[0]->full_name, 'Jeremy Shao', '... got the right full name'); |
152 | is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title'); |
153 | is($ii->employees->[0]->company, $ii, '... got the right company'); |
154 | |
155 | isa_ok($ii->employees->[0]->address, 'Address'); |
156 | is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city'); |
157 | is($ii->employees->[0]->address->state, 'NY', '... got the right state'); |
158 | |
159 | # employee #2 |
160 | |
161 | isa_ok($ii->employees->[1], 'Employee'); |
162 | isa_ok($ii->employees->[1], 'Person'); |
163 | |
164 | is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name'); |
165 | is($ii->employees->[1]->last_name, 'Lee', '... got the right last name'); |
166 | ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial'); |
167 | is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value'); |
168 | is($ii->employees->[1]->full_name, 'Tommy Lee', '... got the right full name'); |
169 | is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title'); |
170 | is($ii->employees->[1]->company, $ii, '... got the right company'); |
171 | |
172 | isa_ok($ii->employees->[1]->address, 'Address'); |
173 | is($ii->employees->[1]->address->city, 'New York', '... got the right city'); |
174 | is($ii->employees->[1]->address->state, 'NY', '... got the right state'); |
175 | |
176 | # employee #3 |
177 | |
178 | isa_ok($ii->employees->[2], 'Employee'); |
179 | isa_ok($ii->employees->[2], 'Person'); |
180 | |
181 | is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name'); |
182 | is($ii->employees->[2]->last_name, 'Little', '... got the right last name'); |
183 | ok($ii->employees->[2]->has_middle_initial, '... got middle initial'); |
184 | is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value'); |
185 | is($ii->employees->[2]->full_name, 'Stevan C. Little', '... got the right full name'); |
186 | is($ii->employees->[2]->title, 'Senior Developer', '... got the right title'); |
187 | is($ii->employees->[2]->company, $ii, '... got the right company'); |
188 | |
189 | isa_ok($ii->employees->[2]->address, 'Address'); |
190 | is($ii->employees->[2]->address->city, 'Madison', '... got the right city'); |
191 | is($ii->employees->[2]->address->state, 'CT', '... got the right state'); |
192 | |
193 | # employee #4 |
194 | |
195 | isa_ok($ii->employees->[3], 'Employee'); |
196 | isa_ok($ii->employees->[3], 'Person'); |
197 | |
198 | is($ii->employees->[3]->first_name, 'Rob', '... got the right first name'); |
199 | is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name'); |
200 | ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial'); |
201 | is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value'); |
202 | is($ii->employees->[3]->full_name, 'Rob Kinyon', '... got the right full name'); |
203 | is($ii->employees->[3]->title, 'Developer', '... got the right title'); |
204 | is($ii->employees->[3]->company, $ii, '... got the right company'); |
205 | |
206 | isa_ok($ii->employees->[3]->address, 'Address'); |
207 | is($ii->employees->[3]->address->city, 'Marysville', '... got the right city'); |
208 | is($ii->employees->[3]->address->state, 'OH', '... got the right state'); |
209 | |
210 | ## check some error conditions for the subtypes |
211 | |
212 | dies_ok { |
213 | Address->new(state => 'British Columbia'), |
214 | } '... we die correctly with bad args'; |
215 | |
216 | lives_ok { |
217 | Address->new(state => 'Connecticut'), |
218 | } '... we live correctly with good args'; |
219 | |
220 | dies_ok { |
221 | Address->new(zip_code => 'AF5J6$'), |
222 | } '... we die correctly with bad args'; |
223 | |
224 | lives_ok { |
225 | Address->new(zip_code => '06443'), |
226 | } '... we live correctly with good args'; |
227 | |
228 | dies_ok { |
229 | Company->new(employees => [ Person->new ]), |
230 | } '... we die correctly with good args'; |
231 | |
232 | lives_ok { |
233 | Company->new(employees => []), |
234 | } '... we live correctly with good args'; |
235 | |