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