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