Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8b9641b8 |
6 | use lib 't/lib'; |
7 | |
1c08fd75 |
8 | use Moose (); |
6197a68c |
9 | use Moose::Util::TypeConstraints; |
8b9641b8 |
10 | use NoInlineAttribute; |
b10dde3a |
11 | use Test::Fatal; |
1c08fd75 |
12 | use Test::More; |
13 | use Test::Moose; |
e3c07b19 |
14 | |
e3c07b19 |
15 | { |
1c08fd75 |
16 | my %handles = ( |
17 | option_accessor => 'accessor', |
18 | quantity => [ accessor => 'quantity' ], |
19 | clear_options => 'clear', |
20 | num_options => 'count', |
21 | delete_option => 'delete', |
22 | is_defined => 'defined', |
23 | options_elements => 'elements', |
24 | has_option => 'exists', |
25 | get_option => 'get', |
26 | has_no_options => 'is_empty', |
865faf6f |
27 | keys => 'keys', |
1399eab0 |
28 | values => 'values', |
1c08fd75 |
29 | key_value => 'kv', |
30 | set_option => 'set', |
e3c07b19 |
31 | ); |
85592815 |
32 | |
1c08fd75 |
33 | my $name = 'Foo1'; |
34 | |
35 | sub build_class { |
36 | my %attr = @_; |
37 | |
38 | my $class = Moose::Meta::Class->create( |
39 | $name++, |
40 | superclasses => ['Moose::Object'], |
41 | ); |
42 | |
8b9641b8 |
43 | my @traits = 'Hash'; |
44 | push @traits, 'NoInlineAttribute' |
45 | if delete $attr{no_inline}; |
46 | |
1c08fd75 |
47 | $class->add_attribute( |
48 | options => ( |
8b9641b8 |
49 | traits => \@traits, |
2d779ce6 |
50 | is => 'rw', |
1c08fd75 |
51 | isa => 'HashRef[Str]', |
52 | default => sub { {} }, |
53 | handles => \%handles, |
54 | clearer => '_clear_options', |
55 | %attr, |
56 | ), |
57 | ); |
58 | |
59 | return ( $class->name, \%handles ); |
60 | } |
d50fc84a |
61 | } |
e3c07b19 |
62 | |
1c08fd75 |
63 | { |
64 | run_tests(build_class); |
65 | run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) ); |
cf0da4e2 |
66 | run_tests( build_class( trigger => sub { } ) ); |
8b9641b8 |
67 | run_tests( build_class( no_inline => 1 ) ); |
6197a68c |
68 | |
69 | # Will force the inlining code to check the entire hashref when it is modified. |
70 | subtype 'MyHashRef', as 'HashRef[Str]', where { 1 }; |
71 | |
72 | run_tests( build_class( isa => 'MyHashRef' ) ); |
73 | |
74 | coerce 'MyHashRef', from 'HashRef', via { $_ }; |
75 | |
76 | run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) ); |
d50fc84a |
77 | } |
59de9de4 |
78 | |
1c08fd75 |
79 | sub run_tests { |
80 | my ( $class, $handles ) = @_; |
59de9de4 |
81 | |
1c08fd75 |
82 | can_ok( $class, $_ ) for sort keys %{$handles}; |
e3c07b19 |
83 | |
1c08fd75 |
84 | with_immutable { |
85 | my $obj = $class->new( options => {} ); |
e3c07b19 |
86 | |
1c08fd75 |
87 | ok( $obj->has_no_options, '... we have no options' ); |
88 | is( $obj->num_options, 0, '... we have no options' ); |
89 | |
90 | is_deeply( $obj->options, {}, '... no options yet' ); |
91 | ok( !$obj->has_option('foo'), '... we have no foo option' ); |
92 | |
b10dde3a |
93 | is( exception { |
7f5ec80d |
94 | is( |
95 | $obj->set_option( foo => 'bar' ), |
96 | 'bar', |
97 | 'set return single new value in scalar context' |
98 | ); |
b10dde3a |
99 | }, undef, '... set the option okay' ); |
1c08fd75 |
100 | |
101 | ok( $obj->is_defined('foo'), '... foo is defined' ); |
102 | |
103 | ok( !$obj->has_no_options, '... we have options' ); |
104 | is( $obj->num_options, 1, '... we have 1 option(s)' ); |
105 | ok( $obj->has_option('foo'), '... we have a foo option' ); |
106 | is_deeply( $obj->options, { foo => 'bar' }, '... got options now' ); |
107 | |
b10dde3a |
108 | is( exception { |
1c08fd75 |
109 | $obj->set_option( bar => 'baz' ); |
b10dde3a |
110 | }, undef, '... set the option okay' ); |
1c08fd75 |
111 | |
112 | is( $obj->num_options, 2, '... we have 2 option(s)' ); |
113 | is_deeply( |
114 | $obj->options, { foo => 'bar', bar => 'baz' }, |
115 | '... got more options now' |
116 | ); |
117 | |
118 | is( $obj->get_option('foo'), 'bar', '... got the right option' ); |
119 | |
120 | is_deeply( |
121 | [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)], |
122 | "get multiple options at once" |
123 | ); |
124 | |
125 | is( |
126 | scalar( $obj->get_option(qw( foo bar)) ), "baz", |
127 | '... got last option in scalar context' |
128 | ); |
129 | |
b10dde3a |
130 | is( exception { |
1c08fd75 |
131 | $obj->set_option( oink => "blah", xxy => "flop" ); |
b10dde3a |
132 | }, undef, '... set the option okay' ); |
1c08fd75 |
133 | |
134 | is( $obj->num_options, 4, "4 options" ); |
135 | is_deeply( |
136 | [ $obj->get_option(qw(foo bar oink xxy)) ], |
137 | [qw(bar baz blah flop)], "get multiple options at once" |
138 | ); |
139 | |
b10dde3a |
140 | is( exception { |
7f5ec80d |
141 | is( scalar $obj->delete_option('bar'), 'baz', |
142 | 'delete returns deleted value' ); |
b10dde3a |
143 | }, undef, '... deleted the option okay' ); |
1c08fd75 |
144 | |
b10dde3a |
145 | is( exception { |
7f5ec80d |
146 | is_deeply( |
147 | [ $obj->delete_option( 'oink', 'xxy' ) ], |
148 | [ 'blah', 'flop' ], |
149 | 'delete returns all deleted values in list context' |
150 | ); |
b10dde3a |
151 | }, undef, '... deleted multiple option okay' ); |
1c08fd75 |
152 | |
153 | is( $obj->num_options, 1, '... we have 1 option(s)' ); |
154 | is_deeply( |
155 | $obj->options, { foo => 'bar' }, |
156 | '... got more options now' |
157 | ); |
158 | |
159 | $obj->clear_options; |
160 | |
161 | is_deeply( $obj->options, {}, "... cleared options" ); |
162 | |
b10dde3a |
163 | is( exception { |
1c08fd75 |
164 | $obj->quantity(4); |
b10dde3a |
165 | }, undef, '... options added okay with defaults' ); |
1c08fd75 |
166 | |
167 | is( $obj->quantity, 4, 'reader part of curried accessor works' ); |
168 | |
2d779ce6 |
169 | is( |
170 | $obj->option_accessor('quantity'), 4, |
171 | 'accessor as reader' |
172 | ); |
173 | |
1c08fd75 |
174 | is_deeply( |
175 | $obj->options, { quantity => 4 }, |
176 | '... returns what we expect' |
177 | ); |
178 | |
5dcac913 |
179 | $obj->option_accessor( size => 42 ); |
2d779ce6 |
180 | |
65594cb4 |
181 | like( |
182 | exception { |
183 | $obj->option_accessor; |
184 | }, |
185 | qr/Cannot call accessor without at least 1 argument/, |
186 | 'error when calling accessor with no arguments' |
187 | ); |
43433317 |
188 | |
2d779ce6 |
189 | is_deeply( |
5dcac913 |
190 | $obj->options, { quantity => 4, size => 42 }, |
2d779ce6 |
191 | 'accessor as writer' |
192 | ); |
193 | |
b10dde3a |
194 | is( exception { |
1c08fd75 |
195 | $class->new( options => { foo => 'BAR' } ); |
b10dde3a |
196 | }, undef, '... good constructor params' ); |
1c08fd75 |
197 | |
b10dde3a |
198 | isnt( exception { |
1c08fd75 |
199 | $obj->set_option( bar => {} ); |
b10dde3a |
200 | }, undef, '... could not add a hash ref where an string is expected' ); |
1c08fd75 |
201 | |
b10dde3a |
202 | isnt( exception { |
1c08fd75 |
203 | $class->new( options => { foo => [] } ); |
b10dde3a |
204 | }, undef, '... bad constructor params' ); |
1c08fd75 |
205 | |
2d779ce6 |
206 | $obj->options( {} ); |
207 | |
7f5ec80d |
208 | is_deeply( |
209 | [ $obj->set_option( oink => "blah", xxy => "flop" ) ], |
210 | [ 'blah', 'flop' ], |
211 | 'set returns newly set values in order of keys provided' |
212 | ); |
213 | |
1399eab0 |
214 | is_deeply( |
215 | [ sort $obj->keys ], |
2d779ce6 |
216 | [ 'oink', 'xxy' ], |
1399eab0 |
217 | 'keys returns expected keys' |
218 | ); |
219 | |
220 | is_deeply( |
221 | [ sort $obj->values ], |
2d779ce6 |
222 | [ 'blah', 'flop' ], |
1399eab0 |
223 | 'values returns expected values' |
224 | ); |
225 | |
1c08fd75 |
226 | my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value; |
227 | is_deeply( |
228 | \@key_value, |
229 | [ |
230 | sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ], |
1c08fd75 |
231 | [ 'oink', 'blah' ] |
232 | ], |
233 | '... got the right key value pairs' |
234 | ) |
235 | or do { |
236 | require Data::Dumper; |
237 | diag( Data::Dumper::Dumper( \@key_value ) ); |
238 | }; |
239 | |
240 | my %options_elements = $obj->options_elements; |
241 | is_deeply( |
242 | \%options_elements, { |
243 | 'oink' => 'blah', |
1c08fd75 |
244 | 'xxy' => 'flop' |
245 | }, |
246 | '... got the right hash elements' |
247 | ); |
248 | |
249 | if ( $class->meta->get_attribute('options')->is_lazy ) { |
250 | my $obj = $class->new; |
251 | |
252 | $obj->set_option( y => 2 ); |
253 | |
254 | is_deeply( |
255 | $obj->options, { x => 1, y => 2 }, |
256 | 'set_option with lazy default' |
257 | ); |
258 | |
259 | $obj->_clear_options; |
260 | |
261 | ok( |
262 | $obj->has_option('x'), |
263 | 'key for x exists - lazy default' |
264 | ); |
265 | |
266 | $obj->_clear_options; |
267 | |
268 | ok( |
269 | $obj->is_defined('x'), |
270 | 'key for x is defined - lazy default' |
271 | ); |
272 | |
273 | $obj->_clear_options; |
274 | |
275 | is_deeply( |
276 | [ $obj->key_value ], |
277 | [ [ x => 1 ] ], |
278 | 'kv returns lazy default' |
279 | ); |
865faf6f |
280 | |
865faf6f |
281 | $obj->_clear_options; |
282 | |
283 | $obj->option_accessor( y => 2 ); |
284 | |
285 | is_deeply( |
286 | [ sort $obj->keys ], |
287 | [ 'x', 'y' ], |
288 | 'accessor triggers lazy default generator' |
289 | ); |
1c08fd75 |
290 | } |
291 | } |
292 | $class; |
d50fc84a |
293 | } |
a28e50e4 |
294 | |
295 | done_testing; |