6 use Test::More tests => 84;
17 sub hello { 'Hello World (from Thing)' }
18 sub goodbye { 'Goodbye World (from Thing)' }
22 use Moose::Util::TypeConstraints;
30 => via { 'FooArrayRef' };
32 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
33 has 'baz' => (is => 'rw', isa => 'Ref');
34 has 'foo' => (is => 'rw', isa => 'FooStr');
36 has 'gorch' => (is => 'ro');
37 has 'gloum' => (is => 'ro', default => sub {[]});
39 has 'bling' => (is => 'ro', isa => 'Thing');
40 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
42 has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
44 has 'one_last_one' => (is => 'rw', isa => 'Ref');
46 # this one will work here ....
47 has 'fail' => (isa => 'CodeRef');
52 use Moose::Util::TypeConstraints;
57 has '+bar' => (default => 'Bar::bar');
58 } '... we can change the default attribute option';
61 has '+baz' => (isa => 'ArrayRef');
62 } '... we can add change the isa as long as it is a subtype';
65 has '+foo' => (coerce => 1);
66 } '... we can change/add coerce as an attribute option';
69 has '+gorch' => (required => 1);
70 } '... we can change/add required as an attribute option';
73 has '+gloum' => (lazy => 1);
74 } '... we can change/add lazy as an attribute option';
77 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
78 } '... extend an attribute with parameterized type';
81 has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
82 } '... extend an attribute with anon-subtype';
85 has '+one_last_one' => (isa => 'Value');
86 } '... cannot extend an attribute with a non-subtype';
89 has '+bling' => (handles => ['hello']);
90 } '... we can add the handles attribute option';
92 # this one will *not* work here ....
94 has '+blang' => (handles => ['hello']);
95 } '... we can not alter the handles attribute option';
97 has '+fail' => (isa => 'Ref');
98 } '... cannot create an attribute with an improper subtype relation';
100 has '+other_fail' => (trigger => sub {});
101 } '... cannot create an attribute with an illegal option';
103 has '+other_fail' => (weak_ref => 1);
104 } '... cannot create an attribute with an illegal option';
106 has '+other_fail' => (isa => 'WangDoodle');
107 } '... cannot create an attribute with a non-existent type';
114 is($foo->foo, undef, '... got the right undef default value');
115 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
116 is($foo->foo, 'FooString', '... got the right value for foo');
118 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
120 is($foo->bar, 'Foo::bar', '... got the right default value');
121 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
123 is($foo->baz, undef, '... got the right undef default value');
127 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
128 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
131 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
132 is($foo->baz, $array_ref, '... got the right value assigned to baz');
134 my $scalar_ref = \(my $var);
135 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
136 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
138 lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
140 lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref';
142 my $code_ref = sub { 1 };
143 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
144 is($foo->baz, $code_ref, '... got the right value assigned to baz');
149 } '... cannot create Bar without required gorch param';
151 my $bar = Bar->new(gorch => 'Bar::gorch');
155 is($bar->foo, undef, '... got the right undef default value');
156 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
157 is($bar->foo, 'FooString', '... got the right value for foo');
158 lives_ok { $bar->foo([]) } '... assigned foo correctly';
159 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
161 is($bar->gorch, 'Bar::gorch', '... got the right default value');
163 is($bar->bar, 'Bar::bar', '... got the right default value');
164 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
166 is($bar->baz, undef, '... got the right undef default value');
170 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
173 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
174 is($bar->baz, $array_ref, '... got the right value assigned to baz');
176 my $scalar_ref = \(my $var);
177 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
179 lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints';
180 dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings';
182 my $code_ref = sub { 1 };
183 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
186 # check some meta-stuff
188 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
189 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
190 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
191 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
192 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
193 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
194 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
195 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
196 ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
197 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
199 isnt(Foo->meta->get_attribute('foo'),
200 Bar->meta->get_attribute('foo'),
201 '... Foo and Bar have different copies of foo');
202 isnt(Foo->meta->get_attribute('bar'),
203 Bar->meta->get_attribute('bar'),
204 '... Foo and Bar have different copies of bar');
205 isnt(Foo->meta->get_attribute('baz'),
206 Bar->meta->get_attribute('baz'),
207 '... Foo and Bar have different copies of baz');
208 isnt(Foo->meta->get_attribute('gorch'),
209 Bar->meta->get_attribute('gorch'),
210 '... Foo and Bar have different copies of gorch');
211 isnt(Foo->meta->get_attribute('gloum'),
212 Bar->meta->get_attribute('gloum'),
213 '... Foo and Bar have different copies of gloum');
214 isnt(Foo->meta->get_attribute('bling'),
215 Bar->meta->get_attribute('bling'),
216 '... Foo and Bar have different copies of bling');
217 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
218 Bar->meta->get_attribute('bunch_of_stuff'),
219 '... Foo and Bar have different copies of bunch_of_stuff');
221 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
222 '... Bar::bar inherited the type constraint too');
223 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
224 '... Bar::baz inherited the type constraint too');
226 is(Bar->meta->get_attribute('bar')->type_constraint->name,
227 'Str', '... Bar::bar inherited the right type constraint too');
229 is(Foo->meta->get_attribute('baz')->type_constraint->name,
230 'Ref', '... Foo::baz inherited the right type constraint too');
231 is(Bar->meta->get_attribute('baz')->type_constraint->name,
232 'ArrayRef', '... Bar::baz inherited the right type constraint too');
234 ok(!Foo->meta->get_attribute('gorch')->is_required,
235 '... Foo::gorch is not a required attr');
236 ok(Bar->meta->get_attribute('gorch')->is_required,
237 '... Bar::gorch is a required attr');
239 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
241 '... Foo::bunch_of_stuff is an ArrayRef');
242 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
244 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
246 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
247 '... Foo::gloum is not a required attr');
248 ok(Bar->meta->get_attribute('gloum')->is_lazy,
249 '... Bar::gloum is a required attr');
251 ok(!Foo->meta->get_attribute('foo')->should_coerce,
252 '... Foo::foo should not coerce');
253 ok(Bar->meta->get_attribute('foo')->should_coerce,
254 '... Bar::foo should coerce');
256 ok(!Foo->meta->get_attribute('bling')->has_handles,
257 '... Foo::foo should not handles');
258 ok(Bar->meta->get_attribute('bling')->has_handles,
259 '... Bar::foo should handles');