6 use Test::More tests => 40;
15 package Test::Attribute::Inline::Documentation;
20 The 'foo' attribute is my favorite
21 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 Moose::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 not 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::UndefDefault::Attributes;
96 default => sub { return }
102 Test::UndefDefault::Attributes->new;
103 } '... default must return a value which passes the type constraint';
109 package OverloadedStr;
111 use overload '""' => sub { 'this is *not* a string' };
113 has 'a_str' => ( isa => 'Str' , is => 'rw' );
116 my $moose_obj = OverloadedStr->new;
118 is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
119 ok($moose_obj, 'this is a *not* a string');
122 $moose_obj->a_str( $moose_obj )
123 } qr/Attribute \(a_str\) does not pass the type constraint \(Str\) with OverloadedStr\=HASH\(.*?\)/, '... dies without overloading the string';
129 package OverloadBreaker;
132 has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
136 OverloadBreaker->new;
137 } qr/Attribute \(a_num\) does not pass the type constraint \(Int\) with \'7\.5\'/, '... this doesnt trip overload to break anymore ';
140 OverloadBreaker->new(a_num => 5);
141 } '... this works fine though';
147 package Test::Builder::Attribute;
150 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
151 sub build_foo { return "works" };
154 my $meta = Test::Builder::Attribute->meta;
155 my $foo_attr = $meta->get_attribute("foo");
157 ok($foo_attr->is_required, "foo is required");
158 ok($foo_attr->has_builder, "foo has builder");
159 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
161 my $instance = Test::Builder::Attribute->new;
162 is($instance->foo, 'works', "foo builder works");
169 package Test::LazyBuild::Attribute;
172 has 'foo' => ( lazy_build => 1, is => 'ro');
173 has '_foo' => ( lazy_build => 1, is => 'ro');
174 sub build_foo { return "works" };
175 sub _build_foo { return "works too" };
178 my $meta = Test::LazyBuild::Attribute->meta;
179 my $foo_attr = $meta->get_attribute("foo");
180 my $_foo_attr = $meta->get_attribute("_foo");
182 ok($foo_attr->is_lazy, "foo is lazy");
183 ok($foo_attr->is_required, "foo is required");
184 ok($foo_attr->is_lazy_build, "foo is lazy_build");
186 ok($foo_attr->has_clearer, "foo has clearer");
187 is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo");
189 ok($foo_attr->has_builder, "foo has builder");
190 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
192 ok($foo_attr->has_predicate, "foo has predicate");
193 is($foo_attr->predicate, "has_foo", ".. and it's named has_foo");
195 ok($_foo_attr->is_lazy, "_foo is lazy");
196 ok($_foo_attr->is_required, "_foo is required");
197 ok($_foo_attr->is_lazy_build, "_foo is lazy_build");
199 ok($_foo_attr->has_clearer, "_foo has clearer");
200 is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo");
202 ok($_foo_attr->has_builder, "_foo has builder");
203 is($_foo_attr->builder, "_build_foo", ".. and it's named _build_foo");
205 ok($_foo_attr->has_predicate, "_foo has predicate");
206 is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");
208 my $instance = Test::LazyBuild::Attribute->new;
209 ok(!$instance->has_foo, "noo foo value yet");
210 ok(!$instance->_has_foo, "noo _foo value yet");
211 is($instance->foo, 'works', "foo builder works");
212 is($instance->_foo, 'works too', "foo builder works too");