cleanup some stuff before release
[gitmo/Moose.git] / t / 020_attributes / 009_attribute_inherited_slot_specs.t
CommitLineData
1d768fb1 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
83cc9094 6use Test::More tests => 72;
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
42 # this one will work here ....
43 has 'fail' => (isa => 'CodeRef');
44 has 'other_fail';
1d768fb1 45
46 package Bar;
1d768fb1 47 use Moose;
48
49 extends 'Foo';
83cc9094 50
51 ::lives_ok {
52 has '+bar' => (default => 'Bar::bar');
53 } '... we can change the default attribute option';
54
55 ::lives_ok {
56 has '+baz' => (isa => 'ArrayRef');
57 } '... we can add change the isa as long as it is a subtype';
1d768fb1 58
83cc9094 59 ::lives_ok {
60 has '+foo' => (coerce => 1);
61 } '... we can change/add coerce as an attribute option';
62
63 ::lives_ok {
64 has '+gorch' => (required => 1);
65 } '... we can change/add required as an attribute option';
66
67 ::lives_ok {
68 has '+gloum' => (lazy => 1);
69 } '... we can change/add lazy as an attribute option';
ce0e8d63 70
83cc9094 71 ::lives_ok {
72 has '+bling' => (handles => ['hello']);
73 } '... we can add the handles attribute option';
ce0e8d63 74
75 # this one will *not* work here ....
83cc9094 76 ::dies_ok {
77 has '+blang' => (handles => ['hello']);
78 } '... we can not alter the handles attribute option';
ce0e8d63 79 ::dies_ok {
80 has '+fail' => (isa => 'Ref');
81 } '... cannot create an attribute with an improper subtype relation';
82 ::dies_ok {
83 has '+other_fail' => (trigger => sub {});
84 } '... cannot create an attribute with an illegal option';
85 ::dies_ok {
86 has '+other_fail' => (weak_ref => 1);
87 } '... cannot create an attribute with an illegal option';
ce0e8d63 88
1d768fb1 89}
90
91my $foo = Foo->new;
92isa_ok($foo, 'Foo');
93
ce0e8d63 94is($foo->foo, undef, '... got the right undef default value');
95lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
96is($foo->foo, 'FooString', '... got the right value for foo');
1d768fb1 97
ce0e8d63 98dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
99
100is($foo->bar, 'Foo::bar', '... got the right default value');
1d768fb1 101dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
102
ce0e8d63 103is($foo->baz, undef, '... got the right undef default value');
104
105{
106 my $hash_ref = {};
107 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
108 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
109
110 my $array_ref = [];
111 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
112 is($foo->baz, $array_ref, '... got the right value assigned to baz');
113
114 my $scalar_ref = \(my $var);
115 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
116 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
117
118 my $code_ref = sub { 1 };
119 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
120 is($foo->baz, $code_ref, '... got the right value assigned to baz');
121}
122
123dies_ok {
124 Bar->new;
125} '... cannot create Bar without required gorch param';
126
127my $bar = Bar->new(gorch => 'Bar::gorch');
1d768fb1 128isa_ok($bar, 'Bar');
129isa_ok($bar, 'Foo');
130
ce0e8d63 131is($bar->foo, undef, '... got the right undef default value');
132lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
133is($bar->foo, 'FooString', '... got the right value for foo');
134lives_ok { $bar->foo([]) } '... assigned foo correctly';
135is($bar->foo, 'FooArrayRef', '... got the right value for foo');
136
137is($bar->gorch, 'Bar::gorch', '... got the right default value');
1d768fb1 138
ce0e8d63 139is($bar->bar, 'Bar::bar', '... got the right default value');
1d768fb1 140dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
141
ce0e8d63 142is($bar->baz, undef, '... got the right undef default value');
143
144{
145 my $hash_ref = {};
146 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
147
148 my $array_ref = [];
149 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
150 is($bar->baz, $array_ref, '... got the right value assigned to baz');
151
152 my $scalar_ref = \(my $var);
153 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
154
155 my $code_ref = sub { 1 };
156 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
157}
158
1d768fb1 159# check some meta-stuff
160
ce0e8d63 161ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
1d768fb1 162ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
ce0e8d63 163ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
164ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
83cc9094 165ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
166ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
167ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
ce0e8d63 168ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
169ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
170
171isnt(Foo->meta->get_attribute('foo'),
172 Bar->meta->get_attribute('foo'),
173 '... Foo and Bar have different copies of foo');
1d768fb1 174isnt(Foo->meta->get_attribute('bar'),
175 Bar->meta->get_attribute('bar'),
176 '... Foo and Bar have different copies of bar');
ce0e8d63 177isnt(Foo->meta->get_attribute('baz'),
178 Bar->meta->get_attribute('baz'),
179 '... Foo and Bar have different copies of baz');
180isnt(Foo->meta->get_attribute('gorch'),
181 Bar->meta->get_attribute('gorch'),
83cc9094 182 '... Foo and Bar have different copies of gorch');
183isnt(Foo->meta->get_attribute('gloum'),
184 Bar->meta->get_attribute('gloum'),
185 '... Foo and Bar have different copies of gloum');
186isnt(Foo->meta->get_attribute('bling'),
187 Bar->meta->get_attribute('bling'),
188 '... Foo and Bar have different copies of bling');
ce0e8d63 189
daea75c9 190ok(Bar->meta->get_attribute('bar')->has_type_constraint,
191 '... Bar::bar inherited the type constraint too');
ce0e8d63 192ok(Bar->meta->get_attribute('baz')->has_type_constraint,
193 '... Bar::baz inherited the type constraint too');
1d768fb1 194
daea75c9 195is(Bar->meta->get_attribute('bar')->type_constraint->name,
196 'Str', '... Bar::bar inherited the right type constraint too');
1d768fb1 197
ce0e8d63 198is(Foo->meta->get_attribute('baz')->type_constraint->name,
199 'Ref', '... Foo::baz inherited the right type constraint too');
200is(Bar->meta->get_attribute('baz')->type_constraint->name,
201 'ArrayRef', '... Bar::baz inherited the right type constraint too');
202
203ok(!Foo->meta->get_attribute('gorch')->is_required,
204 '... Foo::gorch is not a required attr');
205ok(Bar->meta->get_attribute('gorch')->is_required,
206 '... Bar::gorch is a required attr');
207
83cc9094 208ok(!Foo->meta->get_attribute('gloum')->is_lazy,
209 '... Foo::gloum is not a required attr');
210ok(Bar->meta->get_attribute('gloum')->is_lazy,
211 '... Bar::gloum is a required attr');
212
ce0e8d63 213ok(!Foo->meta->get_attribute('foo')->should_coerce,
214 '... Foo::foo should not coerce');
215ok(Bar->meta->get_attribute('foo')->should_coerce,
83cc9094 216 '... Bar::foo should coerce');
217
218ok(!Foo->meta->get_attribute('bling')->has_handles,
219 '... Foo::foo should not handles');
220ok(Bar->meta->get_attribute('bling')->has_handles,
221 '... Bar::foo should handles');
ce0e8d63 222
1d768fb1 223