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