2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
10 $TODO = q{Mouse is not yet completed};
16 package Test::Attribute::Inline::Documentation;
21 The 'foo' attribute is my favorite
22 attribute in the whole wide world.
28 my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');
30 ok($foo_attr->has_documentation, '... the foo has docs');
31 is($foo_attr->documentation,
33 The 'foo' attribute is my favorite
34 attribute in the whole wide world.
36 '... got the foo docs');
41 package Test::For::Lazy::TypeConstraint;
43 use Mouse::Util::TypeConstraints;
45 has 'bad_lazy_attr' => (
49 default => sub { "test" },
52 has 'good_lazy_attr' => (
56 default => sub { [] },
61 my $test = Test::For::Lazy::TypeConstraint->new;
62 isa_ok($test, 'Test::For::Lazy::TypeConstraint');
66 } '... this does not work';
69 $test->good_lazy_attr;
70 } '... this does not work';
75 package Test::Arrayref::Attributes;
78 has [qw(foo bar baz)] => (
84 my $test = Test::Arrayref::Attributes->new;
85 isa_ok($test, 'Test::Arrayref::Attributes');
86 can_ok($test, qw(foo bar baz));
92 package Test::Arrayref::RoleAttributes::Role;
95 has [qw(foo bar baz)] => (
101 package Test::Arrayref::RoleAttributes;
103 with 'Test::Arrayref::RoleAttributes::Role';
106 my $test = Test::Arrayref::RoleAttributes->new;
107 isa_ok($test, 'Test::Arrayref::RoleAttributes');
108 can_ok($test, qw(foo bar baz));
114 package Test::UndefDefault::Attributes;
120 default => sub { return }
126 Test::UndefDefault::Attributes->new;
127 } '... default must return a value which passes the type constraint';
133 package OverloadedStr;
135 use overload '""' => sub { 'this is *not* a string' };
137 has 'a_str' => ( isa => 'Str' , is => 'rw' );
140 my $moose_obj = OverloadedStr->new;
142 is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
143 ok($moose_obj, 'this is a *not* a string');
146 $moose_obj->a_str( $moose_obj )
147 } qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' with value OverloadedStr=HASH\(0x.+?\)/,
148 '... dies without overloading the string';
154 package OverloadBreaker;
157 has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
161 OverloadBreaker->new;
162 } qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' with value 7\.5/,
163 '... this doesnt trip overload to break anymore ';
166 OverloadBreaker->new(a_num => 5);
167 } '... this works fine though';
173 package Test::Builder::Attribute;
176 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
177 sub build_foo { return "works" };
180 my $meta = Test::Builder::Attribute->meta;
181 my $foo_attr = $meta->get_attribute("foo");
183 ok($foo_attr->is_required, "foo is required");
184 ok($foo_attr->has_builder, "foo has builder");
185 is($foo_attr->builder, "build_foo", ".. and it's named build_foo");
187 my $instance = Test::Builder::Attribute->new;
188 is($instance->foo, 'works', "foo builder works");
193 package Test::Builder::Attribute::Broken;
196 has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro');
200 Test::Builder::Attribute::Broken->new;
201 } '... no builder, wtf';
207 package Test::LazyBuild::Attribute;
210 has 'foo' => ( lazy_build => 1, is => 'ro');
211 has '_foo' => ( lazy_build => 1, is => 'ro');
212 has 'fool' => ( lazy_build => 1, is => 'ro');
213 sub _build_foo { return "works" };
214 sub _build__foo { return "works too" };
217 my $meta = Test::LazyBuild::Attribute->meta;
218 my $foo_attr = $meta->get_attribute("foo");
219 my $_foo_attr = $meta->get_attribute("_foo");
221 ok($foo_attr->is_lazy, "foo is lazy");
222 ok($foo_attr->is_lazy_build, "foo is lazy_build");
224 ok($foo_attr->has_clearer, "foo has clearer");
225 is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo");
227 ok($foo_attr->has_builder, "foo has builder");
228 is($foo_attr->builder, "_build_foo", ".. and it's named build_foo");
230 ok($foo_attr->has_predicate, "foo has predicate");
231 is($foo_attr->predicate, "has_foo", ".. and it's named has_foo");
233 ok($_foo_attr->is_lazy, "_foo is lazy");
234 ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required");
235 ok($_foo_attr->is_lazy_build, "_foo is lazy_build");
237 ok($_foo_attr->has_clearer, "_foo has clearer");
238 is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo");
240 ok($_foo_attr->has_builder, "_foo has builder");
241 is($_foo_attr->builder, "_build__foo", ".. and it's named _build_foo");
243 ok($_foo_attr->has_predicate, "_foo has predicate");
244 is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");
246 my $instance = Test::LazyBuild::Attribute->new;
247 ok(!$instance->has_foo, "noo foo value yet");
248 ok(!$instance->_has_foo, "noo _foo value yet");
249 is($instance->foo, 'works', "foo builder works");
250 is($instance->_foo, 'works too', "foo builder works too");
251 throws_ok { $instance->fool }
252 qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/,
253 "Correct error when a builder method is not present";
258 package OutOfClassTest;
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';