Revert autogenerated tests. Tests should not changed radically.
[gitmo/Mouse.git] / t / 020_attributes / 009_attribute_inherited_slot_specs.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9
10 {
11     package Thing::Meta::Attribute;
12     use Mouse;
13
14     extends 'Mouse::Meta::Attribute';
15     around illegal_options_for_inheritance => sub {
16         return (shift->(@_), qw/trigger/);
17     };
18
19     package Thing;
20     use Mouse;
21
22     sub hello   { 'Hello World (from Thing)' }
23     sub goodbye { 'Goodbye World (from Thing)' }
24
25     package Foo;
26     use Mouse;
27     use Mouse::Util::TypeConstraints;
28
29     subtype 'FooStr'
30         => as 'Str'
31         => where { /Foo/ };
32
33     coerce 'FooStr'
34         => from ArrayRef
35             => via { 'FooArrayRef' };
36
37     has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
38     has 'baz' => (is => 'rw', isa => 'Ref');
39     has 'foo' => (is => 'rw', isa => 'FooStr');
40
41     has 'gorch' => (is => 'ro');
42     has 'gloum' => (is => 'ro', default => sub {[]});
43     has 'fleem' => (is => 'ro');
44
45     has 'bling' => (is => 'ro', isa => 'Thing');
46     has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
47
48     has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
49
50     has 'one_last_one' => (is => 'rw', isa => 'Ref');
51
52     # this one will work here ....
53     has 'fail' => (isa => 'CodeRef', is => 'bare');
54     has 'other_fail' => (metaclass => 'Thing::Meta::Attribute', is => 'bare', trigger => sub { });
55
56     package Bar;
57     use Mouse;
58     use Mouse::Util::TypeConstraints;
59
60     extends 'Foo';
61
62     ::lives_ok {
63         has '+bar' => (default => 'Bar::bar');
64     } '... we can change the default attribute option';
65
66     ::lives_ok {
67         has '+baz' => (isa => 'ArrayRef');
68     } '... we can add change the isa as long as it is a subtype';
69
70     ::lives_ok {
71         has '+foo' => (coerce => 1);
72     } '... we can change/add coerce as an attribute option';
73
74     ::lives_ok {
75         has '+gorch' => (required => 1);
76     } '... we can change/add required as an attribute option';
77
78     ::lives_ok {
79         has '+gloum' => (lazy => 1);
80     } '... we can change/add lazy as an attribute option';
81
82     ::lives_ok {
83         has '+gloum' => (lazy_build => 1);
84     } '... we can add lazy_build as an attribute option';
85
86     ::lives_ok {
87         has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
88     } '... extend an attribute with parameterized type';
89
90     ::lives_ok {
91         has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
92     } '... extend an attribute with anon-subtype';
93
94     ::lives_ok {
95         has '+one_last_one' => (isa => 'Value');
96     } '... now can extend an attribute with a non-subtype';
97
98     ::lives_ok {
99         has '+fleem' => (weak_ref => 1);
100     } '... now allowed to add the weak_ref option via inheritance';
101
102     ::lives_ok {
103         has '+bling' => (handles => ['hello']);
104     } '... we can add the handles attribute option';
105
106     # this one will *not* work here ....
107     ::dies_ok {
108         has '+blang' => (handles => ['hello']);
109     } '... we can not alter the handles attribute option';
110     ::lives_ok {
111         has '+fail' => (isa => 'Ref');
112     } '... can now create an attribute with an improper subtype relation';
113     ::dies_ok {
114         has '+other_fail' => (trigger => sub {});
115     } '... cannot create an attribute with an illegal option';
116     ::throws_ok {
117         has '+does_not_exist' => (isa => 'Str');
118     } qr/in Bar/, '... cannot extend a non-existing attribute';
119 }
120
121 my $foo = Foo->new;
122 isa_ok($foo, 'Foo');
123
124 is($foo->foo, undef, '... got the right undef default value');
125 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
126 is($foo->foo, 'FooString', '... got the right value for foo');
127
128 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
129
130 is($foo->bar, 'Foo::bar', '... got the right default value');
131 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
132
133 is($foo->baz, undef, '... got the right undef default value');
134
135 {
136     my $hash_ref = {};
137     lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
138     is($foo->baz, $hash_ref, '... got the right value assigned to baz');
139
140     my $array_ref = [];
141     lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
142     is($foo->baz, $array_ref, '... got the right value assigned to baz');
143
144     my $scalar_ref = \(my $var);
145     lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
146     is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
147
148     lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
149
150     lives_ok { $foo->one_last_one(sub { 'Hello World'}) } '... Foo::one_last_one accepts a code ref';
151
152     my $code_ref = sub { 1 };
153     lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
154     is($foo->baz, $code_ref, '... got the right value assigned to baz');
155 }
156
157 dies_ok {
158     Bar->new;
159 } '... cannot create Bar without required gorch param';
160
161 my $bar = Bar->new(gorch => 'Bar::gorch');
162 isa_ok($bar, 'Bar');
163 isa_ok($bar, 'Foo');
164
165 is($bar->foo, undef, '... got the right undef default value');
166 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
167 is($bar->foo, 'FooString', '... got the right value for foo');
168 lives_ok { $bar->foo([]) } '... assigned foo correctly';
169 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
170
171 is($bar->gorch, 'Bar::gorch', '... got the right default value');
172
173 is($bar->bar, 'Bar::bar', '... got the right default value');
174 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
175
176 is($bar->baz, undef, '... got the right undef default value');
177
178 {
179     my $hash_ref = {};
180     dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
181
182     my $array_ref = [];
183     lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
184     is($bar->baz, $array_ref, '... got the right value assigned to baz');
185
186     my $scalar_ref = \(my $var);
187     dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
188
189     lives_ok { $bar->bunch_of_stuff([1, 2, 3]) } '... Bar::bunch_of_stuff accepts an array of ints';
190     dies_ok { $bar->bunch_of_stuff([qw[one two three]]) } '... Bar::bunch_of_stuff does not accept an array of strings';
191
192     my $code_ref = sub { 1 };
193     dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
194 }
195
196 # check some meta-stuff
197
198 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
199 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
200 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
201 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
202 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
203 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
204 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
205 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
206 ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr');
207 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr');
208
209 isnt(Foo->meta->get_attribute('foo'),
210      Bar->meta->get_attribute('foo'),
211      '... Foo and Bar have different copies of foo');
212 isnt(Foo->meta->get_attribute('bar'),
213      Bar->meta->get_attribute('bar'),
214      '... Foo and Bar have different copies of bar');
215 isnt(Foo->meta->get_attribute('baz'),
216      Bar->meta->get_attribute('baz'),
217      '... Foo and Bar have different copies of baz');
218 isnt(Foo->meta->get_attribute('gorch'),
219      Bar->meta->get_attribute('gorch'),
220      '... Foo and Bar have different copies of gorch');
221 isnt(Foo->meta->get_attribute('gloum'),
222      Bar->meta->get_attribute('gloum'),
223      '... Foo and Bar have different copies of gloum');
224 isnt(Foo->meta->get_attribute('bling'),
225      Bar->meta->get_attribute('bling'),
226      '... Foo and Bar have different copies of bling');
227 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
228      Bar->meta->get_attribute('bunch_of_stuff'),
229      '... Foo and Bar have different copies of bunch_of_stuff');
230
231 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
232    '... Bar::bar inherited the type constraint too');
233 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
234   '... Bar::baz inherited the type constraint too');
235
236 is(Bar->meta->get_attribute('bar')->type_constraint->name,
237    'Str', '... Bar::bar inherited the right type constraint too');
238
239 is(Foo->meta->get_attribute('baz')->type_constraint->name,
240   'Ref', '... Foo::baz inherited the right type constraint too');
241 is(Bar->meta->get_attribute('baz')->type_constraint->name,
242    'ArrayRef', '... Bar::baz inherited the right type constraint too');
243
244 ok(!Foo->meta->get_attribute('gorch')->is_required,
245   '... Foo::gorch is not a required attr');
246 ok(Bar->meta->get_attribute('gorch')->is_required,
247    '... Bar::gorch is a required attr');
248
249 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
250   'ArrayRef',
251   '... Foo::bunch_of_stuff is an ArrayRef');
252 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
253   'ArrayRef[Int]',
254   '... Bar::bunch_of_stuff is an ArrayRef[Int]');
255
256 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
257    '... Foo::gloum is not a required attr');
258 ok(Bar->meta->get_attribute('gloum')->is_lazy,
259    '... Bar::gloum is a required attr');
260
261 ok(!Foo->meta->get_attribute('foo')->should_coerce,
262   '... Foo::foo should not coerce');
263 ok(Bar->meta->get_attribute('foo')->should_coerce,
264    '... Bar::foo should coerce');
265
266 ok(!Foo->meta->get_attribute('bling')->has_handles,
267    '... Foo::foo should not handles');
268 ok(Bar->meta->get_attribute('bling')->has_handles,
269    '... Bar::foo should handles');
270
271 done_testing;