Conflict on File::ChangeNotify for coerce changes
[gitmo/Moose.git] / t / 020_attributes / 009_attribute_inherited_slot_specs.t
CommitLineData
1d768fb1 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
1d768fb1 7use Test::Exception;
8
7ff56534 9
1d768fb1 10{
83cc9094 11 package Thing;
12 use Moose;
d03bd989 13
83cc9094 14 sub hello { 'Hello World (from Thing)' }
d03bd989 15 sub goodbye { 'Goodbye World (from Thing)' }
16
1d768fb1 17 package Foo;
1d768fb1 18 use Moose;
ce0e8d63 19 use Moose::Util::TypeConstraints;
d03bd989 20
21 subtype 'FooStr'
ce0e8d63 22 => as 'Str'
23 => where { /Foo/ };
d03bd989 24
25 coerce 'FooStr'
ce0e8d63 26 => from ArrayRef
27 => via { 'FooArrayRef' };
d03bd989 28
1d768fb1 29 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
d03bd989 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 {[]});
d7e7abd9 35 has 'fleem' => (is => 'ro');
d03bd989 36
83cc9094 37 has 'bling' => (is => 'ro', isa => 'Thing');
d03bd989 38 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
39
f3c4e20e 40 has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
238b424d 41
d03bd989 42 has 'one_last_one' => (is => 'rw', isa => 'Ref');
43
ce0e8d63 44 # this one will work here ....
ccd4cff9 45 has 'fail' => (isa => 'CodeRef', is => 'bare');
46 has 'other_fail' => (is => 'bare');
d03bd989 47
1d768fb1 48 package Bar;
1d768fb1 49 use Moose;
238b424d 50 use Moose::Util::TypeConstraints;
d03bd989 51
1d768fb1 52 extends 'Foo';
83cc9094 53
d03bd989 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';
f3c4e20e 73
74 ::lives_ok {
449559bf 75 has '+gloum' => (lazy_build => 1);
76 } '... we can add lazy_build as an attribute option';
77
78 ::lives_ok {
d03bd989 79 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
f3c4e20e 80 } '... extend an attribute with parameterized type';
d03bd989 81
83cc9094 82 ::lives_ok {
d03bd989 83 has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
84 } '... extend an attribute with anon-subtype';
85
5e98d2b6 86 ::lives_ok {
d03bd989 87 has '+one_last_one' => (isa => 'Value');
88 } '... now can extend an attribute with a non-subtype';
39d37838 89
90 ::lives_ok {
d7e7abd9 91 has '+fleem' => (weak_ref => 1);
92 } '... now allowed to add the weak_ref option via inheritance';
93
94 ::lives_ok {
d03bd989 95 has '+bling' => (handles => ['hello']);
83cc9094 96 } '... we can add the handles attribute option';
d03bd989 97
ce0e8d63 98 # this one will *not* work here ....
83cc9094 99 ::dies_ok {
d03bd989 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';
329c5dd4 108 ::throws_ok {
109 has '+does_not_exist' => (isa => 'Str');
110 } qr/in Bar/, '... cannot extend a non-existing attribute';
1d768fb1 111}
112
113my $foo = Foo->new;
114isa_ok($foo, 'Foo');
115
ce0e8d63 116is($foo->foo, undef, '... got the right undef default value');
117lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
118is($foo->foo, 'FooString', '... got the right value for foo');
1d768fb1 119
ce0e8d63 120dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
121
122is($foo->bar, 'Foo::bar', '... got the right default value');
1d768fb1 123dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
124
ce0e8d63 125is($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');
d03bd989 131
ce0e8d63 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');
d03bd989 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
ce0e8d63 144 my $code_ref = sub { 1 };
145 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
d03bd989 146 is($foo->baz, $code_ref, '... got the right value assigned to baz');
ce0e8d63 147}
148
149dies_ok {
150 Bar->new;
151} '... cannot create Bar without required gorch param';
152
153my $bar = Bar->new(gorch => 'Bar::gorch');
1d768fb1 154isa_ok($bar, 'Bar');
155isa_ok($bar, 'Foo');
156
ce0e8d63 157is($bar->foo, undef, '... got the right undef default value');
158lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
159is($bar->foo, 'FooString', '... got the right value for foo');
160lives_ok { $bar->foo([]) } '... assigned foo correctly';
161is($bar->foo, 'FooArrayRef', '... got the right value for foo');
162
163is($bar->gorch, 'Bar::gorch', '... got the right default value');
1d768fb1 164
ce0e8d63 165is($bar->bar, 'Bar::bar', '... got the right default value');
1d768fb1 166dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
167
ce0e8d63 168is($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';
d03bd989 173
ce0e8d63 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';
d03bd989 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
ce0e8d63 184 my $code_ref = sub { 1 };
185 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
186}
187
1d768fb1 188# check some meta-stuff
189
ce0e8d63 190ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
1d768fb1 191ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
ce0e8d63 192ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
193ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
83cc9094 194ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
195ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
f3c4e20e 196ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
83cc9094 197ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
5e98d2b6 198ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr');
199ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr');
ce0e8d63 200
d03bd989 201isnt(Foo->meta->get_attribute('foo'),
202 Bar->meta->get_attribute('foo'),
ce0e8d63 203 '... Foo and Bar have different copies of foo');
d03bd989 204isnt(Foo->meta->get_attribute('bar'),
205 Bar->meta->get_attribute('bar'),
1d768fb1 206 '... Foo and Bar have different copies of bar');
d03bd989 207isnt(Foo->meta->get_attribute('baz'),
208 Bar->meta->get_attribute('baz'),
209 '... Foo and Bar have different copies of baz');
210isnt(Foo->meta->get_attribute('gorch'),
211 Bar->meta->get_attribute('gorch'),
83cc9094 212 '... Foo and Bar have different copies of gorch');
d03bd989 213isnt(Foo->meta->get_attribute('gloum'),
214 Bar->meta->get_attribute('gloum'),
215 '... Foo and Bar have different copies of gloum');
216isnt(Foo->meta->get_attribute('bling'),
217 Bar->meta->get_attribute('bling'),
218 '... Foo and Bar have different copies of bling');
219isnt(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
223ok(Bar->meta->get_attribute('bar')->has_type_constraint,
daea75c9 224 '... Bar::bar inherited the type constraint too');
d03bd989 225ok(Bar->meta->get_attribute('baz')->has_type_constraint,
226 '... Bar::baz inherited the type constraint too');
1d768fb1 227
d03bd989 228is(Bar->meta->get_attribute('bar')->type_constraint->name,
daea75c9 229 'Str', '... Bar::bar inherited the right type constraint too');
1d768fb1 230
d03bd989 231is(Foo->meta->get_attribute('baz')->type_constraint->name,
ce0e8d63 232 'Ref', '... Foo::baz inherited the right type constraint too');
d03bd989 233is(Bar->meta->get_attribute('baz')->type_constraint->name,
234 'ArrayRef', '... Bar::baz inherited the right type constraint too');
235
236ok(!Foo->meta->get_attribute('gorch')->is_required,
ce0e8d63 237 '... Foo::gorch is not a required attr');
d03bd989 238ok(Bar->meta->get_attribute('gorch')->is_required,
ce0e8d63 239 '... Bar::gorch is a required attr');
d03bd989 240
241is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
f3c4e20e 242 'ArrayRef',
243 '... Foo::bunch_of_stuff is an ArrayRef');
d03bd989 244is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
f3c4e20e 245 'ArrayRef[Int]',
246 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
d03bd989 247
248ok(!Foo->meta->get_attribute('gloum')->is_lazy,
83cc9094 249 '... Foo::gloum is not a required attr');
d03bd989 250ok(Bar->meta->get_attribute('gloum')->is_lazy,
251 '... Bar::gloum is a required attr');
252
253ok(!Foo->meta->get_attribute('foo')->should_coerce,
ce0e8d63 254 '... Foo::foo should not coerce');
d03bd989 255ok(Bar->meta->get_attribute('foo')->should_coerce,
256 '... Bar::foo should coerce');
257
258ok(!Foo->meta->get_attribute('bling')->has_handles,
83cc9094 259 '... Foo::foo should not handles');
d03bd989 260ok(Bar->meta->get_attribute('bling')->has_handles,
261 '... Bar::foo should handles');
ce0e8d63 262
a28e50e4 263done_testing;