inner-augment-super-override
[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);
b6fe348f 97
98 override 'full_name' => sub {
99 my $self = shift;
100 super() . ', ' . $self->title
101 };
5569c072 102}
103
104my $ii;
105lives_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';
143isa_ok($ii, 'Company');
144
145is($ii->name, 'Infinity Interactive', '... got the right name for the company');
146
147isa_ok($ii->address, 'Address');
148is($ii->address->street, '565 Plandome Rd., Suite 307', '... got the right street address');
149is($ii->address->city, 'Manhasset', '... got the right city');
150is($ii->address->state, 'NY', '... got the right state');
151is($ii->address->zip_code, 11030, '... got the zip code');
152
153is($ii->get_employee_count, 4, '... got the right employee count');
154
155# employee #1
156
157isa_ok($ii->employees->[0], 'Employee');
158isa_ok($ii->employees->[0], 'Person');
159
160is($ii->employees->[0]->first_name, 'Jeremy', '... got the right first name');
161is($ii->employees->[0]->last_name, 'Shao', '... got the right last name');
162ok(!$ii->employees->[0]->has_middle_initial, '... no middle initial');
163is($ii->employees->[0]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 164is($ii->employees->[0]->full_name, 'Jeremy Shao, President / Senior Consultant', '... got the right full name');
5569c072 165is($ii->employees->[0]->title, 'President / Senior Consultant', '... got the right title');
166is($ii->employees->[0]->company, $ii, '... got the right company');
51c5e0fb 167ok(isweak($ii->employees->[0]->{company}), '... the company is a weak-ref');
5569c072 168
169isa_ok($ii->employees->[0]->address, 'Address');
170is($ii->employees->[0]->address->city, 'Manhasset', '... got the right city');
171is($ii->employees->[0]->address->state, 'NY', '... got the right state');
172
173# employee #2
174
175isa_ok($ii->employees->[1], 'Employee');
176isa_ok($ii->employees->[1], 'Person');
177
178is($ii->employees->[1]->first_name, 'Tommy', '... got the right first name');
179is($ii->employees->[1]->last_name, 'Lee', '... got the right last name');
180ok(!$ii->employees->[1]->has_middle_initial, '... no middle initial');
181is($ii->employees->[1]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 182is($ii->employees->[1]->full_name, 'Tommy Lee, Vice President / Senior Developer', '... got the right full name');
5569c072 183is($ii->employees->[1]->title, 'Vice President / Senior Developer', '... got the right title');
184is($ii->employees->[1]->company, $ii, '... got the right company');
51c5e0fb 185ok(isweak($ii->employees->[1]->{company}), '... the company is a weak-ref');
5569c072 186
187isa_ok($ii->employees->[1]->address, 'Address');
188is($ii->employees->[1]->address->city, 'New York', '... got the right city');
189is($ii->employees->[1]->address->state, 'NY', '... got the right state');
190
191# employee #3
192
193isa_ok($ii->employees->[2], 'Employee');
194isa_ok($ii->employees->[2], 'Person');
195
196is($ii->employees->[2]->first_name, 'Stevan', '... got the right first name');
197is($ii->employees->[2]->last_name, 'Little', '... got the right last name');
198ok($ii->employees->[2]->has_middle_initial, '... got middle initial');
199is($ii->employees->[2]->middle_initial, 'C', '... got the right middle initial value');
b6fe348f 200is($ii->employees->[2]->full_name, 'Stevan C. Little, Senior Developer', '... got the right full name');
5569c072 201is($ii->employees->[2]->title, 'Senior Developer', '... got the right title');
202is($ii->employees->[2]->company, $ii, '... got the right company');
51c5e0fb 203ok(isweak($ii->employees->[2]->{company}), '... the company is a weak-ref');
5569c072 204
205isa_ok($ii->employees->[2]->address, 'Address');
206is($ii->employees->[2]->address->city, 'Madison', '... got the right city');
207is($ii->employees->[2]->address->state, 'CT', '... got the right state');
208
209# employee #4
210
211isa_ok($ii->employees->[3], 'Employee');
212isa_ok($ii->employees->[3], 'Person');
213
214is($ii->employees->[3]->first_name, 'Rob', '... got the right first name');
215is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name');
216ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial');
217is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value');
b6fe348f 218is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name');
5569c072 219is($ii->employees->[3]->title, 'Developer', '... got the right title');
220is($ii->employees->[3]->company, $ii, '... got the right company');
51c5e0fb 221ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref');
5569c072 222
223isa_ok($ii->employees->[3]->address, 'Address');
224is($ii->employees->[3]->address->city, 'Marysville', '... got the right city');
225is($ii->employees->[3]->address->state, 'OH', '... got the right state');
226
227## check some error conditions for the subtypes
228
229dies_ok {
51c5e0fb 230 Address->new(street => {}),
231} '... we die correctly with bad args';
232
233dies_ok {
234 Address->new(city => {}),
235} '... we die correctly with bad args';
236
237dies_ok {
5569c072 238 Address->new(state => 'British Columbia'),
239} '... we die correctly with bad args';
240
241lives_ok {
242 Address->new(state => 'Connecticut'),
243} '... we live correctly with good args';
244
245dies_ok {
246 Address->new(zip_code => 'AF5J6$'),
247} '... we die correctly with bad args';
248
249lives_ok {
250 Address->new(zip_code => '06443'),
251} '... we live correctly with good args';
252
253dies_ok {
254 Company->new(employees => [ Person->new ]),
255} '... we die correctly with good args';
256
257lives_ok {
258 Company->new(employees => []),
259} '... we live correctly with good args';
260