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