14 elements => 'elements',
15 is_empty => 'is_empty',
21 [ unshift => 42, 84 ],
25 get_curried => [ get => 1 ],
27 set_curried_1 => [ set => 1 ],
28 set_curried_2 => [ set => ( 1, 98 ) ],
29 accessor => 'accessor',
30 accessor_curried_1 => [ accessor => 1 ],
31 accessor_curried_2 => [ accessor => ( 1, 90 ) ],
34 delete_curried => [ delete => 1 ],
36 insert_curried => [ insert => ( 1, 101 ) ],
38 splice_curried_1 => [ splice => 1 ],
39 splice_curried_2 => [ splice => 1, 2 ],
40 splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
42 sort_curried => [ sort => ( sub { $_[1] <=> $_[0] } ) ],
43 sort_in_place => 'sort_in_place',
44 sort_in_place_curried =>
45 [ sort_in_place => ( sub { $_[1] <=> $_[0] } ) ],
47 map_curried => [ map => ( sub { $_ + 1 } ) ],
49 grep_curried => [ grep => ( sub { $_ < 5 } ) ],
51 first_curried => [ first => ( sub { $_ % 2 } ) ],
53 join_curried => [ join => '-' ],
57 reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
58 natatime => 'natatime',
59 natatime_curried => [ natatime => 2 ],
67 my $class = Moose::Meta::Class->create(
69 superclasses => ['Moose::Object'],
72 $class->add_attribute(
76 isa => 'ArrayRef[Int]',
77 default => sub { [] },
79 clearer => '_clear_values',
84 return ( $class->name, \%handles );
89 run_tests(build_class);
90 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
94 my ( $class, $handles ) = @_;
96 can_ok( $class, $_ ) for sort keys %{$handles};
99 my $obj = $class->new( _values => [ 10, 12, 42 ] );
102 $obj->_values, [ 10, 12, 42 ],
103 'values can be set in constructor'
106 ok( !$obj->is_empty, 'values is not empty' );
107 is( $obj->count, 3, 'count returns 3' );
109 throws_ok { $obj->count(22) }
110 qr/Cannot call count with any arguments/,
111 'throws an error when passing an argument passed to count';
113 lives_ok { $obj->push( 1, 2, 3 ) }
114 'pushed three new values and lived';
116 lives_ok { $obj->push() } 'call to push without arguments lives';
118 lives_ok { $obj->unshift( 101, 22 ) }
119 'unshifted two values and lived';
121 lives_ok { $obj->unshift() }
122 'call to unshift without arguments lives';
124 is( $obj->pop, 3, 'pop returns the last value in the array' );
127 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
128 'pop changed the value of the array in the object'
131 throws_ok { $obj->pop(42) }
132 qr/Cannot call pop with any arguments/,
133 'call to pop with arguments dies';
135 is( $obj->shift, 101, 'shift returns the first value' );
137 throws_ok { $obj->shift(42) }
138 qr/Cannot call shift with any arguments/,
139 'call to shift with arguments dies';
142 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
143 'shift changed the value of the array in the object'
147 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
148 'call to elements returns values as a list'
151 throws_ok { $obj->elements(22) }
152 qr/Cannot call elements with any arguments/,
153 'throws an error when passing an argument passed to elements';
155 $obj->_values( [ 1, 2, 3 ] );
157 is( $obj->get(0), 1, 'get values at index 0' );
158 is( $obj->get(1), 2, 'get values at index 1' );
159 is( $obj->get(2), 3, 'get values at index 2' );
160 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
162 throws_ok { $obj->get() }
163 qr/Cannot call get without at least 1 argument/,
164 'throws an error when get is called without any arguments';
166 throws_ok { $obj->get( {} ) }
167 qr/The index passed to get must be an integer/,
168 'throws an error when get is called with an invalid argument';
170 throws_ok { $obj->get(2.2) }
171 qr/The index passed to get must be an integer/,
172 'throws an error when get is called with an invalid argument';
174 throws_ok { $obj->get('foo') }
175 qr/The index passed to get must be an integer/,
176 'throws an error when get is called with an invalid argument';
178 throws_ok { $obj->get_curried(2) }
179 qr/Cannot call get with more than 1 argument/,
180 'throws an error when get_curried is called with an argument';
182 lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
184 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
186 throws_ok { $obj->set( 1, 99, 42 ) }
187 qr/Cannot call set with more than 2 arguments/,
188 'throws an error when set is called with three arguments';
190 lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
192 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
194 throws_ok { $obj->set_curried_1( 99, 42 ) }
195 qr/Cannot call set with more than 2 arguments/,
196 'throws an error when set_curried_1 is called with two arguments';
198 lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
200 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
202 throws_ok { $obj->set_curried_2(42) }
203 qr/Cannot call set with more than 2 arguments/,
204 'throws an error when set_curried_2 is called with one argument';
207 $obj->accessor(1), 98,
208 'accessor with one argument returns value at index 1'
211 lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
215 'accessor set value at index 1'
218 throws_ok { $obj->accessor( 1, 96, 42 ) }
219 qr/Cannot call accessor with more than 2 arguments/,
220 'throws an error when accessor is called with three arguments';
223 $obj->accessor_curried_1, 97,
224 'accessor_curried_1 returns expected value when called with no arguments'
227 lives_ok { $obj->accessor_curried_1(95) }
228 'accessor_curried_1 as writer lives';
232 'accessor_curried_1 set value at index 1'
235 throws_ok { $obj->accessor_curried_1( 96, 42 ) }
236 qr/Cannot call accessor with more than 2 arguments/,
237 'throws an error when accessor_curried_1 is called with two arguments';
239 lives_ok { $obj->accessor_curried_2 }
240 'accessor_curried_2 as writer lives';
244 'accessor_curried_2 set value at index 1'
247 throws_ok { $obj->accessor_curried_2(42) }
248 qr/Cannot call accessor with more than 2 arguments/,
249 'throws an error when accessor_curried_2 is called with one argument';
251 lives_ok { $obj->clear } 'clear lives';
253 ok( $obj->is_empty, 'values is empty after call to clear' );
255 $obj->set( 0 => 42 );
257 throws_ok { $obj->clear(50) }
258 qr/Cannot call clear with any arguments/,
259 'throws an error when clear is called with an argument';
263 'values is not empty after failed call to clear'
267 $obj->push( 1, 5, 10, 42 );
269 lives_ok { $obj->delete(2) } 'delete lives';
272 $obj->_values, [ 1, 5, 42 ],
273 'delete removed the specified element'
276 throws_ok { $obj->delete( 2, 3 ) }
277 qr/Cannot call delete with more than 1 argument/,
278 'throws an error when delete is called with two arguments';
280 lives_ok { $obj->delete_curried } 'delete_curried lives';
283 $obj->_values, [ 1, 42 ],
284 'delete removed the specified element'
287 throws_ok { $obj->delete_curried(2) }
288 qr/Cannot call delete with more than 1 argument/,
289 'throws an error when delete_curried is called with one argument';
291 lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
294 $obj->_values, [ 1, 21, 42 ],
295 'insert added the specified element'
298 throws_ok { $obj->insert( 1, 22, 44 ) }
299 qr/Cannot call insert with more than 2 arguments/,
300 'throws an error when insert is called with three arguments';
302 lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
305 $obj->_values, [ 1, 2, 3, 21, 42 ],
306 'splice added the specified elements'
309 lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
312 $obj->_values, [ 1, 99, 3, 21, 42 ],
313 'splice added the specified elements'
316 throws_ok { $obj->splice() }
317 qr/Cannot call splice without at least 1 argument/,
318 'throws an error when splice is called with no arguments';
320 throws_ok { $obj->splice( 1, 'foo', ) }
321 qr/The length argument passed to splice must be an integer/,
322 'throws an error when splice is called with an invalid length';
324 lives_ok { $obj->splice_curried_1( 2, 101 ) }
325 'splice_curried_1 lives';
328 $obj->_values, [ 1, 101, 21, 42 ],
329 'splice added the specified elements'
332 lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
335 $obj->_values, [ 1, 102, 42 ],
336 'splice added the specified elements'
339 lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
342 $obj->_values, [ 1, 3, 4, 5 ],
343 'splice added the specified elements'
346 $obj->_values( [ 3, 9, 5, 22, 11 ] );
349 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
350 'sort returns sorted values'
354 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
355 'sort returns values sorted by provided function'
358 throws_ok { $obj->sort(1) }
359 qr/The argument passed to sort must be a code reference/,
360 'throws an error when passing a non coderef to sort';
363 $obj->sort( sub { }, 27 );
365 qr/Cannot call sort with more than 1 argument/,
366 'throws an error when passing two arguments to sort';
368 $obj->_values( [ 3, 9, 5, 22, 11 ] );
373 $obj->_values, [ 11, 22, 3, 5, 9 ],
374 'sort_in_place sorts values'
377 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
380 $obj->_values, [ 3, 5, 9, 11, 22 ],
381 'sort_in_place with function sorts values'
385 $obj->sort_in_place( 27 );
387 qr/The argument passed to sort_in_place must be a code reference/,
388 'throws an error when passing a non coderef to sort_in_place';
391 $obj->sort_in_place( sub { }, 27 );
393 qr/Cannot call sort_in_place with more than 1 argument/,
394 'throws an error when passing two arguments to sort_in_place';
396 $obj->_values( [ 3, 9, 5, 22, 11 ] );
398 $obj->sort_in_place_curried;
401 $obj->_values, [ 22, 11, 9, 5, 3 ],
402 'sort_in_place_curried sorts values'
405 throws_ok { $obj->sort_in_place_curried(27) }
406 qr/Cannot call sort_in_place with more than 1 argument/,
407 'throws an error when passing one argument passed to sort_in_place_curried';
409 $obj->_values( [ 1 .. 5 ] );
412 [ $obj->map( sub { $_ + 1 } ) ],
414 'map returns the expected values'
417 throws_ok { $obj->map }
418 qr/Cannot call map without at least 1 argument/,
419 'throws an error when passing no arguments to map';
422 $obj->map( sub { }, 2 );
424 qr/Cannot call map with more than 1 argument/,
425 'throws an error when passing two arguments to map';
427 throws_ok { $obj->map( {} ) }
428 qr/The argument passed to map must be a code reference/,
429 'throws an error when passing a non coderef to map';
431 $obj->_values( [ 1 .. 5 ] );
434 [ $obj->map_curried ],
436 'map_curried returns the expected values'
440 $obj->map_curried( sub { } );
442 qr/Cannot call map with more than 1 argument/,
443 'throws an error when passing one argument passed to map_curried';
445 $obj->_values( [ 2 .. 9 ] );
448 [ $obj->grep( sub { $_ < 5 } ) ],
450 'grep returns the expected values'
453 throws_ok { $obj->grep }
454 qr/Cannot call grep without at least 1 argument/,
455 'throws an error when passing no arguments to grep';
458 $obj->grep( sub { }, 2 );
460 qr/Cannot call grep with more than 1 argument/,
461 'throws an error when passing two arguments to grep';
463 throws_ok { $obj->grep( {} ) }
464 qr/The argument passed to grep must be a code reference/,
465 'throws an error when passing a non coderef to grep';
468 [ $obj->grep_curried ],
470 'grep_curried returns the expected values'
474 $obj->grep_curried( sub { } );
476 qr/Cannot call grep with more than 1 argument/,
477 'throws an error when passing one argument passed to grep_curried';
479 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
482 $obj->first( sub { $_ % 2 } ),
484 'first returns expected value'
487 throws_ok { $obj->first }
488 qr/Cannot call first without at least 1 argument/,
489 'throws an error when passing no arguments to first';
492 $obj->first( sub { }, 2 );
494 qr/Cannot call first with more than 1 argument/,
495 'throws an error when passing two arguments to first';
497 throws_ok { $obj->first( {} ) }
498 qr/The argument passed to first must be a code reference/,
499 'throws an error when passing a non coderef to first';
504 'first_curried returns expected value'
508 $obj->first_curried( sub { } );
510 qr/Cannot call first with more than 1 argument/,
511 'throws an error when passing one argument passed to first_curried';
513 $obj->_values( [ 1 .. 4 ] );
516 $obj->join('-'), '1-2-3-4',
517 'join returns expected result'
520 throws_ok { $obj->join }
521 qr/Cannot call join without at least 1 argument/,
522 'throws an error when passing no arguments to join';
524 throws_ok { $obj->join( '-', 2 ) }
525 qr/Cannot call join with more than 1 argument/,
526 'throws an error when passing two arguments to join';
528 throws_ok { $obj->join( {} ) }
529 qr/The argument passed to join must be a string/,
530 'throws an error when passing a non string to join';
533 [ sort $obj->shuffle ],
535 'shuffle returns all values (cannot check for a random order)'
538 throws_ok { $obj->shuffle(2) }
539 qr/Cannot call shuffle with any arguments/,
540 'throws an error when passing an argument passed to shuffle';
542 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
547 'uniq returns expected values (in original order)'
550 throws_ok { $obj->uniq(2) }
551 qr/Cannot call uniq with any arguments/,
552 'throws an error when passing an argument passed to uniq';
554 $obj->_values( [ 1 .. 5 ] );
557 $obj->reduce( sub { $_[0] * $_[1] } ),
559 'reduce returns expected value'
562 throws_ok { $obj->reduce }
563 qr/Cannot call reduce without at least 1 argument/,
564 'throws an error when passing no arguments to reduce';
567 $obj->reduce( sub { }, 2 );
569 qr/Cannot call reduce with more than 1 argument/,
570 'throws an error when passing two arguments to reduce';
572 throws_ok { $obj->reduce( {} ) }
573 qr/The argument passed to reduce must be a code reference/,
574 'throws an error when passing a non coderef to reduce';
577 $obj->reduce_curried,
579 'reduce_curried returns expected value'
583 $obj->reduce_curried( sub { } );
585 qr/Cannot call reduce with more than 1 argument/,
586 'throws an error when passing one argument passed to reduce_curried';
588 $obj->_values( [ 1 .. 6 ] );
590 my $it = $obj->natatime(2);
592 while ( my @v = $it->() ) {
597 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
599 'natatime returns expected iterator'
603 $obj->natatime( 2, sub { push @nat, [@_] } );
606 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
608 'natatime with function returns expected value'
611 throws_ok { $obj->natatime( {} ) }
612 qr/The n value passed to natatime must be an integer/,
613 'throws an error when passing a non integer to natatime';
615 throws_ok { $obj->natatime( 2, {} ) }
616 qr/The second argument passed to natatime must be a code reference/,
617 'throws an error when passing a non code ref to natatime';
619 $it = $obj->natatime_curried();
621 while ( my @v = $it->() ) {
626 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
628 'natatime_curried returns expected iterator'
632 $obj->natatime_curried( sub { push @nat, [@_] } );
635 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
637 'natatime_curried with function returns expected value'
640 throws_ok { $obj->natatime_curried( {} ) }
641 qr/The second argument passed to natatime must be a code reference/,
642 'throws an error when passing a non code ref to natatime_curried';
644 if ( $class->meta->get_attribute('_values')->is_lazy ) {
645 my $obj = $class->new;
647 is( $obj->count, 2, 'count is 2 (lazy init)' );
652 [ $obj->elements ], [ 42, 84 ],
653 'elements contains default with lazy init'
661 $obj->_values, [ 42, 84, 2 ],
662 'push works with lazy init'
667 $obj->unshift( 3, 4 );
670 $obj->_values, [ 3, 4, 42, 84 ],
671 'unshift works with lazy init'