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