Commit | Line | Data |
ca01a97b |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
1a563243 |
6 | use Test::More tests => 57; |
ca01a97b |
7 | use Test::Exception; |
8 | |
9 | use Scalar::Util 'isweak'; |
10 | |
11 | BEGIN { |
12 | use_ok('Moose'); |
13 | } |
14 | |
15 | { |
16 | package Foo; |
ca01a97b |
17 | use Moose; |
18 | |
19 | eval { |
20 | has 'foo' => ( |
21 | accessor => 'foo', |
22 | ); |
23 | }; |
24 | ::ok(!$@, '... created the accessor method okay'); |
25 | |
26 | eval { |
27 | has 'lazy_foo' => ( |
28 | accessor => 'lazy_foo', |
29 | lazy => 1, |
30 | default => sub { 10 } |
31 | ); |
32 | }; |
33 | ::ok(!$@, '... created the lazy accessor method okay'); |
34 | |
35 | |
36 | eval { |
37 | has 'foo_required' => ( |
38 | accessor => 'foo_required', |
39 | required => 1, |
40 | ); |
41 | }; |
42 | ::ok(!$@, '... created the required accessor method okay'); |
43 | |
44 | eval { |
45 | has 'foo_int' => ( |
46 | accessor => 'foo_int', |
47 | isa => 'Int', |
48 | ); |
49 | }; |
50 | ::ok(!$@, '... created the accessor method with type constraint okay'); |
51 | |
52 | eval { |
53 | has 'foo_weak' => ( |
54 | accessor => 'foo_weak', |
55 | weak_ref => 1 |
56 | ); |
57 | }; |
58 | ::ok(!$@, '... created the accessor method with weak_ref okay'); |
1a563243 |
59 | |
60 | eval { |
61 | has 'foo_deref' => ( |
62 | accessor => 'foo_deref', |
63 | isa => 'ArrayRef', |
64 | auto_deref => 1, |
65 | ); |
66 | }; |
67 | ::ok(!$@, '... created the accessor method with auto_deref okay'); |
68 | |
69 | eval { |
70 | has 'foo_deref_ro' => ( |
71 | reader => 'foo_deref_ro', |
72 | isa => 'ArrayRef', |
73 | auto_deref => 1, |
74 | ); |
75 | }; |
76 | ::ok(!$@, '... created the reader method with auto_deref okay'); |
77 | |
78 | eval { |
79 | has 'foo_deref_hash' => ( |
80 | accessor => 'foo_deref_hash', |
81 | isa => 'HashRef', |
82 | auto_deref => 1, |
83 | ); |
84 | }; |
85 | ::ok(!$@, '... created the reader method with auto_deref okay'); |
ca01a97b |
86 | } |
87 | |
88 | { |
89 | my $foo = Foo->new(foo_required => 'required'); |
90 | isa_ok($foo, 'Foo'); |
91 | |
92 | # regular accessor |
93 | |
94 | can_ok($foo, 'foo'); |
95 | is($foo->foo(), undef, '... got an unset value'); |
96 | lives_ok { |
97 | $foo->foo(100); |
98 | } '... foo wrote successfully'; |
99 | is($foo->foo(), 100, '... got the correct set value'); |
100 | |
101 | ok(!isweak($foo->{foo}), '... it is not a weak reference'); |
102 | |
103 | # required writer |
104 | |
105 | dies_ok { |
106 | Foo->new; |
107 | } '... cannot create without the required attribute'; |
108 | |
109 | can_ok($foo, 'foo_required'); |
110 | is($foo->foo_required(), 'required', '... got an unset value'); |
111 | lives_ok { |
112 | $foo->foo_required(100); |
113 | } '... foo_required wrote successfully'; |
114 | is($foo->foo_required(), 100, '... got the correct set value'); |
115 | |
116 | dies_ok { |
117 | $foo->foo_required(undef); |
118 | } '... foo_required died successfully'; |
119 | |
120 | ok(!isweak($foo->{foo_required}), '... it is not a weak reference'); |
121 | |
122 | # lazy |
123 | |
124 | ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot'); |
125 | |
126 | can_ok($foo, 'lazy_foo'); |
127 | is($foo->lazy_foo(), 10, '... got an deferred value'); |
128 | |
129 | # with type constraint |
130 | |
131 | can_ok($foo, 'foo_int'); |
132 | is($foo->foo_int(), undef, '... got an unset value'); |
133 | lives_ok { |
134 | $foo->foo_int(100); |
135 | } '... foo_int wrote successfully'; |
136 | is($foo->foo_int(), 100, '... got the correct set value'); |
137 | |
138 | dies_ok { |
139 | $foo->foo_int("Foo"); |
140 | } '... foo_int died successfully'; |
141 | |
142 | ok(!isweak($foo->{foo_int}), '... it is not a weak reference'); |
143 | |
144 | # with weak_ref |
145 | |
146 | my $test = []; |
147 | |
148 | can_ok($foo, 'foo_weak'); |
149 | is($foo->foo_weak(), undef, '... got an unset value'); |
150 | lives_ok { |
151 | $foo->foo_weak($test); |
152 | } '... foo_weak wrote successfully'; |
153 | is($foo->foo_weak(), $test, '... got the correct set value'); |
154 | |
155 | ok(isweak($foo->{foo_weak}), '... it is a weak reference'); |
156 | |
1a563243 |
157 | can_ok( $foo, 'foo_deref'); |
158 | is( $foo->foo_deref(), undef, '... unset value'); |
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/ ] ); |
167 | } '... foo_deref wrote successfully'; |
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' ); |
177 | is( $foo->foo_deref_ro(), undef, "... unset value" ); |
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' ); |
189 | is( $foo->foo_deref_hash(), undef, "... unset value" ); |
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 } ); |
199 | } '... foo_deref_hash wrote successfully'; |
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 | |