Commit | Line | Data |
5569c072 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
51c5e0fb |
6 | use Test::More; |
7 | |
8 | BEGIN { |
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 |
14 | use Test::Exception; |
51c5e0fb |
15 | use Scalar::Util 'isweak'; |
16 | |
5569c072 |
17 | BEGIN { |
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); |
b6fe348f |
97 | |
98 | override 'full_name' => sub { |
99 | my $self = shift; |
100 | super() . ', ' . $self->title |
101 | }; |
5569c072 |
102 | } |
103 | |
104 | my $ii; |
105 | lives_ok { |
106 | $ii = Company->new( |
107 | name => 'Infinity Interactive', |
108 | address => Address->new( |
109 | street => '565 Plandome Rd., Suite 307', |
110 | city => 'Manhasset', |
111 | state => 'NY', |
112 | zip_code => '11030' |
113 | ), |
114 | employees => [ |
115 | Employee->new( |
116 | first_name => 'Jeremy', |
117 | last_name => 'Shao', |
118 | title => 'President / Senior Consultant', |
119 | address => Address->new(city => 'Manhasset', state => 'NY') |
120 | ), |
121 | Employee->new( |
122 | first_name => 'Tommy', |
123 | last_name => 'Lee', |
124 | title => 'Vice President / Senior Developer', |
125 | address => Address->new(city => 'New York', state => 'NY') |
126 | ), |
127 | Employee->new( |
128 | first_name => 'Stevan', |
129 | middle_initial => 'C', |
130 | last_name => 'Little', |
131 | title => 'Senior Developer', |
132 | address => Address->new(city => 'Madison', state => 'CT') |
133 | ), |
134 | Employee->new( |
135 | first_name => 'Rob', |
136 | last_name => 'Kinyon', |
137 | title => 'Developer', |
138 | address => Address->new(city => 'Marysville', state => 'OH') |
139 | ), |
140 | ] |
141 | ); |
142 | } '... created the entire company successfully'; |
143 | isa_ok($ii, 'Company'); |
144 | |
145 | is($ii->name, 'Infinity Interactive', '... got the right name for the company'); |
146 | |
147 | isa_ok($ii->address, 'Address'); |
148 | is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address'); |
149 | is($ii->address->city, 'Manhasset', '... got the right city'); |
150 | is($ii->address->state, 'NY', '... got the right state'); |
151 | is($ii->address->zip_code, 11030, '... got the zip code'); |
152 | |
153 | is($ii->get_employee_count, 4, '... got the right employee count'); |
154 | |
155 | # employee #1 |
156 | |
157 | isa_ok($ii->employees->[0], 'Employee'); |
158 | isa_ok($ii->employees->[0], 'Person'); |
159 | |
160 | is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name'); |
161 | is($ii->employees->[0]->last_name, 'Shao', '... got the right last name'); |
162 | ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial'); |
163 | is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value'); |
b6fe348f |
164 | is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name'); |
5569c072 |
165 | is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title'); |
166 | is($ii->employees->[0]->company, $ii, '... got the right company'); |
51c5e0fb |
167 | ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref'); |
5569c072 |
168 | |
169 | isa_ok($ii->employees->[0]->address, 'Address'); |
170 | is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city'); |
171 | is($ii->employees->[0]->address->state, 'NY', '... got the right state'); |
172 | |
173 | # employee #2 |
174 | |
175 | isa_ok($ii->employees->[1], 'Employee'); |
176 | isa_ok($ii->employees->[1], 'Person'); |
177 | |
178 | is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name'); |
179 | is($ii->employees->[1]->last_name, 'Lee', '... got the right last name'); |
180 | ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial'); |
181 | is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value'); |
b6fe348f |
182 | is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name'); |
5569c072 |
183 | is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title'); |
184 | is($ii->employees->[1]->company, $ii, '... got the right company'); |
51c5e0fb |
185 | ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref'); |
5569c072 |
186 | |
187 | isa_ok($ii->employees->[1]->address, 'Address'); |
188 | is($ii->employees->[1]->address->city, 'New York', '... got the right city'); |
189 | is($ii->employees->[1]->address->state, 'NY', '... got the right state'); |
190 | |
191 | # employee #3 |
192 | |
193 | isa_ok($ii->employees->[2], 'Employee'); |
194 | isa_ok($ii->employees->[2], 'Person'); |
195 | |
196 | is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name'); |
197 | is($ii->employees->[2]->last_name, 'Little', '... got the right last name'); |
198 | ok($ii->employees->[2]->has_middle_initial, '... got middle initial'); |
199 | is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value'); |
b6fe348f |
200 | is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name'); |
5569c072 |
201 | is($ii->employees->[2]->title, 'Senior Developer', '... got the right title'); |
202 | is($ii->employees->[2]->company, $ii, '... got the right company'); |
51c5e0fb |
203 | ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref'); |
5569c072 |
204 | |
205 | isa_ok($ii->employees->[2]->address, 'Address'); |
206 | is($ii->employees->[2]->address->city, 'Madison', '... got the right city'); |
207 | is($ii->employees->[2]->address->state, 'CT', '... got the right state'); |
208 | |
209 | # employee #4 |
210 | |
211 | isa_ok($ii->employees->[3], 'Employee'); |
212 | isa_ok($ii->employees->[3], 'Person'); |
213 | |
214 | is($ii->employees->[3]->first_name, 'Rob', '... got the right first name'); |
215 | is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name'); |
216 | ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial'); |
217 | is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value'); |
b6fe348f |
218 | is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name'); |
5569c072 |
219 | is($ii->employees->[3]->title, 'Developer', '... got the right title'); |
220 | is($ii->employees->[3]->company, $ii, '... got the right company'); |
51c5e0fb |
221 | ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref'); |
5569c072 |
222 | |
223 | isa_ok($ii->employees->[3]->address, 'Address'); |
224 | is($ii->employees->[3]->address->city, 'Marysville', '... got the right city'); |
225 | is($ii->employees->[3]->address->state, 'OH', '... got the right state'); |
226 | |
227 | ## check some error conditions for the subtypes |
228 | |
229 | dies_ok { |
51c5e0fb |
230 | Address->new(street => {}), |
231 | } '... we die correctly with bad args'; |
232 | |
233 | dies_ok { |
234 | Address->new(city => {}), |
235 | } '... we die correctly with bad args'; |
236 | |
237 | dies_ok { |
5569c072 |
238 | Address->new(state => 'British Columbia'), |
239 | } '... we die correctly with bad args'; |
240 | |
241 | lives_ok { |
242 | Address->new(state => 'Connecticut'), |
243 | } '... we live correctly with good args'; |
244 | |
245 | dies_ok { |
246 | Address->new(zip_code => 'AF5J6$'), |
247 | } '... we die correctly with bad args'; |
248 | |
249 | lives_ok { |
250 | Address->new(zip_code => '06443'), |
251 | } '... we live correctly with good args'; |
252 | |
253 | dies_ok { |
254 | Company->new(employees => [ Person->new ]), |
255 | } '... we die correctly with good args'; |
256 | |
257 | lives_ok { |
258 | Company->new(employees => []), |
259 | } '... we live correctly with good args'; |
260 | |