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