9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
18 elements => 'elements',
19 is_empty => 'is_empty',
25 [ unshift => 42, 84 ],
29 get_curried => [ get => 1 ],
31 set_curried_1 => [ set => 1 ],
32 set_curried_2 => [ set => ( 1, 98 ) ],
33 accessor => 'accessor',
34 accessor_curried_1 => [ accessor => 1 ],
35 accessor_curried_2 => [ accessor => ( 1, 90 ) ],
38 delete_curried => [ delete => 1 ],
40 insert_curried => [ insert => ( 1, 101 ) ],
42 splice_curried_1 => [ splice => 1 ],
43 splice_curried_2 => [ splice => 1, 2 ],
44 splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
46 sort_curried => [ sort => ( sub { $_[1] <=> $_[0] } ) ],
47 sort_in_place => 'sort_in_place',
48 sort_in_place_curried =>
49 [ sort_in_place => ( sub { $_[1] <=> $_[0] } ) ],
51 map_curried => [ map => ( sub { $_ + 1 } ) ],
53 grep_curried => [ grep => ( sub { $_ < 5 } ) ],
55 first_curried => [ first => ( sub { $_ % 2 } ) ],
56 first_index => 'first_index',
57 first_index_curried => [ first_index => ( sub { $_ % 2 } ) ],
59 join_curried => [ join => '-' ],
63 reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
64 natatime => 'natatime',
65 natatime_curried => [ natatime => 2 ],
73 my $class = Moose::Meta::Class->create(
75 superclasses => ['Moose::Object'],
79 push @traits, 'NoInlineAttribute'
80 if delete $attr{no_inline};
82 $class->add_attribute(
86 isa => 'ArrayRef[Int]',
87 default => sub { [] },
89 clearer => '_clear_values',
94 return ( $class->name, \%handles );
102 '&{}' => sub { ${ $_[0] } },
111 run_tests(build_class);
112 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
113 run_tests( build_class( trigger => sub { } ) );
114 run_tests( build_class( no_inline => 1 ) );
116 # Will force the inlining code to check the entire arrayref when it is modified.
117 subtype 'MyArrayRef', as 'ArrayRef', where { 1 };
119 run_tests( build_class( isa => 'MyArrayRef' ) );
121 coerce 'MyArrayRef', from 'ArrayRef', via { $_ };
123 run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) );
127 my ( $class, $handles ) = @_;
129 can_ok( $class, $_ ) for sort keys %{$handles};
132 my $obj = $class->new( _values => [ 10, 12, 42 ] );
135 $obj->_values, [ 10, 12, 42 ],
136 'values can be set in constructor'
139 ok( !$obj->is_empty, 'values is not empty' );
140 is( $obj->count, 3, 'count returns 3' );
142 like( exception { $obj->count(22) }, qr/Cannot call count with any arguments/, 'throws an error when passing an argument passed to count' );
144 is( exception { $obj->push( 1, 2, 3 ) }, undef, 'pushed three new values and lived' );
146 is( exception { $obj->push() }, undef, 'call to push without arguments lives' );
149 is( $obj->unshift( 101, 22 ), 8,
150 'unshift returns size of the new array' );
151 }, undef, 'unshifted two values and lived' );
154 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
155 'unshift changed the value of the array in the object'
158 is( exception { $obj->unshift() }, undef, 'call to unshift without arguments lives' );
160 is( $obj->pop, 3, 'pop returns the last value in the array' );
163 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
164 'pop changed the value of the array in the object'
167 like( exception { $obj->pop(42) }, qr/Cannot call pop with any arguments/, 'call to pop with arguments dies' );
169 is( $obj->shift, 101, 'shift returns the first value' );
171 like( exception { $obj->shift(42) }, qr/Cannot call shift with any arguments/, 'call to shift with arguments dies' );
174 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
175 'shift changed the value of the array in the object'
179 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
180 'call to elements returns values as a list'
183 like( exception { $obj->elements(22) }, qr/Cannot call elements with any arguments/, 'throws an error when passing an argument passed to elements' );
185 $obj->_values( [ 1, 2, 3 ] );
187 is( $obj->get(0), 1, 'get values at index 0' );
188 is( $obj->get(1), 2, 'get values at index 1' );
189 is( $obj->get(2), 3, 'get values at index 2' );
190 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
192 like( exception { $obj->get() }, qr/Cannot call get without at least 1 argument/, 'throws an error when get is called without any arguments' );
194 like( exception { $obj->get( {} ) }, qr/The index passed to get must be an integer/, 'throws an error when get is called with an invalid argument' );
196 like( exception { $obj->get(2.2) }, qr/The index passed to get must be an integer/, 'throws an error when get is called with an invalid argument' );
198 like( exception { $obj->get('foo') }, qr/The index passed to get must be an integer/, 'throws an error when get is called with an invalid argument' );
200 like( exception { $obj->get_curried(2) }, qr/Cannot call get with more than 1 argument/, 'throws an error when get_curried is called with an argument' );
203 is( $obj->set( 1, 100 ), 100, 'set returns new value' );
204 }, undef, 'set value at index 1 lives' );
206 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
209 like( exception { $obj->set( 1, 99, 42 ) }, qr/Cannot call set with more than 2 arguments/, 'throws an error when set is called with three arguments' );
211 is( exception { $obj->set_curried_1(99) }, undef, 'set_curried_1 lives' );
213 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
215 like( exception { $obj->set_curried_1( 99, 42 ) }, qr/Cannot call set with more than 2 arguments/, 'throws an error when set_curried_1 is called with two arguments' );
217 is( exception { $obj->set_curried_2 }, undef, 'set_curried_2 lives' );
219 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
221 like( exception { $obj->set_curried_2(42) }, qr/Cannot call set with more than 2 arguments/, 'throws an error when set_curried_2 is called with one argument' );
224 $obj->accessor(1), 98,
225 'accessor with one argument returns value at index 1'
229 is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' );
230 }, undef, 'accessor as writer lives' );
236 qr/Cannot call accessor without at least 1 argument/,
237 'throws an error when accessor is called without arguments'
242 'accessor set value at index 1'
245 like( exception { $obj->accessor( 1, 96, 42 ) }, qr/Cannot call accessor with more than 2 arguments/, 'throws an error when accessor is called with three arguments' );
248 $obj->accessor_curried_1, 97,
249 'accessor_curried_1 returns expected value when called with no arguments'
252 is( exception { $obj->accessor_curried_1(95) }, undef, 'accessor_curried_1 as writer lives' );
256 'accessor_curried_1 set value at index 1'
259 like( exception { $obj->accessor_curried_1( 96, 42 ) }, qr/Cannot call accessor with more than 2 arguments/, 'throws an error when accessor_curried_1 is called with two arguments' );
261 is( exception { $obj->accessor_curried_2 }, undef, 'accessor_curried_2 as writer lives' );
265 'accessor_curried_2 set value at index 1'
268 like( exception { $obj->accessor_curried_2(42) }, qr/Cannot call accessor with more than 2 arguments/, 'throws an error when accessor_curried_2 is called with one argument' );
270 is( exception { $obj->clear }, undef, 'clear lives' );
272 ok( $obj->is_empty, 'values is empty after call to clear' );
275 is( $obj->shift, undef,
276 'shift returns undef on an empty array' );
277 }, undef, 'shifted from an empty array and lived' );
279 $obj->set( 0 => 42 );
281 like( exception { $obj->clear(50) }, qr/Cannot call clear with any arguments/, 'throws an error when clear is called with an argument' );
285 'values is not empty after failed call to clear'
288 like( exception { $obj->is_empty(50) }, qr/Cannot call is_empty with any arguments/, 'throws an error when is_empty is called with an argument' );
292 $obj->push( 1, 5, 10, 42 ), 4,
293 'pushed 4 elements, got number of elements in the array back'
297 is( $obj->delete(2), 10, 'delete returns deleted value' );
298 }, undef, 'delete lives' );
301 $obj->_values, [ 1, 5, 42 ],
302 'delete removed the specified element'
305 like( exception { $obj->delete( 2, 3 ) }, qr/Cannot call delete with more than 1 argument/, 'throws an error when delete is called with two arguments' );
307 is( exception { $obj->delete_curried }, undef, 'delete_curried lives' );
310 $obj->_values, [ 1, 42 ],
311 'delete removed the specified element'
314 like( exception { $obj->delete_curried(2) }, qr/Cannot call delete with more than 1 argument/, 'throws an error when delete_curried is called with one argument' );
316 is( exception { $obj->insert( 1, 21 ) }, undef, 'insert lives' );
319 $obj->_values, [ 1, 21, 42 ],
320 'insert added the specified element'
323 like( exception { $obj->insert( 1, 22, 44 ) }, qr/Cannot call insert with more than 2 arguments/, 'throws an error when insert is called with three arguments' );
327 [ $obj->splice( 1, 0, 2, 3 ) ],
329 'return value of splice is empty list when not removing elements'
331 }, undef, 'splice lives' );
334 $obj->_values, [ 1, 2, 3, 21, 42 ],
335 'splice added the specified elements'
340 [ $obj->splice( 1, 2, 99 ) ],
342 'splice returns list of removed values'
344 }, undef, 'splice lives' );
347 $obj->_values, [ 1, 99, 21, 42 ],
348 'splice added the specified elements'
351 like( exception { $obj->splice() }, qr/Cannot call splice without at least 1 argument/, 'throws an error when splice is called with no arguments' );
353 like( exception { $obj->splice( 1, 'foo', ) }, qr/The length argument passed to splice must be an integer/, 'throws an error when splice is called with an invalid length' );
355 is( exception { $obj->splice_curried_1( 2, 101 ) }, undef, 'splice_curried_1 lives' );
358 $obj->_values, [ 1, 101, 42 ],
359 'splice added the specified elements'
362 is( exception { $obj->splice_curried_2(102) }, undef, 'splice_curried_2 lives' );
365 $obj->_values, [ 1, 102 ],
366 'splice added the specified elements'
369 is( exception { $obj->splice_curried_all }, undef, 'splice_curried_all lives' );
372 $obj->_values, [ 1, 3, 4, 5 ],
373 'splice added the specified elements'
377 scalar $obj->splice( 1, 2 ),
379 'splice in scalar context returns last element removed'
383 scalar $obj->splice( 1, 0, 42 ),
385 'splice in scalar context returns undef when no elements are removed'
388 $obj->_values( [ 3, 9, 5, 22, 11 ] );
391 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
392 'sort returns sorted values'
396 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
397 'sort returns values sorted by provided function'
400 like( exception { $obj->sort(1) }, qr/The argument passed to sort must be a code reference/, 'throws an error when passing a non coderef to sort' );
403 $obj->sort( sub { }, 27 );
404 }, qr/Cannot call sort with more than 1 argument/, 'throws an error when passing two arguments to sort' );
406 $obj->_values( [ 3, 9, 5, 22, 11 ] );
411 $obj->_values, [ 11, 22, 3, 5, 9 ],
412 'sort_in_place sorts values'
415 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
418 $obj->_values, [ 3, 5, 9, 11, 22 ],
419 'sort_in_place with function sorts values'
423 $obj->sort_in_place( 27 );
424 }, qr/The argument passed to sort_in_place must be a code reference/, 'throws an error when passing a non coderef to sort_in_place' );
427 $obj->sort_in_place( sub { }, 27 );
428 }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing two arguments to sort_in_place' );
430 $obj->_values( [ 3, 9, 5, 22, 11 ] );
432 $obj->sort_in_place_curried;
435 $obj->_values, [ 22, 11, 9, 5, 3 ],
436 'sort_in_place_curried sorts values'
439 like( exception { $obj->sort_in_place_curried(27) }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing one argument passed to sort_in_place_curried' );
441 $obj->_values( [ 1 .. 5 ] );
444 [ $obj->map( sub { $_ + 1 } ) ],
446 'map returns the expected values'
449 like( exception { $obj->map }, qr/Cannot call map without at least 1 argument/, 'throws an error when passing no arguments to map' );
452 $obj->map( sub { }, 2 );
453 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing two arguments to map' );
455 like( exception { $obj->map( {} ) }, qr/The argument passed to map must be a code reference/, 'throws an error when passing a non coderef to map' );
457 $obj->_values( [ 1 .. 5 ] );
460 [ $obj->map_curried ],
462 'map_curried returns the expected values'
466 $obj->map_curried( sub { } );
467 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing one argument passed to map_curried' );
469 $obj->_values( [ 2 .. 9 ] );
472 [ $obj->grep( sub { $_ < 5 } ) ],
474 'grep returns the expected values'
477 like( exception { $obj->grep }, qr/Cannot call grep without at least 1 argument/, 'throws an error when passing no arguments to grep' );
480 $obj->grep( sub { }, 2 );
481 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing two arguments to grep' );
483 like( exception { $obj->grep( {} ) }, qr/The argument passed to grep must be a code reference/, 'throws an error when passing a non coderef to grep' );
485 my $overloader = Overloader->new( sub { $_ < 5 } );
487 [ $obj->grep($overloader) ],
489 'grep works with obj that overload code dereferencing'
493 [ $obj->grep_curried ],
495 'grep_curried returns the expected values'
499 $obj->grep_curried( sub { } );
500 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing one argument passed to grep_curried' );
502 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
505 $obj->first( sub { $_ % 2 } ),
507 'first returns expected value'
510 like( exception { $obj->first }, qr/Cannot call first without at least 1 argument/, 'throws an error when passing no arguments to first' );
513 $obj->first( sub { }, 2 );
514 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing two arguments to first' );
516 like( exception { $obj->first( {} ) }, qr/The argument passed to first must be a code reference/, 'throws an error when passing a non coderef to first' );
521 'first_curried returns expected value'
525 $obj->first_curried( sub { } );
526 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
530 $obj->first_index( sub { $_ % 2 } ),
532 'first_index returns expected value'
535 like( exception { $obj->first_index }, qr/Cannot call first_index without at least 1 argument/, 'throws an error when passing no arguments to first_index' );
538 $obj->first_index( sub { }, 2 );
539 }, qr/Cannot call first_index with more than 1 argument/, 'throws an error when passing two arguments to first_index' );
541 like( exception { $obj->first_index( {} ) }, qr/The argument passed to first_index must be a code reference/, 'throws an error when passing a non coderef to first_index' );
544 $obj->first_index_curried,
546 'first_index_curried returns expected value'
550 $obj->first_index_curried( sub { } );
551 }, qr/Cannot call first_index with more than 1 argument/, 'throws an error when passing one argument passed to first_index_curried' );
554 $obj->_values( [ 1 .. 4 ] );
557 $obj->join('-'), '1-2-3-4',
558 'join returns expected result'
562 $obj->join(q{}), '1234',
563 'join returns expected result when joining with empty string'
566 like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
568 like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
570 like( exception { $obj->join( {} ) }, qr/The argument passed to join must be a string/, 'throws an error when passing a non string to join' );
573 [ sort $obj->shuffle ],
575 'shuffle returns all values (cannot check for a random order)'
578 like( exception { $obj->shuffle(2) }, qr/Cannot call shuffle with any arguments/, 'throws an error when passing an argument passed to shuffle' );
580 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
585 'uniq returns expected values (in original order)'
588 like( exception { $obj->uniq(2) }, qr/Cannot call uniq with any arguments/, 'throws an error when passing an argument passed to uniq' );
590 $obj->_values( [ 1 .. 5 ] );
593 $obj->reduce( sub { $_[0] * $_[1] } ),
595 'reduce returns expected value'
598 like( exception { $obj->reduce }, qr/Cannot call reduce without at least 1 argument/, 'throws an error when passing no arguments to reduce' );
601 $obj->reduce( sub { }, 2 );
602 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing two arguments to reduce' );
604 like( exception { $obj->reduce( {} ) }, qr/The argument passed to reduce must be a code reference/, 'throws an error when passing a non coderef to reduce' );
607 $obj->reduce_curried,
609 'reduce_curried returns expected value'
613 $obj->reduce_curried( sub { } );
614 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing one argument passed to reduce_curried' );
616 $obj->_values( [ 1 .. 6 ] );
618 my $it = $obj->natatime(2);
620 while ( my @v = $it->() ) {
625 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
627 'natatime returns expected iterator'
631 $obj->natatime( 2, sub { push @nat, [@_] } );
634 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
636 'natatime with function returns expected value'
639 like( exception { $obj->natatime( {} ) }, qr/The n value passed to natatime must be an integer/, 'throws an error when passing a non integer to natatime' );
641 like( exception { $obj->natatime( 2, {} ) }, qr/The second argument passed to natatime must be a code reference/, 'throws an error when passing a non code ref to natatime' );
643 $it = $obj->natatime_curried();
645 while ( my @v = $it->() ) {
650 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
652 'natatime_curried returns expected iterator'
656 $obj->natatime_curried( sub { push @nat, [@_] } );
659 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
661 'natatime_curried with function returns expected value'
664 like( exception { $obj->natatime_curried( {} ) }, qr/The second argument passed to natatime must be a code reference/, 'throws an error when passing a non code ref to natatime_curried' );
666 if ( $class->meta->get_attribute('_values')->is_lazy ) {
667 my $obj = $class->new;
669 is( $obj->count, 2, 'count is 2 (lazy init)' );
674 [ $obj->elements ], [ 42, 84 ],
675 'elements contains default with lazy init'
683 $obj->_values, [ 42, 84, 2 ],
684 'push works with lazy init'
689 $obj->unshift( 3, 4 );
692 $obj->_values, [ 3, 4, 42, 84 ],
693 'unshift works with lazy init'
701 my ( $class, $handles ) = build_class( isa => 'ArrayRef' );
702 my $obj = $class->new;
705 exception { $obj->accessor( 0, undef ) },
707 'can use accessor to set value to undef'
710 exception { $obj->accessor_curried_1(undef) },
712 'can use curried accessor to set value to undef'