Make sure weak attributes remain weak when cloning (Moose 2.0007)
[gitmo/Mouse.git] / t / 020_attributes / 009_attribute_inherited_slot_specs.t
1 #!/usr/bin/perl
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;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 use Test::Exception;
11
12
13 {
14     package Thing::Meta::Attribute;
15     use Mouse;
16
17     extends 'Mouse::Meta::Attribute';
18     around illegal_options_for_inheritance => sub {
19         return (shift->(@_), qw/trigger/);
20     };
21
22     package Thing;
23     use Mouse;
24
25     sub hello   { 'Hello World (from Thing)' }
26     sub goodbye { 'Goodbye World (from Thing)' }
27
28     package Foo;
29     use Mouse;
30     use Mouse::Util::TypeConstraints;
31
32     subtype 'FooStr'
33         => as 'Str'
34         => where { /Foo/ };
35
36     coerce 'FooStr'
37         => from ArrayRef
38             => via { 'FooArrayRef' };
39
40     has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
41     has 'baz' => (is => 'rw', isa => 'Ref');
42     has 'foo' => (is => 'rw', isa => 'FooStr');
43
44     has 'gorch' => (is => 'ro');
45     has 'gloum' => (is => 'ro', default => sub {[]});
46     has 'fleem' => (is => 'ro');
47
48     has 'bling' => (is => 'ro', isa => 'Thing');
49     has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
50
51     has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
52
53     has 'one_last_one' => (is => 'rw', isa => 'Ref');
54
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 { });
58
59     package Bar;
60     use Mouse;
61     use Mouse::Util::TypeConstraints;
62
63     extends 'Foo';
64
65     ::lives_ok {
66         has '+bar' => (default => 'Bar::bar');
67     } '... we can change the default attribute option';
68
69     ::lives_ok {
70         has '+baz' => (isa => 'ArrayRef');
71     } '... we can add change the isa as long as it is a subtype';
72
73     ::lives_ok {
74         has '+foo' => (coerce => 1);
75     } '... we can change/add coerce as an attribute option';
76
77     ::lives_ok {
78         has '+gorch' => (required => 1);
79     } '... we can change/add required as an attribute option';
80
81     ::lives_ok {
82         has '+gloum' => (lazy => 1);
83     } '... we can change/add lazy as an attribute option';
84
85     ::lives_ok {
86         has '+gloum' => (lazy_build => 1);
87     } '... we can add lazy_build as an attribute option';
88
89     ::lives_ok {
90         has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
91     } '... extend an attribute with parameterized type';
92
93     ::lives_ok {
94         has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
95     } '... extend an attribute with anon-subtype';
96
97     ::lives_ok {
98         has '+one_last_one' => (isa => 'Value');
99     } '... now can extend an attribute with a non-subtype';
100
101     ::lives_ok {
102         has '+fleem' => (weak_ref => 1);
103     } '... now allowed to add the weak_ref option via inheritance';
104
105     ::lives_ok {
106         has '+bling' => (handles => ['hello']);
107     } '... we can add the handles attribute option';
108
109     # this one will *not* work here ....
110     ::dies_ok {
111         has '+blang' => (handles => ['hello']);
112     } '... we can not alter the handles attribute option';
113     ::lives_ok {
114         has '+fail' => (isa => 'Ref');
115     } '... can now create an attribute with an improper subtype relation';
116     ::dies_ok {
117         has '+other_fail' => (trigger => sub {});
118     } '... cannot create an attribute with an illegal option';
119     ::throws_ok {
120         has '+does_not_exist' => (isa => 'Str');
121     } qr/in Bar/, '... cannot extend a non-existing attribute';
122 }
123
124 my $foo = Foo->new;
125 isa_ok($foo, 'Foo');
126
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');
130
131 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
132
133 is($foo->bar, 'Foo::bar', '... got the right default value');
134 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
135
136 is($foo->baz, undef, '... got the right undef default value');
137
138 {
139     my $hash_ref = {};
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');
142
143     my $array_ref = [];
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');
146
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');
150
151     lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
152
153     lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref';
154
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');
158 }
159
160 dies_ok {
161     Bar->new;
162 } '... cannot create Bar without required gorch param';
163
164 my $bar = Bar->new(gorch => 'Bar::gorch');
165 isa_ok($bar, 'Bar');
166 isa_ok($bar, 'Foo');
167
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');
173
174 is($bar->gorch, 'Bar::gorch', '... got the right default value');
175
176 is($bar->bar, 'Bar::bar', '... got the right default value');
177 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
178
179 is($bar->baz, undef, '... got the right undef default value');
180
181 {
182     my $hash_ref = {};
183     dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
184
185     my $array_ref = [];
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');
188
189     my $scalar_ref = \(my $var);
190     dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
191
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';
194
195     my $code_ref = sub { 1 };
196     dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
197 }
198
199 # check some meta-stuff
200
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');
211
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');
233
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');
238
239 is(Bar->meta->get_attribute('bar')->type_constraint->name,
240    'Str', '... Bar::bar inherited the right type constraint too');
241
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');
246
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');
251
252 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
253   'ArrayRef',
254   '... Foo::bunch_of_stuff is an ArrayRef');
255 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
256   'ArrayRef[Int]',
257   '... Bar::bunch_of_stuff is an ArrayRef[Int]');
258
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');
263
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');
268
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');
273
274 done_testing;