2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
14 package Thing::Meta::Attribute;
17 extends 'Mouse::Meta::Attribute';
18 around illegal_options_for_inheritance => sub {
19 return (shift->(@_), qw/trigger/);
25 sub hello { 'Hello World (from Thing)' }
26 sub goodbye { 'Goodbye World (from Thing)' }
30 use Mouse::Util::TypeConstraints;
38 => via { 'FooArrayRef' };
40 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
41 has 'baz' => (is => 'rw', isa => 'Ref');
42 has 'foo' => (is => 'rw', isa => 'FooStr');
44 has 'gorch' => (is => 'ro');
45 has 'gloum' => (is => 'ro', default => sub {[]});
46 has 'fleem' => (is => 'ro');
48 has 'bling' => (is => 'ro', isa => 'Thing');
49 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
51 has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
53 has 'one_last_one' => (is => 'rw', isa => 'Ref');
55 # this one will work here ....
56 has 'fail' => (isa => 'CodeRef', is => 'bare');
57 has 'other_fail' => (metaclass => 'Thing::Meta::Attribute', is => 'bare', trigger => sub { });
61 use Mouse::Util::TypeConstraints;
66 has '+bar' => (default => 'Bar::bar');
67 } '... we can change the default attribute option';
70 has '+baz' => (isa => 'ArrayRef');
71 } '... we can add change the isa as long as it is a subtype';
74 has '+foo' => (coerce => 1);
75 } '... we can change/add coerce as an attribute option';
78 has '+gorch' => (required => 1);
79 } '... we can change/add required as an attribute option';
82 has '+gloum' => (lazy => 1);
83 } '... we can change/add lazy as an attribute option';
86 has '+gloum' => (lazy_build => 1);
87 } '... we can add lazy_build as an attribute option';
90 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
91 } '... extend an attribute with parameterized type';
94 has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
95 } '... extend an attribute with anon-subtype';
98 has '+one_last_one' => (isa => 'Value');
99 } '... now can extend an attribute with a non-subtype';
102 has '+fleem' => (weak_ref => 1);
103 } '... now allowed to add the weak_ref option via inheritance';
106 has '+bling' => (handles => ['hello']);
107 } '... we can add the handles attribute option';
109 # this one will *not* work here ....
111 has '+blang' => (handles => ['hello']);
112 } '... we can not alter the handles attribute option';
114 has '+fail' => (isa => 'Ref');
115 } '... can now create an attribute with an improper subtype relation';
117 has '+other_fail' => (trigger => sub {});
118 } '... cannot create an attribute with an illegal option';
120 has '+does_not_exist' => (isa => 'Str');
121 } qr/in Bar/, '... cannot extend a non-existing attribute';
127 is($foo->foo, undef, '... got the right undef default value');
128 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
129 is($foo->foo, 'FooString', '... got the right value for foo');
131 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
133 is($foo->bar, 'Foo::bar', '... got the right default value');
134 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
136 is($foo->baz, undef, '... got the right undef default value');
140 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
141 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
144 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
145 is($foo->baz, $array_ref, '... got the right value assigned to baz');
147 my $scalar_ref = \(my $var);
148 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
149 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
151 lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
153 lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref';
155 my $code_ref = sub { 1 };
156 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
157 is($foo->baz, $code_ref, '... got the right value assigned to baz');
162 } '... cannot create Bar without required gorch param';
164 my $bar = Bar->new(gorch => 'Bar::gorch');
168 is($bar->foo, undef, '... got the right undef default value');
169 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
170 is($bar->foo, 'FooString', '... got the right value for foo');
171 lives_ok { $bar->foo([]) } '... assigned foo correctly';
172 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
174 is($bar->gorch, 'Bar::gorch', '... got the right default value');
176 is($bar->bar, 'Bar::bar', '... got the right default value');
177 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
179 is($bar->baz, undef, '... got the right undef default value');
183 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
186 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
187 is($bar->baz, $array_ref, '... got the right value assigned to baz');
189 my $scalar_ref = \(my $var);
190 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
192 lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints';
193 dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings';
195 my $code_ref = sub { 1 };
196 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
199 # check some meta-stuff
201 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
202 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
203 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
204 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
205 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
206 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
207 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
208 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
209 ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr');
210 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr');
212 isnt(Foo->meta->get_attribute('foo'),
213 Bar->meta->get_attribute('foo'),
214 '... Foo and Bar have different copies of foo');
215 isnt(Foo->meta->get_attribute('bar'),
216 Bar->meta->get_attribute('bar'),
217 '... Foo and Bar have different copies of bar');
218 isnt(Foo->meta->get_attribute('baz'),
219 Bar->meta->get_attribute('baz'),
220 '... Foo and Bar have different copies of baz');
221 isnt(Foo->meta->get_attribute('gorch'),
222 Bar->meta->get_attribute('gorch'),
223 '... Foo and Bar have different copies of gorch');
224 isnt(Foo->meta->get_attribute('gloum'),
225 Bar->meta->get_attribute('gloum'),
226 '... Foo and Bar have different copies of gloum');
227 isnt(Foo->meta->get_attribute('bling'),
228 Bar->meta->get_attribute('bling'),
229 '... Foo and Bar have different copies of bling');
230 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
231 Bar->meta->get_attribute('bunch_of_stuff'),
232 '... Foo and Bar have different copies of bunch_of_stuff');
234 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
235 '... Bar::bar inherited the type constraint too');
236 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
237 '... Bar::baz inherited the type constraint too');
239 is(Bar->meta->get_attribute('bar')->type_constraint->name,
240 'Str', '... Bar::bar inherited the right type constraint too');
242 is(Foo->meta->get_attribute('baz')->type_constraint->name,
243 'Ref', '... Foo::baz inherited the right type constraint too');
244 is(Bar->meta->get_attribute('baz')->type_constraint->name,
245 'ArrayRef', '... Bar::baz inherited the right type constraint too');
247 ok(!Foo->meta->get_attribute('gorch')->is_required,
248 '... Foo::gorch is not a required attr');
249 ok(Bar->meta->get_attribute('gorch')->is_required,
250 '... Bar::gorch is a required attr');
252 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
254 '... Foo::bunch_of_stuff is an ArrayRef');
255 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
257 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
259 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
260 '... Foo::gloum is not a required attr');
261 ok(Bar->meta->get_attribute('gloum')->is_lazy,
262 '... Bar::gloum is a required attr');
264 ok(!Foo->meta->get_attribute('foo')->should_coerce,
265 '... Foo::foo should not coerce');
266 ok(Bar->meta->get_attribute('foo')->should_coerce,
267 '... Bar::foo should coerce');
269 ok(!Foo->meta->get_attribute('bling')->has_handles,
270 '... Foo::foo should not handles');
271 ok(Bar->meta->get_attribute('bling')->has_handles,
272 '... Bar::foo should handles');