6 use Test::More tests => 80;
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 # this one will work here ....
45 has 'fail' => (isa => 'CodeRef');
54 has '+bar' => (default => 'Bar::bar');
55 } '... we can change the default attribute option';
58 has '+baz' => (isa => 'ArrayRef');
59 } '... we can add change the isa as long as it is a subtype';
62 has '+foo' => (coerce => 1);
63 } '... we can change/add coerce as an attribute option';
66 has '+gorch' => (required => 1);
67 } '... we can change/add required as an attribute option';
70 has '+gloum' => (lazy => 1);
71 } '... we can change/add lazy as an attribute option';
74 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
75 } '... extend an attribute with parameterized type';
78 has '+bling' => (handles => ['hello']);
79 } '... we can add the handles attribute option';
81 # this one will *not* work here ....
83 has '+blang' => (handles => ['hello']);
84 } '... we can not alter the handles attribute option';
86 has '+fail' => (isa => 'Ref');
87 } '... cannot create an attribute with an improper subtype relation';
89 has '+other_fail' => (trigger => sub {});
90 } '... cannot create an attribute with an illegal option';
92 has '+other_fail' => (weak_ref => 1);
93 } '... cannot create an attribute with an illegal option';
100 is($foo->foo, undef, '... got the right undef default value');
101 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
102 is($foo->foo, 'FooString', '... got the right value for foo');
104 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
106 is($foo->bar, 'Foo::bar', '... got the right default value');
107 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
109 is($foo->baz, undef, '... got the right undef default value');
113 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
114 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
117 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
118 is($foo->baz, $array_ref, '... got the right value assigned to baz');
120 my $scalar_ref = \(my $var);
121 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
122 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
124 lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
126 my $code_ref = sub { 1 };
127 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
128 is($foo->baz, $code_ref, '... got the right value assigned to baz');
133 } '... cannot create Bar without required gorch param';
135 my $bar = Bar->new(gorch => 'Bar::gorch');
139 is($bar->foo, undef, '... got the right undef default value');
140 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
141 is($bar->foo, 'FooString', '... got the right value for foo');
142 lives_ok { $bar->foo([]) } '... assigned foo correctly';
143 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
145 is($bar->gorch, 'Bar::gorch', '... got the right default value');
147 is($bar->bar, 'Bar::bar', '... got the right default value');
148 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
150 is($bar->baz, undef, '... got the right undef default value');
154 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
157 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
158 is($bar->baz, $array_ref, '... got the right value assigned to baz');
160 my $scalar_ref = \(my $var);
161 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
163 lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints';
164 dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings';
166 my $code_ref = sub { 1 };
167 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
170 # check some meta-stuff
172 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
173 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
174 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
175 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
176 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
177 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
178 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
179 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
180 ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
181 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
183 isnt(Foo->meta->get_attribute('foo'),
184 Bar->meta->get_attribute('foo'),
185 '... Foo and Bar have different copies of foo');
186 isnt(Foo->meta->get_attribute('bar'),
187 Bar->meta->get_attribute('bar'),
188 '... Foo and Bar have different copies of bar');
189 isnt(Foo->meta->get_attribute('baz'),
190 Bar->meta->get_attribute('baz'),
191 '... Foo and Bar have different copies of baz');
192 isnt(Foo->meta->get_attribute('gorch'),
193 Bar->meta->get_attribute('gorch'),
194 '... Foo and Bar have different copies of gorch');
195 isnt(Foo->meta->get_attribute('gloum'),
196 Bar->meta->get_attribute('gloum'),
197 '... Foo and Bar have different copies of gloum');
198 isnt(Foo->meta->get_attribute('bling'),
199 Bar->meta->get_attribute('bling'),
200 '... Foo and Bar have different copies of bling');
201 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
202 Bar->meta->get_attribute('bunch_of_stuff'),
203 '... Foo and Bar have different copies of bunch_of_stuff');
205 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
206 '... Bar::bar inherited the type constraint too');
207 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
208 '... Bar::baz inherited the type constraint too');
210 is(Bar->meta->get_attribute('bar')->type_constraint->name,
211 'Str', '... Bar::bar inherited the right type constraint too');
213 is(Foo->meta->get_attribute('baz')->type_constraint->name,
214 'Ref', '... Foo::baz inherited the right type constraint too');
215 is(Bar->meta->get_attribute('baz')->type_constraint->name,
216 'ArrayRef', '... Bar::baz inherited the right type constraint too');
218 ok(!Foo->meta->get_attribute('gorch')->is_required,
219 '... Foo::gorch is not a required attr');
220 ok(Bar->meta->get_attribute('gorch')->is_required,
221 '... Bar::gorch is a required attr');
223 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
225 '... Foo::bunch_of_stuff is an ArrayRef');
226 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
228 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
230 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
231 '... Foo::gloum is not a required attr');
232 ok(Bar->meta->get_attribute('gloum')->is_lazy,
233 '... Bar::gloum is a required attr');
235 ok(!Foo->meta->get_attribute('foo')->should_coerce,
236 '... Foo::foo should not coerce');
237 ok(Bar->meta->get_attribute('foo')->should_coerce,
238 '... Bar::foo should coerce');
240 ok(!Foo->meta->get_attribute('bling')->has_handles,
241 '... Foo::foo should not handles');
242 ok(Bar->meta->get_attribute('bling')->has_handles,
243 '... Bar::foo should handles');