Regenerate test files
[gitmo/Mouse.git] / t / 020_attributes / 012_misc_attribute_tests.t
1 #!/usr/bin/perl
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;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 $TODO = q{Mouse is not yet completed};
11 use Test::Exception;
12
13
14 {
15     {
16         package Test::Attribute::Inline::Documentation;
17         use Mouse;
18
19         has 'foo' => (
20             documentation => q{
21                 The 'foo' attribute is my favorite
22                 attribute in the whole wide world.
23             },
24             is => 'bare',
25         );
26     }
27
28     my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');
29
30     ok($foo_attr->has_documentation, '... the foo has docs');
31     is($foo_attr->documentation,
32             q{
33                 The 'foo' attribute is my favorite
34                 attribute in the whole wide world.
35             },
36     '... got the foo docs');
37 }
38
39 {
40     {
41         package Test::For::Lazy::TypeConstraint;
42         use Mouse;
43         use Mouse::Util::TypeConstraints;
44
45         has 'bad_lazy_attr' => (
46             is => 'rw',
47             isa => 'ArrayRef',
48             lazy => 1,
49             default => sub { "test" },
50         );
51
52         has 'good_lazy_attr' => (
53             is => 'rw',
54             isa => 'ArrayRef',
55             lazy => 1,
56             default => sub { [] },
57         );
58
59     }
60
61     my $test = Test::For::Lazy::TypeConstraint->new;
62     isa_ok($test, 'Test::For::Lazy::TypeConstraint');
63
64     dies_ok {
65         $test->bad_lazy_attr;
66     } '... this does not work';
67
68     lives_ok {
69         $test->good_lazy_attr;
70     } '... this does not work';
71 }
72
73 {
74     {
75         package Test::Arrayref::Attributes;
76         use Mouse;
77
78         has [qw(foo bar baz)] => (
79             is => 'rw',
80         );
81
82     }
83
84     my $test = Test::Arrayref::Attributes->new;
85     isa_ok($test, 'Test::Arrayref::Attributes');
86     can_ok($test, qw(foo bar baz));
87
88 }
89
90 {
91     {
92         package Test::Arrayref::RoleAttributes::Role;
93         use Mouse::Role;
94
95         has [qw(foo bar baz)] => (
96             is => 'rw',
97         );
98
99     }
100     {
101         package Test::Arrayref::RoleAttributes;
102         use Mouse;
103         with 'Test::Arrayref::RoleAttributes::Role';
104     }
105
106     my $test = Test::Arrayref::RoleAttributes->new;
107     isa_ok($test, 'Test::Arrayref::RoleAttributes');
108     can_ok($test, qw(foo bar baz));
109
110 }
111
112 {
113     {
114         package Test::UndefDefault::Attributes;
115         use Mouse;
116
117         has 'foo' => (
118             is      => 'ro',
119             isa     => 'Str',
120             default => sub { return }
121         );
122
123     }
124
125     dies_ok {
126         Test::UndefDefault::Attributes->new;
127     } '... default must return a value which passes the type constraint';
128
129 }
130
131 {
132     {
133         package OverloadedStr;
134         use Mouse;
135         use overload '""' => sub { 'this is *not* a string' };
136
137         has 'a_str' => ( isa => 'Str' , is => 'rw' );
138     }
139
140     my $moose_obj = OverloadedStr->new;
141
142     is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
143     ok($moose_obj, 'this is a *not* a string');
144
145     throws_ok {
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';
149
150 }
151
152 {
153     {
154         package OverloadBreaker;
155         use Mouse;
156
157         has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
158     }
159
160     throws_ok {
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 ';
164
165     lives_ok {
166         OverloadBreaker->new(a_num => 5);
167     } '... this works fine though';
168
169 }
170
171 {
172     {
173       package Test::Builder::Attribute;
174         use Mouse;
175
176         has 'foo'  => ( required => 1, builder => 'build_foo', is => 'ro');
177         sub build_foo { return "works" };
178     }
179
180     my $meta = Test::Builder::Attribute->meta;
181     my $foo_attr  = $meta->get_attribute("foo");
182
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");
186
187     my $instance = Test::Builder::Attribute->new;
188     is($instance->foo, 'works', "foo builder works");
189 }
190
191 {
192     {
193         package Test::Builder::Attribute::Broken;
194         use Mouse;
195
196         has 'foo'  => ( required => 1, builder => 'build_foo', is => 'ro');
197     }
198
199     dies_ok {
200         Test::Builder::Attribute::Broken->new;
201     } '... no builder, wtf';
202 }
203
204
205 {
206     {
207       package Test::LazyBuild::Attribute;
208         use Mouse;
209
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" };
215     }
216
217     my $meta = Test::LazyBuild::Attribute->meta;
218     my $foo_attr  = $meta->get_attribute("foo");
219     my $_foo_attr = $meta->get_attribute("_foo");
220
221     ok($foo_attr->is_lazy, "foo is lazy");
222     ok($foo_attr->is_lazy_build, "foo is lazy_build");
223
224     ok($foo_attr->has_clearer, "foo has clearer");
225     is($foo_attr->clearer, "clear_foo",  ".. and it's named clear_foo");
226
227     ok($foo_attr->has_builder, "foo has builder");
228     is($foo_attr->builder, "_build_foo",  ".. and it's named build_foo");
229
230     ok($foo_attr->has_predicate, "foo has predicate");
231     is($foo_attr->predicate, "has_foo",  ".. and it's named has_foo");
232
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");
236
237     ok($_foo_attr->has_clearer, "_foo has clearer");
238     is($_foo_attr->clearer, "_clear_foo",  ".. and it's named _clear_foo");
239
240     ok($_foo_attr->has_builder, "_foo has builder");
241     is($_foo_attr->builder, "_build__foo",  ".. and it's named _build_foo");
242
243     ok($_foo_attr->has_predicate, "_foo has predicate");
244     is($_foo_attr->predicate, "_has_foo",  ".. and it's named _has_foo");
245
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";
254
255 }
256
257 {
258     package OutOfClassTest;
259
260     use Mouse;
261 }
262
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';
265
266 ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call');
267 ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can');
268
269
270 {
271     {
272         package Foo;
273         use Mouse;
274
275         ::throws_ok { has 'foo' => ( 'ro', isa => 'Str' ) }
276             qr/^Usage/, 'has throws error with odd number of attribute options';
277     }
278
279 }
280
281 done_testing;