12 package Test::Attribute::Inline::Documentation;
17 The 'foo' attribute is my favorite
18 attribute in the whole wide world.
24 my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');
26 ok($foo_attr->has_documentation, '... the foo has docs');
27 is($foo_attr->documentation,
29 The 'foo' attribute is my favorite
30 attribute in the whole wide world.
32 '... got the foo docs');
37 package Test::For::Lazy::TypeConstraint;
39 use Moose::Util::TypeConstraints;
41 has 'bad_lazy_attr' => (
45 default => sub { "test" },
48 has 'good_lazy_attr' => (
52 default => sub { [] },
57 my $test = Test::For::Lazy::TypeConstraint->new;
58 isa_ok($test, 'Test::For::Lazy::TypeConstraint');
62 }, undef, '... this does not work' );
65 $test->good_lazy_attr;
66 }, undef, '... this does not work' );
71 package Test::Arrayref::Attributes;
74 has [qw(foo bar baz)] => (
80 my $test = Test::Arrayref::Attributes->new;
81 isa_ok($test, 'Test::Arrayref::Attributes');
82 can_ok($test, qw(foo bar baz));
88 package Test::Arrayref::RoleAttributes::Role;
91 has [qw(foo bar baz)] => (
97 package Test::Arrayref::RoleAttributes;
99 with 'Test::Arrayref::RoleAttributes::Role';
102 my $test = Test::Arrayref::RoleAttributes->new;
103 isa_ok($test, 'Test::Arrayref::RoleAttributes');
104 can_ok($test, qw(foo bar baz));
110 package Test::UndefDefault::Attributes;
116 default => sub { return }
122 Test::UndefDefault::Attributes->new;
123 }, undef, '... default must return a value which passes the type constraint' );
129 package OverloadedStr;
131 use overload '""' => sub { 'this is *not* a string' };
133 has 'a_str' => ( isa => 'Str' , is => 'rw' );
136 my $moose_obj = OverloadedStr->new;
138 is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
139 ok($moose_obj, 'this is a *not* a string');
142 $moose_obj->a_str( $moose_obj )
143 }, qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' with value .*OverloadedStr/, '... dies without overloading the string' );
149 package OverloadBreaker;
152 has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
156 OverloadBreaker->new;
157 }, qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' with value 7\.5/, '... this doesnt trip overload to break anymore ' );
160 OverloadBreaker->new(a_num => 5);
161 }, undef, '... this works fine though' );
167 package Test::Builder::Attribute;
170 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
171 sub build_foo { return "works" };
174 my $meta = Test::Builder::Attribute->meta;
175 my $foo_attr = $meta->get_attribute("foo");
177 ok($foo_attr->is_required, "foo is required");
178 ok($foo_attr->has_builder, "foo has builder");
179 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
181 my $instance = Test::Builder::Attribute->new;
182 is($instance->foo, 'works', "foo builder works");
187 package Test::Builder::Attribute::Broken;
190 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
194 Test::Builder::Attribute::Broken->new;
195 }, undef, '... no builder, wtf' );
201 package Test::LazyBuild::Attribute;
204 has 'foo' => ( lazy_build => 1, is => 'ro');
205 has '_foo' => ( lazy_build => 1, is => 'ro');
206 has 'fool' => ( lazy_build => 1, is => 'ro');
207 sub _build_foo { return "works" };
208 sub _build__foo { return "works too" };
211 my $meta = Test::LazyBuild::Attribute->meta;
212 my $foo_attr = $meta->get_attribute("foo");
213 my $_foo_attr = $meta->get_attribute("_foo");
215 ok($foo_attr->is_lazy, "foo is lazy");
216 ok($foo_attr->is_lazy_build, "foo is lazy_build");
218 ok($foo_attr->has_clearer, "foo has clearer");
219 is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo");
221 ok($foo_attr->has_builder, "foo has builder");
222 is($foo_attr->builder, "_build_foo", ".. and it's named build_foo");
224 ok($foo_attr->has_predicate, "foo has predicate");
225 is($foo_attr->predicate, "has_foo", ".. and it's named has_foo");
227 ok($_foo_attr->is_lazy, "_foo is lazy");
228 ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required");
229 ok($_foo_attr->is_lazy_build, "_foo is lazy_build");
231 ok($_foo_attr->has_clearer, "_foo has clearer");
232 is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo");
234 ok($_foo_attr->has_builder, "_foo has builder");
235 is($_foo_attr->builder, "_build__foo", ".. and it's named _build_foo");
237 ok($_foo_attr->has_predicate, "_foo has predicate");
238 is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");
240 my $instance = Test::LazyBuild::Attribute->new;
241 ok(!$instance->has_foo, "noo foo value yet");
242 ok(!$instance->_has_foo, "noo _foo value yet");
243 is($instance->foo, 'works', "foo builder works");
244 is($instance->_foo, 'works too', "foo builder works too");
245 like( exception { $instance->fool }, qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/, "Correct error when a builder method is not present" );
250 package OutOfClassTest;
255 is( exception { OutOfClassTest::has('foo', is => 'bare'); }, undef, 'create attr via direct sub call' );
256 is( exception { OutOfClassTest->can('has')->('bar', is => 'bare'); }, undef, 'create attr via can' );
258 ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call');
259 ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can');
267 ::like( ::exception { has 'foo' => ( 'ro', isa => 'Str' ) }, qr/^Usage/, 'has throws error with odd number of attribute options' );