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