Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a7821be5 |
6 | use Moose (); |
bbeb5798 |
7 | use Moose::Util::TypeConstraints; |
a28e50e4 |
8 | use Test::More; |
e3c07b19 |
9 | use Test::Exception; |
37ecda0d |
10 | use Test::Moose; |
d50fc84a |
11 | |
e3c07b19 |
12 | { |
a7821be5 |
13 | my %handles = ( |
14 | count => 'count', |
15 | elements => 'elements', |
16 | is_empty => 'is_empty', |
17 | push => 'push', |
18 | push_curried => |
19 | [ push => 42, 84 ], |
20 | unshift => 'unshift', |
21 | unshift_curried => |
22 | [ unshift => 42, 84 ], |
23 | pop => 'pop', |
24 | shift => 'shift', |
25 | get => 'get', |
26 | get_curried => [ get => 1 ], |
27 | set => 'set', |
28 | set_curried_1 => [ set => 1 ], |
29 | set_curried_2 => [ set => ( 1, 98 ) ], |
30 | accessor => 'accessor', |
31 | accessor_curried_1 => [ accessor => 1 ], |
32 | accessor_curried_2 => [ accessor => ( 1, 90 ) ], |
33 | clear => 'clear', |
34 | delete => 'delete', |
35 | delete_curried => [ delete => 1 ], |
36 | insert => 'insert', |
37 | insert_curried => [ insert => ( 1, 101 ) ], |
38 | splice => 'splice', |
39 | splice_curried_1 => [ splice => 1 ], |
40 | splice_curried_2 => [ splice => 1, 2 ], |
41 | splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ], |
42 | sort => 'sort', |
43 | sort_curried => [ sort => ( sub { $_[1] <=> $_[0] } ) ], |
44 | sort_in_place => 'sort_in_place', |
45 | sort_in_place_curried => |
46 | [ sort_in_place => ( sub { $_[1] <=> $_[0] } ) ], |
47 | map => 'map', |
48 | map_curried => [ map => ( sub { $_ + 1 } ) ], |
49 | grep => 'grep', |
50 | grep_curried => [ grep => ( sub { $_ < 5 } ) ], |
51 | first => 'first', |
52 | first_curried => [ first => ( sub { $_ % 2 } ) ], |
53 | join => 'join', |
54 | join_curried => [ join => '-' ], |
55 | shuffle => 'shuffle', |
56 | uniq => 'uniq', |
57 | reduce => 'reduce', |
58 | reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ], |
59 | natatime => 'natatime', |
60 | natatime_curried => [ natatime => 2 ], |
e3c07b19 |
61 | ); |
a7821be5 |
62 | |
63 | my $name = 'Foo1'; |
64 | |
65 | sub build_class { |
66 | my %attr = @_; |
67 | |
68 | my $class = Moose::Meta::Class->create( |
69 | $name++, |
70 | superclasses => ['Moose::Object'], |
71 | ); |
72 | |
73 | $class->add_attribute( |
74 | _values => ( |
75 | traits => ['Array'], |
76 | is => 'rw', |
77 | isa => 'ArrayRef[Int]', |
78 | default => sub { [] }, |
79 | handles => \%handles, |
80 | clearer => '_clear_values', |
81 | %attr, |
82 | ), |
83 | ); |
84 | |
85 | return ( $class->name, \%handles ); |
86 | } |
e3c07b19 |
87 | } |
88 | |
862b6081 |
89 | { |
a7821be5 |
90 | run_tests(build_class); |
91 | run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) ); |
bbeb5798 |
92 | |
93 | # Will force the inlining code to check the entire arrayref when it is modified. |
94 | subtype 'MyArrayRef', as 'ArrayRef', where { 1 }; |
95 | |
96 | run_tests( build_class( isa => 'MyArrayRef' ) ); |
97 | |
98 | coerce 'MyArrayRef', from 'ArrayRef', via { $_ }; |
99 | |
100 | run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) ); |
a7821be5 |
101 | } |
e3c07b19 |
102 | |
a7821be5 |
103 | sub run_tests { |
104 | my ( $class, $handles ) = @_; |
e3c07b19 |
105 | |
a7821be5 |
106 | can_ok( $class, $_ ) for sort keys %{$handles}; |
e3c07b19 |
107 | |
a7821be5 |
108 | with_immutable { |
109 | my $obj = $class->new( _values => [ 10, 12, 42 ] ); |
e3c07b19 |
110 | |
a7821be5 |
111 | is_deeply( |
112 | $obj->_values, [ 10, 12, 42 ], |
113 | 'values can be set in constructor' |
114 | ); |
e3c07b19 |
115 | |
a7821be5 |
116 | ok( !$obj->is_empty, 'values is not empty' ); |
117 | is( $obj->count, 3, 'count returns 3' ); |
e3c07b19 |
118 | |
a7821be5 |
119 | throws_ok { $obj->count(22) } |
120 | qr/Cannot call count with any arguments/, |
e3181911 |
121 | 'throws an error when passing an argument passed to count'; |
e3c07b19 |
122 | |
a7821be5 |
123 | lives_ok { $obj->push( 1, 2, 3 ) } |
124 | 'pushed three new values and lived'; |
e3c07b19 |
125 | |
a7821be5 |
126 | lives_ok { $obj->push() } 'call to push without arguments lives'; |
e3c07b19 |
127 | |
a7821be5 |
128 | lives_ok { $obj->unshift( 101, 22 ) } |
129 | 'unshifted two values and lived'; |
e3c07b19 |
130 | |
e32b7489 |
131 | is_deeply( |
132 | $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ], |
133 | 'unshift changed the value of the array in the object' |
134 | ); |
135 | |
a7821be5 |
136 | lives_ok { $obj->unshift() } |
137 | 'call to unshift without arguments lives'; |
e3c07b19 |
138 | |
a7821be5 |
139 | is( $obj->pop, 3, 'pop returns the last value in the array' ); |
910684ee |
140 | |
a7821be5 |
141 | is_deeply( |
142 | $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ], |
143 | 'pop changed the value of the array in the object' |
144 | ); |
910684ee |
145 | |
a7821be5 |
146 | throws_ok { $obj->pop(42) } |
147 | qr/Cannot call pop with any arguments/, |
148 | 'call to pop with arguments dies'; |
910684ee |
149 | |
a7821be5 |
150 | is( $obj->shift, 101, 'shift returns the first value' ); |
910684ee |
151 | |
a7821be5 |
152 | throws_ok { $obj->shift(42) } |
153 | qr/Cannot call shift with any arguments/, |
154 | 'call to shift with arguments dies'; |
e3c07b19 |
155 | |
a7821be5 |
156 | is_deeply( |
157 | $obj->_values, [ 22, 10, 12, 42, 1, 2 ], |
158 | 'shift changed the value of the array in the object' |
159 | ); |
e3c07b19 |
160 | |
a7821be5 |
161 | is_deeply( |
162 | [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ], |
163 | 'call to elements returns values as a list' |
164 | ); |
e3c07b19 |
165 | |
a7821be5 |
166 | throws_ok { $obj->elements(22) } |
167 | qr/Cannot call elements with any arguments/, |
e3181911 |
168 | 'throws an error when passing an argument passed to elements'; |
e3c07b19 |
169 | |
a7821be5 |
170 | $obj->_values( [ 1, 2, 3 ] ); |
e3c07b19 |
171 | |
a7821be5 |
172 | is( $obj->get(0), 1, 'get values at index 0' ); |
173 | is( $obj->get(1), 2, 'get values at index 1' ); |
174 | is( $obj->get(2), 3, 'get values at index 2' ); |
175 | is( $obj->get_curried, 2, 'get_curried returns value at index 1' ); |
e3c07b19 |
176 | |
a7821be5 |
177 | throws_ok { $obj->get() } |
178 | qr/Cannot call get without at least 1 argument/, |
179 | 'throws an error when get is called without any arguments'; |
e3c07b19 |
180 | |
a7821be5 |
181 | throws_ok { $obj->get( {} ) } |
e3181911 |
182 | qr/The index passed to get must be an integer/, |
a7821be5 |
183 | 'throws an error when get is called with an invalid argument'; |
e3c07b19 |
184 | |
a7821be5 |
185 | throws_ok { $obj->get(2.2) } |
e3181911 |
186 | qr/The index passed to get must be an integer/, |
a7821be5 |
187 | 'throws an error when get is called with an invalid argument'; |
e3c07b19 |
188 | |
a7821be5 |
189 | throws_ok { $obj->get('foo') } |
e3181911 |
190 | qr/The index passed to get must be an integer/, |
a7821be5 |
191 | 'throws an error when get is called with an invalid argument'; |
e3c07b19 |
192 | |
a7821be5 |
193 | throws_ok { $obj->get_curried(2) } |
194 | qr/Cannot call get with more than 1 argument/, |
195 | 'throws an error when get_curried is called with an argument'; |
e3c07b19 |
196 | |
a7821be5 |
197 | lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives'; |
e3c07b19 |
198 | |
a7821be5 |
199 | is( $obj->get(1), 100, 'get value at index 1 returns new value' ); |
e3c07b19 |
200 | |
a7821be5 |
201 | throws_ok { $obj->set( 1, 99, 42 ) } |
202 | qr/Cannot call set with more than 2 arguments/, |
203 | 'throws an error when set is called with three arguments'; |
59de9de4 |
204 | |
a7821be5 |
205 | lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives'; |
59de9de4 |
206 | |
a7821be5 |
207 | is( $obj->get(1), 99, 'get value at index 1 returns new value' ); |
59de9de4 |
208 | |
a7821be5 |
209 | throws_ok { $obj->set_curried_1( 99, 42 ) } |
210 | qr/Cannot call set with more than 2 arguments/, |
211 | 'throws an error when set_curried_1 is called with two arguments'; |
59de9de4 |
212 | |
a7821be5 |
213 | lives_ok { $obj->set_curried_2 } 'set_curried_2 lives'; |
59de9de4 |
214 | |
a7821be5 |
215 | is( $obj->get(1), 98, 'get value at index 1 returns new value' ); |
59de9de4 |
216 | |
a7821be5 |
217 | throws_ok { $obj->set_curried_2(42) } |
218 | qr/Cannot call set with more than 2 arguments/, |
219 | 'throws an error when set_curried_2 is called with one argument'; |
59de9de4 |
220 | |
a7821be5 |
221 | is( |
222 | $obj->accessor(1), 98, |
223 | 'accessor with one argument returns value at index 1' |
224 | ); |
59de9de4 |
225 | |
a7821be5 |
226 | lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives'; |
59de9de4 |
227 | |
a7821be5 |
228 | is( |
229 | $obj->get(1), 97, |
230 | 'accessor set value at index 1' |
231 | ); |
862b6081 |
232 | |
a7821be5 |
233 | throws_ok { $obj->accessor( 1, 96, 42 ) } |
234 | qr/Cannot call accessor with more than 2 arguments/, |
235 | 'throws an error when accessor is called with three arguments'; |
59de9de4 |
236 | |
a7821be5 |
237 | is( |
238 | $obj->accessor_curried_1, 97, |
239 | 'accessor_curried_1 returns expected value when called with no arguments' |
240 | ); |
59de9de4 |
241 | |
a7821be5 |
242 | lives_ok { $obj->accessor_curried_1(95) } |
243 | 'accessor_curried_1 as writer lives'; |
862b6081 |
244 | |
a7821be5 |
245 | is( |
246 | $obj->get(1), 95, |
247 | 'accessor_curried_1 set value at index 1' |
248 | ); |
862b6081 |
249 | |
a7821be5 |
250 | throws_ok { $obj->accessor_curried_1( 96, 42 ) } |
251 | qr/Cannot call accessor with more than 2 arguments/, |
252 | 'throws an error when accessor_curried_1 is called with two arguments'; |
862b6081 |
253 | |
a7821be5 |
254 | lives_ok { $obj->accessor_curried_2 } |
255 | 'accessor_curried_2 as writer lives'; |
862b6081 |
256 | |
a7821be5 |
257 | is( |
258 | $obj->get(1), 90, |
259 | 'accessor_curried_2 set value at index 1' |
260 | ); |
862b6081 |
261 | |
a7821be5 |
262 | throws_ok { $obj->accessor_curried_2(42) } |
263 | qr/Cannot call accessor with more than 2 arguments/, |
264 | 'throws an error when accessor_curried_2 is called with one argument'; |
862b6081 |
265 | |
a7821be5 |
266 | lives_ok { $obj->clear } 'clear lives'; |
862b6081 |
267 | |
a7821be5 |
268 | ok( $obj->is_empty, 'values is empty after call to clear' ); |
862b6081 |
269 | |
a7821be5 |
270 | $obj->set( 0 => 42 ); |
862b6081 |
271 | |
a7821be5 |
272 | throws_ok { $obj->clear(50) } |
273 | qr/Cannot call clear with any arguments/, |
274 | 'throws an error when clear is called with an argument'; |
862b6081 |
275 | |
a7821be5 |
276 | ok( |
277 | !$obj->is_empty, |
278 | 'values is not empty after failed call to clear' |
279 | ); |
59de9de4 |
280 | |
a7821be5 |
281 | $obj->clear; |
282 | $obj->push( 1, 5, 10, 42 ); |
59de9de4 |
283 | |
a7821be5 |
284 | lives_ok { $obj->delete(2) } 'delete lives'; |
59de9de4 |
285 | |
a7821be5 |
286 | is_deeply( |
287 | $obj->_values, [ 1, 5, 42 ], |
288 | 'delete removed the specified element' |
289 | ); |
e3c07b19 |
290 | |
a7821be5 |
291 | throws_ok { $obj->delete( 2, 3 ) } |
292 | qr/Cannot call delete with more than 1 argument/, |
293 | 'throws an error when delete is called with two arguments'; |
59de9de4 |
294 | |
a7821be5 |
295 | lives_ok { $obj->delete_curried } 'delete_curried lives'; |
910684ee |
296 | |
a7821be5 |
297 | is_deeply( |
298 | $obj->_values, [ 1, 42 ], |
299 | 'delete removed the specified element' |
300 | ); |
910684ee |
301 | |
a7821be5 |
302 | throws_ok { $obj->delete_curried(2) } |
303 | qr/Cannot call delete with more than 1 argument/, |
304 | 'throws an error when delete_curried is called with one argument'; |
e3c07b19 |
305 | |
a7821be5 |
306 | lives_ok { $obj->insert( 1, 21 ) } 'insert lives'; |
910684ee |
307 | |
a7821be5 |
308 | is_deeply( |
309 | $obj->_values, [ 1, 21, 42 ], |
310 | 'insert added the specified element' |
311 | ); |
910684ee |
312 | |
a7821be5 |
313 | throws_ok { $obj->insert( 1, 22, 44 ) } |
314 | qr/Cannot call insert with more than 2 arguments/, |
315 | 'throws an error when insert is called with three arguments'; |
59de9de4 |
316 | |
a7821be5 |
317 | lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives'; |
910684ee |
318 | |
a7821be5 |
319 | is_deeply( |
320 | $obj->_values, [ 1, 2, 3, 21, 42 ], |
321 | 'splice added the specified elements' |
322 | ); |
910684ee |
323 | |
a7821be5 |
324 | lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives'; |
e3c07b19 |
325 | |
a7821be5 |
326 | is_deeply( |
327 | $obj->_values, [ 1, 99, 3, 21, 42 ], |
328 | 'splice added the specified elements' |
329 | ); |
e3c07b19 |
330 | |
a7821be5 |
331 | throws_ok { $obj->splice() } |
332 | qr/Cannot call splice without at least 1 argument/, |
333 | 'throws an error when splice is called with no arguments'; |
910684ee |
334 | |
e3181911 |
335 | throws_ok { $obj->splice( 1, 'foo', ) } |
336 | qr/The length argument passed to splice must be an integer/, |
337 | 'throws an error when splice is called with an invalid length'; |
338 | |
a7821be5 |
339 | lives_ok { $obj->splice_curried_1( 2, 101 ) } |
340 | 'splice_curried_1 lives'; |
910684ee |
341 | |
a7821be5 |
342 | is_deeply( |
343 | $obj->_values, [ 1, 101, 21, 42 ], |
344 | 'splice added the specified elements' |
345 | ); |
59de9de4 |
346 | |
a7821be5 |
347 | lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives'; |
862b6081 |
348 | |
a7821be5 |
349 | is_deeply( |
350 | $obj->_values, [ 1, 102, 42 ], |
351 | 'splice added the specified elements' |
352 | ); |
862b6081 |
353 | |
a7821be5 |
354 | lives_ok { $obj->splice_curried_all } 'splice_curried_all lives'; |
862b6081 |
355 | |
a7821be5 |
356 | is_deeply( |
357 | $obj->_values, [ 1, 3, 4, 5 ], |
358 | 'splice added the specified elements' |
359 | ); |
59de9de4 |
360 | |
a7821be5 |
361 | $obj->_values( [ 3, 9, 5, 22, 11 ] ); |
862b6081 |
362 | |
a7821be5 |
363 | is_deeply( |
364 | [ $obj->sort ], [ 11, 22, 3, 5, 9 ], |
365 | 'sort returns sorted values' |
366 | ); |
862b6081 |
367 | |
a7821be5 |
368 | is_deeply( |
369 | [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ], |
370 | 'sort returns values sorted by provided function' |
371 | ); |
910684ee |
372 | |
a7821be5 |
373 | throws_ok { $obj->sort(1) } |
e3181911 |
374 | qr/The argument passed to sort must be a code reference/, |
375 | 'throws an error when passing a non coderef to sort'; |
910684ee |
376 | |
a7821be5 |
377 | throws_ok { |
378 | $obj->sort( sub { }, 27 ); |
379 | } |
380 | qr/Cannot call sort with more than 1 argument/, |
e3181911 |
381 | 'throws an error when passing two arguments to sort'; |
910684ee |
382 | |
a7821be5 |
383 | $obj->_values( [ 3, 9, 5, 22, 11 ] ); |
862b6081 |
384 | |
a7821be5 |
385 | $obj->sort_in_place; |
d50fc84a |
386 | |
a7821be5 |
387 | is_deeply( |
388 | $obj->_values, [ 11, 22, 3, 5, 9 ], |
389 | 'sort_in_place sorts values' |
390 | ); |
862b6081 |
391 | |
a7821be5 |
392 | $obj->sort_in_place( sub { $_[0] <=> $_[1] } ); |
862b6081 |
393 | |
a7821be5 |
394 | is_deeply( |
395 | $obj->_values, [ 3, 5, 9, 11, 22 ], |
396 | 'sort_in_place with function sorts values' |
397 | ); |
398 | |
399 | throws_ok { |
e3181911 |
400 | $obj->sort_in_place( 27 ); |
401 | } |
402 | qr/The argument passed to sort_in_place must be a code reference/, |
403 | 'throws an error when passing a non coderef to sort_in_place'; |
404 | |
405 | throws_ok { |
a7821be5 |
406 | $obj->sort_in_place( sub { }, 27 ); |
407 | } |
408 | qr/Cannot call sort_in_place with more than 1 argument/, |
e3181911 |
409 | 'throws an error when passing two arguments to sort_in_place'; |
a7821be5 |
410 | |
411 | $obj->_values( [ 3, 9, 5, 22, 11 ] ); |
412 | |
413 | $obj->sort_in_place_curried; |
414 | |
415 | is_deeply( |
416 | $obj->_values, [ 22, 11, 9, 5, 3 ], |
417 | 'sort_in_place_curried sorts values' |
418 | ); |
419 | |
420 | throws_ok { $obj->sort_in_place_curried(27) } |
421 | qr/Cannot call sort_in_place with more than 1 argument/, |
e3181911 |
422 | 'throws an error when passing one argument passed to sort_in_place_curried'; |
a7821be5 |
423 | |
424 | $obj->_values( [ 1 .. 5 ] ); |
425 | |
426 | is_deeply( |
427 | [ $obj->map( sub { $_ + 1 } ) ], |
428 | [ 2 .. 6 ], |
429 | 'map returns the expected values' |
430 | ); |
431 | |
432 | throws_ok { $obj->map } |
433 | qr/Cannot call map without at least 1 argument/, |
e3181911 |
434 | 'throws an error when passing no arguments to map'; |
a7821be5 |
435 | |
436 | throws_ok { |
437 | $obj->map( sub { }, 2 ); |
438 | } |
439 | qr/Cannot call map with more than 1 argument/, |
e3181911 |
440 | 'throws an error when passing two arguments to map'; |
a7821be5 |
441 | |
442 | throws_ok { $obj->map( {} ) } |
e3181911 |
443 | qr/The argument passed to map must be a code reference/, |
444 | 'throws an error when passing a non coderef to map'; |
a7821be5 |
445 | |
446 | $obj->_values( [ 1 .. 5 ] ); |
447 | |
448 | is_deeply( |
449 | [ $obj->map_curried ], |
450 | [ 2 .. 6 ], |
451 | 'map_curried returns the expected values' |
452 | ); |
453 | |
454 | throws_ok { |
455 | $obj->map_curried( sub { } ); |
456 | } |
457 | qr/Cannot call map with more than 1 argument/, |
e3181911 |
458 | 'throws an error when passing one argument passed to map_curried'; |
a7821be5 |
459 | |
460 | $obj->_values( [ 2 .. 9 ] ); |
461 | |
462 | is_deeply( |
463 | [ $obj->grep( sub { $_ < 5 } ) ], |
464 | [ 2 .. 4 ], |
465 | 'grep returns the expected values' |
466 | ); |
467 | |
468 | throws_ok { $obj->grep } |
469 | qr/Cannot call grep without at least 1 argument/, |
e3181911 |
470 | 'throws an error when passing no arguments to grep'; |
a7821be5 |
471 | |
472 | throws_ok { |
473 | $obj->grep( sub { }, 2 ); |
474 | } |
475 | qr/Cannot call grep with more than 1 argument/, |
e3181911 |
476 | 'throws an error when passing two arguments to grep'; |
a7821be5 |
477 | |
478 | throws_ok { $obj->grep( {} ) } |
e3181911 |
479 | qr/The argument passed to grep must be a code reference/, |
480 | 'throws an error when passing a non coderef to grep'; |
a7821be5 |
481 | |
482 | is_deeply( |
483 | [ $obj->grep_curried ], |
484 | [ 2 .. 4 ], |
485 | 'grep_curried returns the expected values' |
486 | ); |
487 | |
488 | throws_ok { |
489 | $obj->grep_curried( sub { } ); |
490 | } |
491 | qr/Cannot call grep with more than 1 argument/, |
e3181911 |
492 | 'throws an error when passing one argument passed to grep_curried'; |
a7821be5 |
493 | |
494 | $obj->_values( [ 2, 4, 22, 99, 101, 6 ] ); |
495 | |
496 | is( |
497 | $obj->first( sub { $_ % 2 } ), |
498 | 99, |
499 | 'first returns expected value' |
500 | ); |
501 | |
502 | throws_ok { $obj->first } |
503 | qr/Cannot call first without at least 1 argument/, |
e3181911 |
504 | 'throws an error when passing no arguments to first'; |
a7821be5 |
505 | |
506 | throws_ok { |
507 | $obj->first( sub { }, 2 ); |
508 | } |
509 | qr/Cannot call first with more than 1 argument/, |
e3181911 |
510 | 'throws an error when passing two arguments to first'; |
a7821be5 |
511 | |
512 | throws_ok { $obj->first( {} ) } |
e3181911 |
513 | qr/The argument passed to first must be a code reference/, |
514 | 'throws an error when passing a non coderef to first'; |
a7821be5 |
515 | |
516 | is( |
517 | $obj->first_curried, |
518 | 99, |
519 | 'first_curried returns expected value' |
520 | ); |
521 | |
522 | throws_ok { |
523 | $obj->first_curried( sub { } ); |
524 | } |
525 | qr/Cannot call first with more than 1 argument/, |
e3181911 |
526 | 'throws an error when passing one argument passed to first_curried'; |
a7821be5 |
527 | |
528 | $obj->_values( [ 1 .. 4 ] ); |
529 | |
530 | is( |
531 | $obj->join('-'), '1-2-3-4', |
532 | 'join returns expected result' |
533 | ); |
534 | |
535 | throws_ok { $obj->join } |
536 | qr/Cannot call join without at least 1 argument/, |
e3181911 |
537 | 'throws an error when passing no arguments to join'; |
a7821be5 |
538 | |
539 | throws_ok { $obj->join( '-', 2 ) } |
540 | qr/Cannot call join with more than 1 argument/, |
e3181911 |
541 | 'throws an error when passing two arguments to join'; |
a7821be5 |
542 | |
543 | throws_ok { $obj->join( {} ) } |
e3181911 |
544 | qr/The argument passed to join must be a string/, |
545 | 'throws an error when passing a non string to join'; |
a7821be5 |
546 | |
547 | is_deeply( |
548 | [ sort $obj->shuffle ], |
549 | [ 1 .. 4 ], |
550 | 'shuffle returns all values (cannot check for a random order)' |
551 | ); |
552 | |
553 | throws_ok { $obj->shuffle(2) } |
554 | qr/Cannot call shuffle with any arguments/, |
e3181911 |
555 | 'throws an error when passing an argument passed to shuffle'; |
a7821be5 |
556 | |
557 | $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] ); |
558 | |
559 | is_deeply( |
560 | [ $obj->uniq ], |
561 | [ 1 .. 4, 5, 7 ], |
562 | 'uniq returns expected values (in original order)' |
563 | ); |
564 | |
565 | throws_ok { $obj->uniq(2) } |
566 | qr/Cannot call uniq with any arguments/, |
e3181911 |
567 | 'throws an error when passing an argument passed to uniq'; |
a7821be5 |
568 | |
569 | $obj->_values( [ 1 .. 5 ] ); |
570 | |
571 | is( |
572 | $obj->reduce( sub { $_[0] * $_[1] } ), |
573 | 120, |
574 | 'reduce returns expected value' |
575 | ); |
576 | |
577 | throws_ok { $obj->reduce } |
578 | qr/Cannot call reduce without at least 1 argument/, |
e3181911 |
579 | 'throws an error when passing no arguments to reduce'; |
a7821be5 |
580 | |
581 | throws_ok { |
582 | $obj->reduce( sub { }, 2 ); |
583 | } |
584 | qr/Cannot call reduce with more than 1 argument/, |
e3181911 |
585 | 'throws an error when passing two arguments to reduce'; |
a7821be5 |
586 | |
587 | throws_ok { $obj->reduce( {} ) } |
e3181911 |
588 | qr/The argument passed to reduce must be a code reference/, |
589 | 'throws an error when passing a non coderef to reduce'; |
a7821be5 |
590 | |
591 | is( |
592 | $obj->reduce_curried, |
593 | 120, |
594 | 'reduce_curried returns expected value' |
595 | ); |
596 | |
597 | throws_ok { |
598 | $obj->reduce_curried( sub { } ); |
599 | } |
600 | qr/Cannot call reduce with more than 1 argument/, |
e3181911 |
601 | 'throws an error when passing one argument passed to reduce_curried'; |
a7821be5 |
602 | |
603 | $obj->_values( [ 1 .. 6 ] ); |
604 | |
605 | my $it = $obj->natatime(2); |
606 | my @nat; |
607 | while ( my @v = $it->() ) { |
608 | push @nat, \@v; |
609 | } |
610 | |
611 | is_deeply( |
612 | [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], |
613 | \@nat, |
614 | 'natatime returns expected iterator' |
615 | ); |
616 | |
617 | @nat = (); |
618 | $obj->natatime( 2, sub { push @nat, [@_] } ); |
619 | |
620 | is_deeply( |
621 | [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], |
622 | \@nat, |
623 | 'natatime with function returns expected value' |
624 | ); |
625 | |
626 | throws_ok { $obj->natatime( {} ) } |
e3181911 |
627 | qr/The n value passed to natatime must be an integer/, |
628 | 'throws an error when passing a non integer to natatime'; |
a7821be5 |
629 | |
630 | throws_ok { $obj->natatime( 2, {} ) } |
e3181911 |
631 | qr/The second argument passed to natatime must be a code reference/, |
632 | 'throws an error when passing a non code ref to natatime'; |
a7821be5 |
633 | |
04bcce6a |
634 | $it = $obj->natatime_curried(); |
635 | @nat = (); |
a098a4a3 |
636 | while ( my @v = $it->() ) { |
637 | push @nat, \@v; |
638 | } |
639 | |
640 | is_deeply( |
641 | [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], |
642 | \@nat, |
643 | 'natatime_curried returns expected iterator' |
644 | ); |
645 | |
646 | @nat = (); |
647 | $obj->natatime_curried( sub { push @nat, [@_] } ); |
648 | |
649 | is_deeply( |
650 | [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], |
651 | \@nat, |
652 | 'natatime_curried with function returns expected value' |
653 | ); |
654 | |
655 | throws_ok { $obj->natatime_curried( {} ) } |
e3181911 |
656 | qr/The second argument passed to natatime must be a code reference/, |
657 | 'throws an error when passing a non code ref to natatime_curried'; |
a098a4a3 |
658 | |
a7821be5 |
659 | if ( $class->meta->get_attribute('_values')->is_lazy ) { |
660 | my $obj = $class->new; |
661 | |
662 | is( $obj->count, 2, 'count is 2 (lazy init)' ); |
663 | |
664 | $obj->_clear_values; |
665 | |
a098a4a3 |
666 | is_deeply( |
667 | [ $obj->elements ], [ 42, 84 ], |
668 | 'elements contains default with lazy init' |
669 | ); |
a7821be5 |
670 | |
671 | $obj->_clear_values; |
672 | |
673 | $obj->push(2); |
674 | |
675 | is_deeply( |
676 | $obj->_values, [ 42, 84, 2 ], |
677 | 'push works with lazy init' |
678 | ); |
679 | |
680 | $obj->_clear_values; |
681 | |
682 | $obj->unshift( 3, 4 ); |
683 | |
684 | is_deeply( |
685 | $obj->_values, [ 3, 4, 42, 84 ], |
686 | 'unshift works with lazy init' |
687 | ); |
688 | } |
689 | } |
690 | $class; |
862b6081 |
691 | } |
a28e50e4 |
692 | |
693 | done_testing; |