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