Commit | Line | Data |
ca01a97b |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7ff56534 |
6 | use Test::More tests => 57; |
ca01a97b |
7 | use Test::Exception; |
8 | |
9 | use Scalar::Util 'isweak'; |
10 | |
7ff56534 |
11 | |
ca01a97b |
12 | |
13 | { |
14 | package Foo; |
ca01a97b |
15 | use Moose; |
d03bd989 |
16 | |
ca01a97b |
17 | eval { |
18 | has 'foo' => ( |
19 | accessor => 'foo', |
20 | ); |
21 | }; |
1808c2da |
22 | ::ok(!$@, 'created the accessor method okay'); |
d03bd989 |
23 | |
ca01a97b |
24 | eval { |
25 | has 'lazy_foo' => ( |
d03bd989 |
26 | accessor => 'lazy_foo', |
27 | lazy => 1, |
ca01a97b |
28 | default => sub { 10 } |
29 | ); |
30 | }; |
1808c2da |
31 | ::ok(!$@, 'created the lazy accessor method okay'); |
d03bd989 |
32 | |
ca01a97b |
33 | |
34 | eval { |
35 | has 'foo_required' => ( |
36 | accessor => 'foo_required', |
37 | required => 1, |
38 | ); |
39 | }; |
1808c2da |
40 | ::ok(!$@, 'created the required accessor method okay'); |
ca01a97b |
41 | |
42 | eval { |
43 | has 'foo_int' => ( |
44 | accessor => 'foo_int', |
45 | isa => 'Int', |
46 | ); |
47 | }; |
1808c2da |
48 | ::ok(!$@, 'created the accessor method with type constraint okay'); |
d03bd989 |
49 | |
ca01a97b |
50 | eval { |
51 | has 'foo_weak' => ( |
52 | accessor => 'foo_weak', |
53 | weak_ref => 1 |
54 | ); |
55 | }; |
1808c2da |
56 | ::ok(!$@, 'created the accessor method with weak_ref okay'); |
1a563243 |
57 | |
58 | eval { |
59 | has 'foo_deref' => ( |
60 | accessor => 'foo_deref', |
61 | isa => 'ArrayRef', |
62 | auto_deref => 1, |
63 | ); |
64 | }; |
1808c2da |
65 | ::ok(!$@, 'created the accessor method with auto_deref okay'); |
1a563243 |
66 | |
67 | eval { |
68 | has 'foo_deref_ro' => ( |
69 | reader => 'foo_deref_ro', |
70 | isa => 'ArrayRef', |
71 | auto_deref => 1, |
72 | ); |
73 | }; |
1808c2da |
74 | ::ok(!$@, 'created the reader method with auto_deref okay'); |
1a563243 |
75 | |
76 | eval { |
77 | has 'foo_deref_hash' => ( |
78 | accessor => 'foo_deref_hash', |
79 | isa => 'HashRef', |
80 | auto_deref => 1, |
81 | ); |
82 | }; |
1808c2da |
83 | ::ok(!$@, 'created the reader method with auto_deref okay'); |
ca01a97b |
84 | } |
85 | |
86 | { |
87 | my $foo = Foo->new(foo_required => 'required'); |
88 | isa_ok($foo, 'Foo'); |
89 | |
90 | # regular accessor |
91 | |
92 | can_ok($foo, 'foo'); |
1808c2da |
93 | is($foo->foo(), undef, 'got an unset value'); |
ca01a97b |
94 | lives_ok { |
95 | $foo->foo(100); |
1808c2da |
96 | } 'foo wrote successfully'; |
97 | is($foo->foo(), 100, 'got the correct set value'); |
d03bd989 |
98 | |
1808c2da |
99 | ok(!isweak($foo->{foo}), 'it is not a weak reference'); |
d03bd989 |
100 | |
ca01a97b |
101 | # required writer |
d03bd989 |
102 | |
ca01a97b |
103 | dies_ok { |
104 | Foo->new; |
1808c2da |
105 | } 'cannot create without the required attribute'; |
ca01a97b |
106 | |
107 | can_ok($foo, 'foo_required'); |
1808c2da |
108 | is($foo->foo_required(), 'required', 'got an unset value'); |
ca01a97b |
109 | lives_ok { |
110 | $foo->foo_required(100); |
1808c2da |
111 | } 'foo_required wrote successfully'; |
112 | is($foo->foo_required(), 100, 'got the correct set value'); |
d03bd989 |
113 | |
b6af66f8 |
114 | lives_ok { |
ca01a97b |
115 | $foo->foo_required(undef); |
1808c2da |
116 | } 'foo_required did not die with undef'; |
b6af66f8 |
117 | |
118 | is($foo->foo_required, undef, "value is undef"); |
ca01a97b |
119 | |
1808c2da |
120 | ok(!isweak($foo->{foo_required}), 'it is not a weak reference'); |
d03bd989 |
121 | |
ca01a97b |
122 | # lazy |
d03bd989 |
123 | |
1808c2da |
124 | ok(!exists($foo->{lazy_foo}), 'no value in lazy_foo slot'); |
d03bd989 |
125 | |
ca01a97b |
126 | can_ok($foo, 'lazy_foo'); |
1808c2da |
127 | is($foo->lazy_foo(), 10, 'got an deferred value'); |
d03bd989 |
128 | |
ca01a97b |
129 | # with type constraint |
d03bd989 |
130 | |
ca01a97b |
131 | can_ok($foo, 'foo_int'); |
1808c2da |
132 | is($foo->foo_int(), undef, 'got an unset value'); |
ca01a97b |
133 | lives_ok { |
134 | $foo->foo_int(100); |
1808c2da |
135 | } 'foo_int wrote successfully'; |
136 | is($foo->foo_int(), 100, 'got the correct set value'); |
d03bd989 |
137 | |
ca01a97b |
138 | dies_ok { |
139 | $foo->foo_int("Foo"); |
1808c2da |
140 | } 'foo_int died successfully'; |
d03bd989 |
141 | |
1808c2da |
142 | ok(!isweak($foo->{foo_int}), 'it is not a weak reference'); |
d03bd989 |
143 | |
ca01a97b |
144 | # with weak_ref |
d03bd989 |
145 | |
ca01a97b |
146 | my $test = []; |
d03bd989 |
147 | |
ca01a97b |
148 | can_ok($foo, 'foo_weak'); |
1808c2da |
149 | is($foo->foo_weak(), undef, 'got an unset value'); |
ca01a97b |
150 | lives_ok { |
151 | $foo->foo_weak($test); |
1808c2da |
152 | } 'foo_weak wrote successfully'; |
153 | is($foo->foo_weak(), $test, 'got the correct set value'); |
d03bd989 |
154 | |
1808c2da |
155 | ok(isweak($foo->{foo_weak}), 'it is a weak reference'); |
ca01a97b |
156 | |
1a563243 |
157 | can_ok( $foo, 'foo_deref'); |
1808c2da |
158 | is_deeply( [$foo->foo_deref()], [], 'default default value'); |
1a563243 |
159 | my @list; |
160 | lives_ok { |
161 | @list = $foo->foo_deref(); |
162 | } "... doesn't deref undef value"; |
163 | is_deeply( \@list, [], "returns empty list in list context"); |
164 | |
165 | lives_ok { |
166 | $foo->foo_deref( [ qw/foo bar gorch/ ] ); |
1808c2da |
167 | } 'foo_deref wrote successfully'; |
1a563243 |
168 | |
169 | is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" ); |
170 | is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" ); |
171 | |
172 | is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" ); |
173 | is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" ); |
174 | |
175 | |
176 | can_ok( $foo, 'foo_deref' ); |
3f7376b0 |
177 | is_deeply( [$foo->foo_deref_ro()], [], "... default default value" ); |
1a563243 |
178 | |
179 | dies_ok { |
180 | $foo->foo_deref_ro( [] ); |
181 | } "... read only"; |
182 | |
183 | $foo->{foo_deref_ro} = [qw/la la la/]; |
184 | |
185 | is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" ); |
186 | is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" ); |
187 | |
188 | can_ok( $foo, 'foo_deref_hash' ); |
3f7376b0 |
189 | is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" ); |
1a563243 |
190 | |
191 | my %hash; |
192 | lives_ok { |
193 | %hash = $foo->foo_deref_hash(); |
194 | } "... doesn't deref undef value"; |
195 | is_deeply( \%hash, {}, "returns empty list in list context"); |
196 | |
197 | lives_ok { |
198 | $foo->foo_deref_hash( { foo => 1, bar => 2 } ); |
1808c2da |
199 | } 'foo_deref_hash wrote successfully'; |
1a563243 |
200 | |
201 | is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" ); |
202 | |
203 | %hash = $foo->foo_deref_hash; |
204 | is_deeply( \%hash, { foo => 1, bar => 2 }, "list context"); |
ca01a97b |
205 | } |
206 | |
207 | |
208 | |