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