14 sub hello { 'Hello World (from Thing)' }
15 sub goodbye { 'Goodbye World (from Thing)' }
19 use Moose::Util::TypeConstraints;
27 => via { 'FooArrayRef' };
29 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
30 has 'baz' => (is => 'rw', isa => 'Ref');
31 has 'foo' => (is => 'rw', isa => 'FooStr');
33 has 'gorch' => (is => 'ro');
34 has 'gloum' => (is => 'ro', default => sub {[]});
35 has 'fleem' => (is => 'ro');
37 has 'bling' => (is => 'ro', isa => 'Thing');
38 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
40 has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
42 has 'one_last_one' => (is => 'rw', isa => 'Ref');
44 # this one will work here ....
45 has 'fail' => (isa => 'CodeRef', is => 'bare');
46 has 'other_fail' => (is => 'bare');
50 use Moose::Util::TypeConstraints;
55 has '+bar' => (default => 'Bar::bar');
56 } '... we can change the default attribute option';
59 has '+baz' => (isa => 'ArrayRef');
60 } '... we can add change the isa as long as it is a subtype';
63 has '+foo' => (coerce => 1);
64 } '... we can change/add coerce as an attribute option';
67 has '+gorch' => (required => 1);
68 } '... we can change/add required as an attribute option';
71 has '+gloum' => (lazy => 1);
72 } '... we can change/add lazy as an attribute option';
75 has '+gloum' => (lazy_build => 1);
76 } '... we can add lazy_build as an attribute option';
79 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
80 } '... extend an attribute with parameterized type';
83 has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
84 } '... extend an attribute with anon-subtype';
87 has '+one_last_one' => (isa => 'Value');
88 } '... now can extend an attribute with a non-subtype';
91 has '+fleem' => (weak_ref => 1);
92 } '... now allowed to add the weak_ref option via inheritance';
95 has '+bling' => (handles => ['hello']);
96 } '... we can add the handles attribute option';
98 # this one will *not* work here ....
100 has '+blang' => (handles => ['hello']);
101 } '... we can not alter the handles attribute option';
103 has '+fail' => (isa => 'Ref');
104 } '... can now create an attribute with an improper subtype relation';
106 has '+other_fail' => (trigger => sub {});
107 } '... cannot create an attribute with an illegal option';
109 has '+does_not_exist' => (isa => 'Str');
110 } qr/in Bar/, '... cannot extend a non-existing attribute';
116 is($foo->foo, undef, '... got the right undef default value');
117 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
118 is($foo->foo, 'FooString', '... got the right value for foo');
120 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
122 is($foo->bar, 'Foo::bar', '... got the right default value');
123 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
125 is($foo->baz, undef, '... got the right undef default value');
129 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
130 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
133 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
134 is($foo->baz, $array_ref, '... got the right value assigned to baz');
136 my $scalar_ref = \(my $var);
137 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
138 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
140 lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
142 lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref';
144 my $code_ref = sub { 1 };
145 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
146 is($foo->baz, $code_ref, '... got the right value assigned to baz');
151 } '... cannot create Bar without required gorch param';
153 my $bar = Bar->new(gorch => 'Bar::gorch');
157 is($bar->foo, undef, '... got the right undef default value');
158 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
159 is($bar->foo, 'FooString', '... got the right value for foo');
160 lives_ok { $bar->foo([]) } '... assigned foo correctly';
161 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
163 is($bar->gorch, 'Bar::gorch', '... got the right default value');
165 is($bar->bar, 'Bar::bar', '... got the right default value');
166 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
168 is($bar->baz, undef, '... got the right undef default value');
172 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
175 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
176 is($bar->baz, $array_ref, '... got the right value assigned to baz');
178 my $scalar_ref = \(my $var);
179 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
181 lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints';
182 dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings';
184 my $code_ref = sub { 1 };
185 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
188 # check some meta-stuff
190 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
191 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
192 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
193 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
194 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
195 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
196 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
197 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
198 ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr');
199 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr');
201 isnt(Foo->meta->get_attribute('foo'),
202 Bar->meta->get_attribute('foo'),
203 '... Foo and Bar have different copies of foo');
204 isnt(Foo->meta->get_attribute('bar'),
205 Bar->meta->get_attribute('bar'),
206 '... Foo and Bar have different copies of bar');
207 isnt(Foo->meta->get_attribute('baz'),
208 Bar->meta->get_attribute('baz'),
209 '... Foo and Bar have different copies of baz');
210 isnt(Foo->meta->get_attribute('gorch'),
211 Bar->meta->get_attribute('gorch'),
212 '... Foo and Bar have different copies of gorch');
213 isnt(Foo->meta->get_attribute('gloum'),
214 Bar->meta->get_attribute('gloum'),
215 '... Foo and Bar have different copies of gloum');
216 isnt(Foo->meta->get_attribute('bling'),
217 Bar->meta->get_attribute('bling'),
218 '... Foo and Bar have different copies of bling');
219 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
220 Bar->meta->get_attribute('bunch_of_stuff'),
221 '... Foo and Bar have different copies of bunch_of_stuff');
223 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
224 '... Bar::bar inherited the type constraint too');
225 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
226 '... Bar::baz inherited the type constraint too');
228 is(Bar->meta->get_attribute('bar')->type_constraint->name,
229 'Str', '... Bar::bar inherited the right type constraint too');
231 is(Foo->meta->get_attribute('baz')->type_constraint->name,
232 'Ref', '... Foo::baz inherited the right type constraint too');
233 is(Bar->meta->get_attribute('baz')->type_constraint->name,
234 'ArrayRef', '... Bar::baz inherited the right type constraint too');
236 ok(!Foo->meta->get_attribute('gorch')->is_required,
237 '... Foo::gorch is not a required attr');
238 ok(Bar->meta->get_attribute('gorch')->is_required,
239 '... Bar::gorch is a required attr');
241 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
243 '... Foo::bunch_of_stuff is an ArrayRef');
244 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
246 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
248 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
249 '... Foo::gloum is not a required attr');
250 ok(Bar->meta->get_attribute('gloum')->is_lazy,
251 '... Bar::gloum is a required attr');
253 ok(!Foo->meta->get_attribute('foo')->should_coerce,
254 '... Foo::foo should not coerce');
255 ok(Bar->meta->get_attribute('foo')->should_coerce,
256 '... Bar::foo should coerce');
258 ok(!Foo->meta->get_attribute('bling')->has_handles,
259 '... Foo::foo should not handles');
260 ok(Bar->meta->get_attribute('bling')->has_handles,
261 '... Bar::foo should handles');