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';
122 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
123 'unshift changed the value of the array in the object'
126 lives_ok { $obj->unshift() }
127 'call to unshift without arguments lives';
129 is( $obj->pop, 3, 'pop returns the last value in the array' );
132 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
133 'pop changed the value of the array in the object'
136 throws_ok { $obj->pop(42) }
137 qr/Cannot call pop with any arguments/,
138 'call to pop with arguments dies';
140 is( $obj->shift, 101, 'shift returns the first value' );
142 throws_ok { $obj->shift(42) }
143 qr/Cannot call shift with any arguments/,
144 'call to shift with arguments dies';
147 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
148 'shift changed the value of the array in the object'
152 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
153 'call to elements returns values as a list'
156 throws_ok { $obj->elements(22) }
157 qr/Cannot call elements with any arguments/,
158 'throws an error when passing an argument passed to elements';
160 $obj->_values( [ 1, 2, 3 ] );
162 is( $obj->get(0), 1, 'get values at index 0' );
163 is( $obj->get(1), 2, 'get values at index 1' );
164 is( $obj->get(2), 3, 'get values at index 2' );
165 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
167 throws_ok { $obj->get() }
168 qr/Cannot call get without at least 1 argument/,
169 'throws an error when get is called without any arguments';
171 throws_ok { $obj->get( {} ) }
172 qr/The index passed to get must be an integer/,
173 'throws an error when get is called with an invalid argument';
175 throws_ok { $obj->get(2.2) }
176 qr/The index passed to get must be an integer/,
177 'throws an error when get is called with an invalid argument';
179 throws_ok { $obj->get('foo') }
180 qr/The index passed to get must be an integer/,
181 'throws an error when get is called with an invalid argument';
183 throws_ok { $obj->get_curried(2) }
184 qr/Cannot call get with more than 1 argument/,
185 'throws an error when get_curried is called with an argument';
187 lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
189 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
191 throws_ok { $obj->set( 1, 99, 42 ) }
192 qr/Cannot call set with more than 2 arguments/,
193 'throws an error when set is called with three arguments';
195 lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
197 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
199 throws_ok { $obj->set_curried_1( 99, 42 ) }
200 qr/Cannot call set with more than 2 arguments/,
201 'throws an error when set_curried_1 is called with two arguments';
203 lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
205 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
207 throws_ok { $obj->set_curried_2(42) }
208 qr/Cannot call set with more than 2 arguments/,
209 'throws an error when set_curried_2 is called with one argument';
212 $obj->accessor(1), 98,
213 'accessor with one argument returns value at index 1'
216 lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
220 'accessor set value at index 1'
223 throws_ok { $obj->accessor( 1, 96, 42 ) }
224 qr/Cannot call accessor with more than 2 arguments/,
225 'throws an error when accessor is called with three arguments';
228 $obj->accessor_curried_1, 97,
229 'accessor_curried_1 returns expected value when called with no arguments'
232 lives_ok { $obj->accessor_curried_1(95) }
233 'accessor_curried_1 as writer lives';
237 'accessor_curried_1 set value at index 1'
240 throws_ok { $obj->accessor_curried_1( 96, 42 ) }
241 qr/Cannot call accessor with more than 2 arguments/,
242 'throws an error when accessor_curried_1 is called with two arguments';
244 lives_ok { $obj->accessor_curried_2 }
245 'accessor_curried_2 as writer lives';
249 'accessor_curried_2 set value at index 1'
252 throws_ok { $obj->accessor_curried_2(42) }
253 qr/Cannot call accessor with more than 2 arguments/,
254 'throws an error when accessor_curried_2 is called with one argument';
256 lives_ok { $obj->clear } 'clear lives';
258 ok( $obj->is_empty, 'values is empty after call to clear' );
260 $obj->set( 0 => 42 );
262 throws_ok { $obj->clear(50) }
263 qr/Cannot call clear with any arguments/,
264 'throws an error when clear is called with an argument';
268 'values is not empty after failed call to clear'
272 $obj->push( 1, 5, 10, 42 );
274 lives_ok { $obj->delete(2) } 'delete lives';
277 $obj->_values, [ 1, 5, 42 ],
278 'delete removed the specified element'
281 throws_ok { $obj->delete( 2, 3 ) }
282 qr/Cannot call delete with more than 1 argument/,
283 'throws an error when delete is called with two arguments';
285 lives_ok { $obj->delete_curried } 'delete_curried lives';
288 $obj->_values, [ 1, 42 ],
289 'delete removed the specified element'
292 throws_ok { $obj->delete_curried(2) }
293 qr/Cannot call delete with more than 1 argument/,
294 'throws an error when delete_curried is called with one argument';
296 lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
299 $obj->_values, [ 1, 21, 42 ],
300 'insert added the specified element'
303 throws_ok { $obj->insert( 1, 22, 44 ) }
304 qr/Cannot call insert with more than 2 arguments/,
305 'throws an error when insert is called with three arguments';
307 lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
310 $obj->_values, [ 1, 2, 3, 21, 42 ],
311 'splice added the specified elements'
314 lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
317 $obj->_values, [ 1, 99, 3, 21, 42 ],
318 'splice added the specified elements'
321 throws_ok { $obj->splice() }
322 qr/Cannot call splice without at least 1 argument/,
323 'throws an error when splice is called with no arguments';
325 throws_ok { $obj->splice( 1, 'foo', ) }
326 qr/The length argument passed to splice must be an integer/,
327 'throws an error when splice is called with an invalid length';
329 lives_ok { $obj->splice_curried_1( 2, 101 ) }
330 'splice_curried_1 lives';
333 $obj->_values, [ 1, 101, 21, 42 ],
334 'splice added the specified elements'
337 lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
340 $obj->_values, [ 1, 102, 42 ],
341 'splice added the specified elements'
344 lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
347 $obj->_values, [ 1, 3, 4, 5 ],
348 'splice added the specified elements'
351 $obj->_values( [ 3, 9, 5, 22, 11 ] );
354 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
355 'sort returns sorted values'
359 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
360 'sort returns values sorted by provided function'
363 throws_ok { $obj->sort(1) }
364 qr/The argument passed to sort must be a code reference/,
365 'throws an error when passing a non coderef to sort';
368 $obj->sort( sub { }, 27 );
370 qr/Cannot call sort with more than 1 argument/,
371 'throws an error when passing two arguments to sort';
373 $obj->_values( [ 3, 9, 5, 22, 11 ] );
378 $obj->_values, [ 11, 22, 3, 5, 9 ],
379 'sort_in_place sorts values'
382 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
385 $obj->_values, [ 3, 5, 9, 11, 22 ],
386 'sort_in_place with function sorts values'
390 $obj->sort_in_place( 27 );
392 qr/The argument passed to sort_in_place must be a code reference/,
393 'throws an error when passing a non coderef to sort_in_place';
396 $obj->sort_in_place( sub { }, 27 );
398 qr/Cannot call sort_in_place with more than 1 argument/,
399 'throws an error when passing two arguments to sort_in_place';
401 $obj->_values( [ 3, 9, 5, 22, 11 ] );
403 $obj->sort_in_place_curried;
406 $obj->_values, [ 22, 11, 9, 5, 3 ],
407 'sort_in_place_curried sorts values'
410 throws_ok { $obj->sort_in_place_curried(27) }
411 qr/Cannot call sort_in_place with more than 1 argument/,
412 'throws an error when passing one argument passed to sort_in_place_curried';
414 $obj->_values( [ 1 .. 5 ] );
417 [ $obj->map( sub { $_ + 1 } ) ],
419 'map returns the expected values'
422 throws_ok { $obj->map }
423 qr/Cannot call map without at least 1 argument/,
424 'throws an error when passing no arguments to map';
427 $obj->map( sub { }, 2 );
429 qr/Cannot call map with more than 1 argument/,
430 'throws an error when passing two arguments to map';
432 throws_ok { $obj->map( {} ) }
433 qr/The argument passed to map must be a code reference/,
434 'throws an error when passing a non coderef to map';
436 $obj->_values( [ 1 .. 5 ] );
439 [ $obj->map_curried ],
441 'map_curried returns the expected values'
445 $obj->map_curried( sub { } );
447 qr/Cannot call map with more than 1 argument/,
448 'throws an error when passing one argument passed to map_curried';
450 $obj->_values( [ 2 .. 9 ] );
453 [ $obj->grep( sub { $_ < 5 } ) ],
455 'grep returns the expected values'
458 throws_ok { $obj->grep }
459 qr/Cannot call grep without at least 1 argument/,
460 'throws an error when passing no arguments to grep';
463 $obj->grep( sub { }, 2 );
465 qr/Cannot call grep with more than 1 argument/,
466 'throws an error when passing two arguments to grep';
468 throws_ok { $obj->grep( {} ) }
469 qr/The argument passed to grep must be a code reference/,
470 'throws an error when passing a non coderef to grep';
473 [ $obj->grep_curried ],
475 'grep_curried returns the expected values'
479 $obj->grep_curried( sub { } );
481 qr/Cannot call grep with more than 1 argument/,
482 'throws an error when passing one argument passed to grep_curried';
484 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
487 $obj->first( sub { $_ % 2 } ),
489 'first returns expected value'
492 throws_ok { $obj->first }
493 qr/Cannot call first without at least 1 argument/,
494 'throws an error when passing no arguments to first';
497 $obj->first( sub { }, 2 );
499 qr/Cannot call first with more than 1 argument/,
500 'throws an error when passing two arguments to first';
502 throws_ok { $obj->first( {} ) }
503 qr/The argument passed to first must be a code reference/,
504 'throws an error when passing a non coderef to first';
509 'first_curried returns expected value'
513 $obj->first_curried( sub { } );
515 qr/Cannot call first with more than 1 argument/,
516 'throws an error when passing one argument passed to first_curried';
518 $obj->_values( [ 1 .. 4 ] );
521 $obj->join('-'), '1-2-3-4',
522 'join returns expected result'
525 throws_ok { $obj->join }
526 qr/Cannot call join without at least 1 argument/,
527 'throws an error when passing no arguments to join';
529 throws_ok { $obj->join( '-', 2 ) }
530 qr/Cannot call join with more than 1 argument/,
531 'throws an error when passing two arguments to join';
533 throws_ok { $obj->join( {} ) }
534 qr/The argument passed to join must be a string/,
535 'throws an error when passing a non string to join';
538 [ sort $obj->shuffle ],
540 'shuffle returns all values (cannot check for a random order)'
543 throws_ok { $obj->shuffle(2) }
544 qr/Cannot call shuffle with any arguments/,
545 'throws an error when passing an argument passed to shuffle';
547 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
552 'uniq returns expected values (in original order)'
555 throws_ok { $obj->uniq(2) }
556 qr/Cannot call uniq with any arguments/,
557 'throws an error when passing an argument passed to uniq';
559 $obj->_values( [ 1 .. 5 ] );
562 $obj->reduce( sub { $_[0] * $_[1] } ),
564 'reduce returns expected value'
567 throws_ok { $obj->reduce }
568 qr/Cannot call reduce without at least 1 argument/,
569 'throws an error when passing no arguments to reduce';
572 $obj->reduce( sub { }, 2 );
574 qr/Cannot call reduce with more than 1 argument/,
575 'throws an error when passing two arguments to reduce';
577 throws_ok { $obj->reduce( {} ) }
578 qr/The argument passed to reduce must be a code reference/,
579 'throws an error when passing a non coderef to reduce';
582 $obj->reduce_curried,
584 'reduce_curried returns expected value'
588 $obj->reduce_curried( sub { } );
590 qr/Cannot call reduce with more than 1 argument/,
591 'throws an error when passing one argument passed to reduce_curried';
593 $obj->_values( [ 1 .. 6 ] );
595 my $it = $obj->natatime(2);
597 while ( my @v = $it->() ) {
602 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
604 'natatime returns expected iterator'
608 $obj->natatime( 2, sub { push @nat, [@_] } );
611 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
613 'natatime with function returns expected value'
616 throws_ok { $obj->natatime( {} ) }
617 qr/The n value passed to natatime must be an integer/,
618 'throws an error when passing a non integer to natatime';
620 throws_ok { $obj->natatime( 2, {} ) }
621 qr/The second argument passed to natatime must be a code reference/,
622 'throws an error when passing a non code ref to natatime';
624 $it = $obj->natatime_curried();
626 while ( my @v = $it->() ) {
631 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
633 'natatime_curried returns expected iterator'
637 $obj->natatime_curried( sub { push @nat, [@_] } );
640 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
642 'natatime_curried with function returns expected value'
645 throws_ok { $obj->natatime_curried( {} ) }
646 qr/The second argument passed to natatime must be a code reference/,
647 'throws an error when passing a non code ref to natatime_curried';
649 if ( $class->meta->get_attribute('_values')->is_lazy ) {
650 my $obj = $class->new;
652 is( $obj->count, 2, 'count is 2 (lazy init)' );
657 [ $obj->elements ], [ 42, 84 ],
658 'elements contains default with lazy init'
666 $obj->_values, [ 42, 84, 2 ],
667 'push works with lazy init'
672 $obj->unshift( 3, 4 );
675 $obj->_values, [ 3, 4, 42, 84 ],
676 'unshift works with lazy init'