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