Commit | Line | Data |
1d768fb1 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
449559bf |
6 | use Test::More tests => 84; |
1d768fb1 |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | |
1d768fb1 |
10 | |
1d768fb1 |
11 | { |
83cc9094 |
12 | package Thing; |
13 | use Moose; |
d03bd989 |
14 | |
83cc9094 |
15 | sub hello { 'Hello World (from Thing)' } |
d03bd989 |
16 | sub goodbye { 'Goodbye World (from Thing)' } |
17 | |
1d768fb1 |
18 | package Foo; |
1d768fb1 |
19 | use Moose; |
ce0e8d63 |
20 | use Moose::Util::TypeConstraints; |
d03bd989 |
21 | |
22 | subtype 'FooStr' |
ce0e8d63 |
23 | => as 'Str' |
24 | => where { /Foo/ }; |
d03bd989 |
25 | |
26 | coerce 'FooStr' |
ce0e8d63 |
27 | => from ArrayRef |
28 | => via { 'FooArrayRef' }; |
d03bd989 |
29 | |
1d768fb1 |
30 | has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar'); |
d03bd989 |
31 | has 'baz' => (is => 'rw', isa => 'Ref'); |
32 | has 'foo' => (is => 'rw', isa => 'FooStr'); |
33 | |
34 | has 'gorch' => (is => 'ro'); |
35 | has 'gloum' => (is => 'ro', default => sub {[]}); |
36 | |
83cc9094 |
37 | has 'bling' => (is => 'ro', isa => 'Thing'); |
d03bd989 |
38 | has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']); |
39 | |
f3c4e20e |
40 | has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef'); |
238b424d |
41 | |
d03bd989 |
42 | has 'one_last_one' => (is => 'rw', isa => 'Ref'); |
43 | |
ce0e8d63 |
44 | # this one will work here .... |
ccd4cff9 |
45 | has 'fail' => (isa => 'CodeRef', is => 'bare'); |
46 | has 'other_fail' => (is => 'bare'); |
d03bd989 |
47 | |
1d768fb1 |
48 | package Bar; |
1d768fb1 |
49 | use Moose; |
238b424d |
50 | use Moose::Util::TypeConstraints; |
d03bd989 |
51 | |
1d768fb1 |
52 | extends 'Foo'; |
83cc9094 |
53 | |
d03bd989 |
54 | ::lives_ok { |
55 | has '+bar' => (default => 'Bar::bar'); |
56 | } '... we can change the default attribute option'; |
57 | |
58 | ::lives_ok { |
59 | has '+baz' => (isa => 'ArrayRef'); |
60 | } '... we can add change the isa as long as it is a subtype'; |
61 | |
62 | ::lives_ok { |
63 | has '+foo' => (coerce => 1); |
64 | } '... we can change/add coerce as an attribute option'; |
65 | |
66 | ::lives_ok { |
67 | has '+gorch' => (required => 1); |
68 | } '... we can change/add required as an attribute option'; |
69 | |
70 | ::lives_ok { |
71 | has '+gloum' => (lazy => 1); |
72 | } '... we can change/add lazy as an attribute option'; |
f3c4e20e |
73 | |
74 | ::lives_ok { |
449559bf |
75 | has '+gloum' => (lazy_build => 1); |
76 | } '... we can add lazy_build as an attribute option'; |
77 | |
78 | ::lives_ok { |
d03bd989 |
79 | has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]'); |
f3c4e20e |
80 | } '... extend an attribute with parameterized type'; |
d03bd989 |
81 | |
83cc9094 |
82 | ::lives_ok { |
d03bd989 |
83 | has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' })); |
84 | } '... extend an attribute with anon-subtype'; |
85 | |
5e98d2b6 |
86 | ::lives_ok { |
d03bd989 |
87 | has '+one_last_one' => (isa => 'Value'); |
88 | } '... now can extend an attribute with a non-subtype'; |
39d37838 |
89 | |
90 | ::lives_ok { |
d03bd989 |
91 | has '+bling' => (handles => ['hello']); |
83cc9094 |
92 | } '... we can add the handles attribute option'; |
d03bd989 |
93 | |
ce0e8d63 |
94 | # this one will *not* work here .... |
83cc9094 |
95 | ::dies_ok { |
d03bd989 |
96 | has '+blang' => (handles => ['hello']); |
97 | } '... we can not alter the handles attribute option'; |
98 | ::lives_ok { |
99 | has '+fail' => (isa => 'Ref'); |
100 | } '... can now create an attribute with an improper subtype relation'; |
101 | ::dies_ok { |
102 | has '+other_fail' => (trigger => sub {}); |
103 | } '... cannot create an attribute with an illegal option'; |
104 | ::dies_ok { |
105 | has '+other_fail' => (weak_ref => 1); |
106 | } '... cannot create an attribute with an illegal option'; |
329c5dd4 |
107 | ::throws_ok { |
108 | has '+does_not_exist' => (isa => 'Str'); |
109 | } qr/in Bar/, '... cannot extend a non-existing attribute'; |
1d768fb1 |
110 | } |
111 | |
112 | my $foo = Foo->new; |
113 | isa_ok($foo, 'Foo'); |
114 | |
ce0e8d63 |
115 | is($foo->foo, undef, '... got the right undef default value'); |
116 | lives_ok { $foo->foo('FooString') } '... assigned foo correctly'; |
117 | is($foo->foo, 'FooString', '... got the right value for foo'); |
1d768fb1 |
118 | |
ce0e8d63 |
119 | dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)'; |
120 | |
121 | is($foo->bar, 'Foo::bar', '... got the right default value'); |
1d768fb1 |
122 | dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr'; |
123 | |
ce0e8d63 |
124 | is($foo->baz, undef, '... got the right undef default value'); |
125 | |
126 | { |
127 | my $hash_ref = {}; |
128 | lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs'; |
129 | is($foo->baz, $hash_ref, '... got the right value assigned to baz'); |
d03bd989 |
130 | |
ce0e8d63 |
131 | my $array_ref = []; |
132 | lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref'; |
133 | is($foo->baz, $array_ref, '... got the right value assigned to baz'); |
134 | |
135 | my $scalar_ref = \(my $var); |
136 | lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref'; |
137 | is($foo->baz, $scalar_ref, '... got the right value assigned to baz'); |
d03bd989 |
138 | |
139 | lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings'; |
140 | |
141 | lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref'; |
142 | |
ce0e8d63 |
143 | my $code_ref = sub { 1 }; |
144 | lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref'; |
d03bd989 |
145 | is($foo->baz, $code_ref, '... got the right value assigned to baz'); |
ce0e8d63 |
146 | } |
147 | |
148 | dies_ok { |
149 | Bar->new; |
150 | } '... cannot create Bar without required gorch param'; |
151 | |
152 | my $bar = Bar->new(gorch => 'Bar::gorch'); |
1d768fb1 |
153 | isa_ok($bar, 'Bar'); |
154 | isa_ok($bar, 'Foo'); |
155 | |
ce0e8d63 |
156 | is($bar->foo, undef, '... got the right undef default value'); |
157 | lives_ok { $bar->foo('FooString') } '... assigned foo correctly'; |
158 | is($bar->foo, 'FooString', '... got the right value for foo'); |
159 | lives_ok { $bar->foo([]) } '... assigned foo correctly'; |
160 | is($bar->foo, 'FooArrayRef', '... got the right value for foo'); |
161 | |
162 | is($bar->gorch, 'Bar::gorch', '... got the right default value'); |
1d768fb1 |
163 | |
ce0e8d63 |
164 | is($bar->bar, 'Bar::bar', '... got the right default value'); |
1d768fb1 |
165 | dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr'; |
166 | |
ce0e8d63 |
167 | is($bar->baz, undef, '... got the right undef default value'); |
168 | |
169 | { |
170 | my $hash_ref = {}; |
171 | dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs'; |
d03bd989 |
172 | |
ce0e8d63 |
173 | my $array_ref = []; |
174 | lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref'; |
175 | is($bar->baz, $array_ref, '... got the right value assigned to baz'); |
176 | |
177 | my $scalar_ref = \(my $var); |
178 | dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref'; |
d03bd989 |
179 | |
180 | lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints'; |
181 | dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings'; |
182 | |
ce0e8d63 |
183 | my $code_ref = sub { 1 }; |
184 | dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref'; |
185 | } |
186 | |
1d768fb1 |
187 | # check some meta-stuff |
188 | |
ce0e8d63 |
189 | ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr'); |
1d768fb1 |
190 | ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr'); |
ce0e8d63 |
191 | ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr'); |
192 | ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr'); |
83cc9094 |
193 | ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr'); |
194 | ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr'); |
f3c4e20e |
195 | ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr'); |
83cc9094 |
196 | ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr'); |
5e98d2b6 |
197 | ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr'); |
198 | ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr'); |
ce0e8d63 |
199 | |
d03bd989 |
200 | isnt(Foo->meta->get_attribute('foo'), |
201 | Bar->meta->get_attribute('foo'), |
ce0e8d63 |
202 | '... Foo and Bar have different copies of foo'); |
d03bd989 |
203 | isnt(Foo->meta->get_attribute('bar'), |
204 | Bar->meta->get_attribute('bar'), |
1d768fb1 |
205 | '... Foo and Bar have different copies of bar'); |
d03bd989 |
206 | isnt(Foo->meta->get_attribute('baz'), |
207 | Bar->meta->get_attribute('baz'), |
208 | '... Foo and Bar have different copies of baz'); |
209 | isnt(Foo->meta->get_attribute('gorch'), |
210 | Bar->meta->get_attribute('gorch'), |
83cc9094 |
211 | '... Foo and Bar have different copies of gorch'); |
d03bd989 |
212 | isnt(Foo->meta->get_attribute('gloum'), |
213 | Bar->meta->get_attribute('gloum'), |
214 | '... Foo and Bar have different copies of gloum'); |
215 | isnt(Foo->meta->get_attribute('bling'), |
216 | Bar->meta->get_attribute('bling'), |
217 | '... Foo and Bar have different copies of bling'); |
218 | isnt(Foo->meta->get_attribute('bunch_of_stuff'), |
219 | Bar->meta->get_attribute('bunch_of_stuff'), |
220 | '... Foo and Bar have different copies of bunch_of_stuff'); |
221 | |
222 | ok(Bar->meta->get_attribute('bar')->has_type_constraint, |
daea75c9 |
223 | '... Bar::bar inherited the type constraint too'); |
d03bd989 |
224 | ok(Bar->meta->get_attribute('baz')->has_type_constraint, |
225 | '... Bar::baz inherited the type constraint too'); |
1d768fb1 |
226 | |
d03bd989 |
227 | is(Bar->meta->get_attribute('bar')->type_constraint->name, |
daea75c9 |
228 | 'Str', '... Bar::bar inherited the right type constraint too'); |
1d768fb1 |
229 | |
d03bd989 |
230 | is(Foo->meta->get_attribute('baz')->type_constraint->name, |
ce0e8d63 |
231 | 'Ref', '... Foo::baz inherited the right type constraint too'); |
d03bd989 |
232 | is(Bar->meta->get_attribute('baz')->type_constraint->name, |
233 | 'ArrayRef', '... Bar::baz inherited the right type constraint too'); |
234 | |
235 | ok(!Foo->meta->get_attribute('gorch')->is_required, |
ce0e8d63 |
236 | '... Foo::gorch is not a required attr'); |
d03bd989 |
237 | ok(Bar->meta->get_attribute('gorch')->is_required, |
ce0e8d63 |
238 | '... Bar::gorch is a required attr'); |
d03bd989 |
239 | |
240 | is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name, |
f3c4e20e |
241 | 'ArrayRef', |
242 | '... Foo::bunch_of_stuff is an ArrayRef'); |
d03bd989 |
243 | is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name, |
f3c4e20e |
244 | 'ArrayRef[Int]', |
245 | '... Bar::bunch_of_stuff is an ArrayRef[Int]'); |
d03bd989 |
246 | |
247 | ok(!Foo->meta->get_attribute('gloum')->is_lazy, |
83cc9094 |
248 | '... Foo::gloum is not a required attr'); |
d03bd989 |
249 | ok(Bar->meta->get_attribute('gloum')->is_lazy, |
250 | '... Bar::gloum is a required attr'); |
251 | |
252 | ok(!Foo->meta->get_attribute('foo')->should_coerce, |
ce0e8d63 |
253 | '... Foo::foo should not coerce'); |
d03bd989 |
254 | ok(Bar->meta->get_attribute('foo')->should_coerce, |
255 | '... Bar::foo should coerce'); |
256 | |
257 | ok(!Foo->meta->get_attribute('bling')->has_handles, |
83cc9094 |
258 | '... Foo::foo should not handles'); |
d03bd989 |
259 | ok(Bar->meta->get_attribute('bling')->has_handles, |
260 | '... Bar::foo should handles'); |
ce0e8d63 |
261 | |
1d768fb1 |
262 | |