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