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