(failing) test for runtime roles and non-moose classes + does attrs
[gitmo/Moose.git] / t / 020_attributes / 009_attribute_inherited_slot_specs.t
CommitLineData
1d768fb1 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
f3c4e20e 6use Test::More tests => 80;
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');
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;
50
51 extends 'Foo';
83cc9094 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';
1d768fb1 60
83cc9094 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';
f3c4e20e 72
73 ::lives_ok {
74 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
75 } '... extend an attribute with parameterized type';
ce0e8d63 76
83cc9094 77 ::lives_ok {
78 has '+bling' => (handles => ['hello']);
79 } '... we can add the handles attribute option';
ce0e8d63 80
81 # this one will *not* work here ....
83cc9094 82 ::dies_ok {
83 has '+blang' => (handles => ['hello']);
84 } '... we can not alter the handles attribute option';
ce0e8d63 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';
ce0e8d63 94
1d768fb1 95}
96
97my $foo = Foo->new;
98isa_ok($foo, 'Foo');
99
ce0e8d63 100is($foo->foo, undef, '... got the right undef default value');
101lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
102is($foo->foo, 'FooString', '... got the right value for foo');
1d768fb1 103
ce0e8d63 104dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
105
106is($foo->bar, 'Foo::bar', '... got the right default value');
1d768fb1 107dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
108
ce0e8d63 109is($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
f3c4e20e 124 lives_ok { $foo->bunch_of_stuff([qw[one two three]]) } '... Foo::bunch_of_stuff accepts an array of strings';
125
ce0e8d63 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
131dies_ok {
132 Bar->new;
133} '... cannot create Bar without required gorch param';
134
135my $bar = Bar->new(gorch => 'Bar::gorch');
1d768fb1 136isa_ok($bar, 'Bar');
137isa_ok($bar, 'Foo');
138
ce0e8d63 139is($bar->foo, undef, '... got the right undef default value');
140lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
141is($bar->foo, 'FooString', '... got the right value for foo');
142lives_ok { $bar->foo([]) } '... assigned foo correctly';
143is($bar->foo, 'FooArrayRef', '... got the right value for foo');
144
145is($bar->gorch, 'Bar::gorch', '... got the right default value');
1d768fb1 146
ce0e8d63 147is($bar->bar, 'Bar::bar', '... got the right default value');
1d768fb1 148dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
149
ce0e8d63 150is($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
f3c4e20e 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
ce0e8d63 166 my $code_ref = sub { 1 };
167 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
168}
169
1d768fb1 170# check some meta-stuff
171
ce0e8d63 172ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
1d768fb1 173ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
ce0e8d63 174ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
175ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
83cc9094 176ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
177ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
f3c4e20e 178ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
83cc9094 179ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
ce0e8d63 180ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
181ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
182
183isnt(Foo->meta->get_attribute('foo'),
184 Bar->meta->get_attribute('foo'),
185 '... Foo and Bar have different copies of foo');
1d768fb1 186isnt(Foo->meta->get_attribute('bar'),
187 Bar->meta->get_attribute('bar'),
188 '... Foo and Bar have different copies of bar');
ce0e8d63 189isnt(Foo->meta->get_attribute('baz'),
190 Bar->meta->get_attribute('baz'),
191 '... Foo and Bar have different copies of baz');
192isnt(Foo->meta->get_attribute('gorch'),
193 Bar->meta->get_attribute('gorch'),
83cc9094 194 '... Foo and Bar have different copies of gorch');
195isnt(Foo->meta->get_attribute('gloum'),
196 Bar->meta->get_attribute('gloum'),
197 '... Foo and Bar have different copies of gloum');
198isnt(Foo->meta->get_attribute('bling'),
199 Bar->meta->get_attribute('bling'),
200 '... Foo and Bar have different copies of bling');
f3c4e20e 201isnt(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');
ce0e8d63 204
daea75c9 205ok(Bar->meta->get_attribute('bar')->has_type_constraint,
206 '... Bar::bar inherited the type constraint too');
ce0e8d63 207ok(Bar->meta->get_attribute('baz')->has_type_constraint,
208 '... Bar::baz inherited the type constraint too');
1d768fb1 209
daea75c9 210is(Bar->meta->get_attribute('bar')->type_constraint->name,
211 'Str', '... Bar::bar inherited the right type constraint too');
1d768fb1 212
ce0e8d63 213is(Foo->meta->get_attribute('baz')->type_constraint->name,
214 'Ref', '... Foo::baz inherited the right type constraint too');
215is(Bar->meta->get_attribute('baz')->type_constraint->name,
216 'ArrayRef', '... Bar::baz inherited the right type constraint too');
217
218ok(!Foo->meta->get_attribute('gorch')->is_required,
219 '... Foo::gorch is not a required attr');
220ok(Bar->meta->get_attribute('gorch')->is_required,
221 '... Bar::gorch is a required attr');
222
f3c4e20e 223is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
224 'ArrayRef',
225 '... Foo::bunch_of_stuff is an ArrayRef');
226is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
227 'ArrayRef[Int]',
228 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
229
83cc9094 230ok(!Foo->meta->get_attribute('gloum')->is_lazy,
231 '... Foo::gloum is not a required attr');
232ok(Bar->meta->get_attribute('gloum')->is_lazy,
233 '... Bar::gloum is a required attr');
234
ce0e8d63 235ok(!Foo->meta->get_attribute('foo')->should_coerce,
236 '... Foo::foo should not coerce');
237ok(Bar->meta->get_attribute('foo')->should_coerce,
83cc9094 238 '... Bar::foo should coerce');
239
240ok(!Foo->meta->get_attribute('bling')->has_handles,
241 '... Foo::foo should not handles');
242ok(Bar->meta->get_attribute('bling')->has_handles,
243 '... Bar::foo should handles');
ce0e8d63 244
1d768fb1 245