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