6 use Test::More tests => 43;
14 package Test::Attribute::Inline::Documentation;
19 The 'foo' attribute is my favorite
20 attribute in the whole wide world.
26 my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');
28 ok($foo_attr->has_documentation, '... the foo has docs');
29 is($foo_attr->documentation,
31 The 'foo' attribute is my favorite
32 attribute in the whole wide world.
34 '... got the foo docs');
39 package Test::For::Lazy::TypeConstraint;
41 use Mouse::Util::TypeConstraints;
43 has 'bad_lazy_attr' => (
47 default => sub { "test" },
50 has 'good_lazy_attr' => (
54 default => sub { [] },
59 my $test = Test::For::Lazy::TypeConstraint->new;
60 isa_ok($test, 'Test::For::Lazy::TypeConstraint');
64 } '... this does not work';
67 $test->good_lazy_attr;
68 } '... this does work';
73 package Test::Arrayref::Attributes;
76 has [qw(foo bar baz)] => (
82 my $test = Test::Arrayref::Attributes->new;
83 isa_ok($test, 'Test::Arrayref::Attributes');
84 can_ok($test, qw(foo bar baz));
90 package Test::Arrayref::RoleAttributes::Role;
93 has [qw(foo bar baz)] => (
99 package Test::Arrayref::RoleAttributes;
101 with 'Test::Arrayref::RoleAttributes::Role';
104 my $test = Test::Arrayref::RoleAttributes->new;
105 isa_ok($test, 'Test::Arrayref::RoleAttributes');
106 can_ok($test, qw(foo bar baz));
112 package Test::UndefDefault::Attributes;
118 default => sub { return }
124 Test::UndefDefault::Attributes->new;
125 } '... default must return a value which passes the type constraint';
131 package OverloadedStr;
133 use overload '""' => sub { 'this is *not* a string' };
135 has 'a_str' => ( isa => 'Str' , is => 'rw' );
138 my $moose_obj = OverloadedStr->new;
140 is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
141 ok($moose_obj, 'this is a *not* a string');
144 $moose_obj->a_str( $moose_obj )
145 } qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' with value OverloadedStr=HASH\(0x.+?\)/,
146 '... dies without overloading the string';
152 package OverloadBreaker;
155 has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
159 OverloadBreaker->new;
160 } qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' with value 7\.5/,
161 '... this doesnt trip overload to break anymore ';
164 OverloadBreaker->new(a_num => 5);
165 } '... this works fine though';
171 package Test::Builder::Attribute;
174 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
175 sub build_foo { return "works" };
178 my $meta = Test::Builder::Attribute->meta;
179 my $foo_attr = $meta->get_attribute("foo");
181 ok($foo_attr->is_required, "foo is required");
182 ok($foo_attr->has_builder, "foo has builder");
183 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
185 my $instance = Test::Builder::Attribute->new;
186 is($instance->foo, 'works', "foo builder works");
191 package Test::Builder::Attribute::Broken;
194 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
198 Test::Builder::Attribute::Broken->new;
199 } '... no builder, wtf';
205 package Test::LazyBuild::Attribute;
208 has 'foo' => ( lazy_build => 1, is => 'ro');
209 has '_foo' => ( lazy_build => 1, is => 'ro');
210 has 'fool' => ( lazy_build => 1, is => 'ro');
211 sub _build_foo { return "works" };
212 sub _build__foo { return "works too" };
215 my $meta = Test::LazyBuild::Attribute->meta;
216 my $foo_attr = $meta->get_attribute("foo");
217 my $_foo_attr = $meta->get_attribute("_foo");
219 ok($foo_attr->is_lazy, "foo is lazy");
220 ok($foo_attr->is_lazy_build, "foo is lazy_build");
222 ok($foo_attr->has_clearer, "foo has clearer");
223 is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo");
225 ok($foo_attr->has_builder, "foo has builder");
226 is($foo_attr->builder, "_build_foo", ".. and it's named build_foo");
228 ok($foo_attr->has_predicate, "foo has predicate");
229 is($foo_attr->predicate, "has_foo", ".. and it's named has_foo");
231 ok($_foo_attr->is_lazy, "_foo is lazy");
232 ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required");
233 ok($_foo_attr->is_lazy_build, "_foo is lazy_build");
235 ok($_foo_attr->has_clearer, "_foo has clearer");
236 is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo");
238 ok($_foo_attr->has_builder, "_foo has builder");
239 is($_foo_attr->builder, "_build__foo", ".. and it's named _build_foo");
241 ok($_foo_attr->has_predicate, "_foo has predicate");
242 is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");
244 my $instance = Test::LazyBuild::Attribute->new;
245 ok(!$instance->has_foo, "noo foo value yet");
246 ok(!$instance->_has_foo, "noo _foo value yet");
247 is($instance->foo, 'works', "foo builder works");
248 is($instance->_foo, 'works too', "foo builder works too");
249 dies_ok { $instance->fool }
250 # throws_ok { $instance->fool }
251 # qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/,
252 "Correct error when a builder method is not present";
257 package OutOfClassTest;
262 # Mouse::Exporter does not support 'with_meta'
263 #lives_ok { OutOfClassTest::has('foo', is => 'bare'); } 'create attr via direct sub call';
264 #lives_ok { OutOfClassTest->can('has')->('bar', is => 'bare'); } 'create attr via can';
266 #ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call');
267 #ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can');
275 ::throws_ok { has 'foo' => ( 'ro', isa => 'Str' ) }
276 qr/^Usage/, 'has throws error with odd number of attribute options';