6 use Test::More tests => 47;
13 package Test::Attribute::Inline::Documentation;
18 The 'foo' attribute is my favorite
19 attribute in the whole wide world.
25 my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');
27 ok($foo_attr->has_documentation, 'the foo has docs');
28 is($foo_attr->documentation,
30 The 'foo' attribute is my favorite
31 attribute in the whole wide world.
38 package Test::For::Lazy::TypeConstraint;
40 use Moose::Util::TypeConstraints;
42 has 'bad_lazy_attr' => (
46 default => sub { "test" },
49 has 'good_lazy_attr' => (
53 default => sub { [] },
58 my $test = Test::For::Lazy::TypeConstraint->new;
59 isa_ok($test, 'Test::For::Lazy::TypeConstraint');
63 } 'this does not work';
66 $test->good_lazy_attr;
67 } 'this does not work';
72 package Test::Arrayref::Attributes;
75 has [qw(foo bar baz)] => (
81 my $test = Test::Arrayref::Attributes->new;
82 isa_ok($test, 'Test::Arrayref::Attributes');
83 can_ok($test, qw(foo bar baz));
89 package Test::Arrayref::RoleAttributes::Role;
92 has [qw(foo bar baz)] => (
98 package Test::Arrayref::RoleAttributes;
100 with 'Test::Arrayref::RoleAttributes::Role';
103 my $test = Test::Arrayref::RoleAttributes->new;
104 isa_ok($test, 'Test::Arrayref::RoleAttributes');
105 can_ok($test, qw(foo bar baz));
111 package Test::UndefDefault::Attributes;
117 default => sub { return }
123 Test::UndefDefault::Attributes->new;
124 } 'default must return a value which passes the type constraint';
130 package OverloadedStr;
132 use overload '""' => sub { 'this is *not* a string' };
134 has 'a_str' => ( isa => 'Str' , is => 'rw' );
137 my $moose_obj = OverloadedStr->new;
139 is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
140 ok($moose_obj, 'this is a *not* a string');
143 $moose_obj->a_str( $moose_obj )
144 } qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' failed with value OverloadedStr=HASH\(0x.+?\)/,
145 'dies without overloading the string';
151 package OverloadBreaker;
154 has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
158 OverloadBreaker->new;
159 } qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 7\.5/,
160 'this doesnt trip overload to break anymore ';
163 OverloadBreaker->new(a_num => 5);
164 } 'this works fine though';
170 package Test::Builder::Attribute;
173 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
174 sub build_foo { return "works" };
177 my $meta = Test::Builder::Attribute->meta;
178 my $foo_attr = $meta->get_attribute("foo");
180 ok($foo_attr->is_required, "foo is required");
181 ok($foo_attr->has_builder, "foo has builder");
182 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
184 my $instance = Test::Builder::Attribute->new;
185 is($instance->foo, 'works', "foo builder works");
190 package Test::Builder::Attribute::Broken;
193 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
197 Test::Builder::Attribute::Broken->new;
204 package Test::LazyBuild::Attribute;
207 has 'foo' => ( lazy_build => 1, is => 'ro');
208 has '_foo' => ( lazy_build => 1, is => 'ro');
209 has 'fool' => ( lazy_build => 1, is => 'ro');
210 sub _build_foo { return "works" };
211 sub _build__foo { return "works too" };
214 my $meta = Test::LazyBuild::Attribute->meta;
215 my $foo_attr = $meta->get_attribute("foo");
216 my $_foo_attr = $meta->get_attribute("_foo");
218 ok($foo_attr->is_lazy, "foo is lazy");
219 ok($foo_attr->is_lazy_build, "foo is lazy_build");
221 ok($foo_attr->has_clearer, "foo has clearer");
222 is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo");
224 ok($foo_attr->has_builder, "foo has builder");
225 is($foo_attr->builder, "_build_foo", ".. and it's named build_foo");
227 ok($foo_attr->has_predicate, "foo has predicate");
228 is($foo_attr->predicate, "has_foo", ".. and it's named has_foo");
230 ok($_foo_attr->is_lazy, "_foo is lazy");
231 ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required");
232 ok($_foo_attr->is_lazy_build, "_foo is lazy_build");
234 ok($_foo_attr->has_clearer, "_foo has clearer");
235 is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo");
237 ok($_foo_attr->has_builder, "_foo has builder");
238 is($_foo_attr->builder, "_build__foo", ".. and it's named _build_foo");
240 ok($_foo_attr->has_predicate, "_foo has predicate");
241 is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");
243 my $instance = Test::LazyBuild::Attribute->new;
244 ok(!$instance->has_foo, "noo foo value yet");
245 ok(!$instance->_has_foo, "noo _foo value yet");
246 is($instance->foo, 'works', "foo builder works");
247 is($instance->_foo, 'works too', "foo builder works too");
248 throws_ok { $instance->fool }
249 qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/,
250 "Correct error when a builder method is not present";
255 package OutOfClassTest;
260 lives_ok { OutOfClassTest::has('foo', is => 'bare'); } 'create attr via direct sub call';
261 lives_ok { OutOfClassTest->can('has')->('bar', is => 'bare'); } 'create attr via can';
263 ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call');
264 ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can');
272 ::throws_ok { has 'foo' => ( 'ro', isa => 'Str' ) }
273 qr/^Usage/, 'has throws error with odd number of attribute options';