6 use Test::More tests => 72;
17 sub hello { 'Hello World (from Thing)' }
18 sub goodbye { 'Goodbye World (from Thing)' }
22 use Moose::Util::TypeConstraints;
30 => via { 'FooArrayRef' };
32 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
33 has 'baz' => (is => 'rw', isa => 'Ref');
34 has 'foo' => (is => 'rw', isa => 'FooStr');
36 has 'gorch' => (is => 'ro');
37 has 'gloum' => (is => 'ro', default => sub {[]});
39 has 'bling' => (is => 'ro', isa => 'Thing');
40 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
42 # this one will work here ....
43 has 'fail' => (isa => 'CodeRef');
52 has '+bar' => (default => 'Bar::bar');
53 } '... we can change the default attribute option';
56 has '+baz' => (isa => 'ArrayRef');
57 } '... we can add change the isa as long as it is a subtype';
60 has '+foo' => (coerce => 1);
61 } '... we can change/add coerce as an attribute option';
64 has '+gorch' => (required => 1);
65 } '... we can change/add required as an attribute option';
68 has '+gloum' => (lazy => 1);
69 } '... we can change/add lazy as an attribute option';
72 has '+bling' => (handles => ['hello']);
73 } '... we can add the handles attribute option';
75 # this one will *not* work here ....
77 has '+blang' => (handles => ['hello']);
78 } '... we can not alter the handles attribute option';
80 has '+fail' => (isa => 'Ref');
81 } '... cannot create an attribute with an improper subtype relation';
83 has '+other_fail' => (trigger => sub {});
84 } '... cannot create an attribute with an illegal option';
86 has '+other_fail' => (weak_ref => 1);
87 } '... cannot create an attribute with an illegal option';
94 is($foo->foo, undef, '... got the right undef default value');
95 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
96 is($foo->foo, 'FooString', '... got the right value for foo');
98 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
100 is($foo->bar, 'Foo::bar', '... got the right default value');
101 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
103 is($foo->baz, undef, '... got the right undef default value');
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');
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');
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');
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');
125 } '... cannot create Bar without required gorch param';
127 my $bar = Bar->new(gorch => 'Bar::gorch');
131 is($bar->foo, undef, '... got the right undef default value');
132 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
133 is($bar->foo, 'FooString', '... got the right value for foo');
134 lives_ok { $bar->foo([]) } '... assigned foo correctly';
135 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
137 is($bar->gorch, 'Bar::gorch', '... got the right default value');
139 is($bar->bar, 'Bar::bar', '... got the right default value');
140 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
142 is($bar->baz, undef, '... got the right undef default value');
146 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
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');
152 my $scalar_ref = \(my $var);
153 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
155 my $code_ref = sub { 1 };
156 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
159 # check some meta-stuff
161 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
162 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
163 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
164 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
165 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
166 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
167 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
168 ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
169 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
171 isnt(Foo->meta->get_attribute('foo'),
172 Bar->meta->get_attribute('foo'),
173 '... Foo and Bar have different copies of foo');
174 isnt(Foo->meta->get_attribute('bar'),
175 Bar->meta->get_attribute('bar'),
176 '... Foo and Bar have different copies of bar');
177 isnt(Foo->meta->get_attribute('baz'),
178 Bar->meta->get_attribute('baz'),
179 '... Foo and Bar have different copies of baz');
180 isnt(Foo->meta->get_attribute('gorch'),
181 Bar->meta->get_attribute('gorch'),
182 '... Foo and Bar have different copies of gorch');
183 isnt(Foo->meta->get_attribute('gloum'),
184 Bar->meta->get_attribute('gloum'),
185 '... Foo and Bar have different copies of gloum');
186 isnt(Foo->meta->get_attribute('bling'),
187 Bar->meta->get_attribute('bling'),
188 '... Foo and Bar have different copies of bling');
190 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
191 '... Bar::bar inherited the type constraint too');
192 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
193 '... Bar::baz inherited the type constraint too');
195 is(Bar->meta->get_attribute('bar')->type_constraint->name,
196 'Str', '... Bar::bar inherited the right type constraint too');
198 is(Foo->meta->get_attribute('baz')->type_constraint->name,
199 'Ref', '... Foo::baz inherited the right type constraint too');
200 is(Bar->meta->get_attribute('baz')->type_constraint->name,
201 'ArrayRef', '... Bar::baz inherited the right type constraint too');
203 ok(!Foo->meta->get_attribute('gorch')->is_required,
204 '... Foo::gorch is not a required attr');
205 ok(Bar->meta->get_attribute('gorch')->is_required,
206 '... Bar::gorch is a required attr');
208 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
209 '... Foo::gloum is not a required attr');
210 ok(Bar->meta->get_attribute('gloum')->is_lazy,
211 '... Bar::gloum is a required attr');
213 ok(!Foo->meta->get_attribute('foo')->should_coerce,
214 '... Foo::foo should not coerce');
215 ok(Bar->meta->get_attribute('foo')->should_coerce,
216 '... Bar::foo should coerce');
218 ok(!Foo->meta->get_attribute('bling')->has_handles,
219 '... Foo::foo should not handles');
220 ok(Bar->meta->get_attribute('bling')->has_handles,
221 '... Bar::foo should handles');