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_in_place => 'grep_in_place',
54 grep_curried => [ grep => ( sub { $_ < 5 } ) ],
56 first_curried => [ first => ( sub { $_ % 2 } ) ],
58 join_curried => [ join => '-' ],
62 reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
63 natatime => 'natatime',
64 natatime_curried => [ natatime => 2 ],
72 my $class = Moose::Meta::Class->create(
74 superclasses => ['Moose::Object'],
78 push @traits, 'NoInlineAttribute'
79 if delete $attr{no_inline};
81 $class->add_attribute(
85 isa => 'ArrayRef[Int]',
86 default => sub { [] },
88 clearer => '_clear_values',
93 return ( $class->name, \%handles );
101 '&{}' => sub { ${ $_[0] } },
110 run_tests(build_class);
111 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
112 run_tests( build_class( trigger => sub { } ) );
113 run_tests( build_class( no_inline => 1 ) );
115 # Will force the inlining code to check the entire arrayref when it is modified.
116 subtype 'MyArrayRef', as 'ArrayRef', where { 1 };
118 run_tests( build_class( isa => 'MyArrayRef' ) );
120 coerce 'MyArrayRef', from 'ArrayRef', via { $_ };
122 run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) );
126 my ( $class, $handles ) = @_;
128 can_ok( $class, $_ ) for sort keys %{$handles};
131 my $obj = $class->new( _values => [ 10, 12, 42 ] );
134 $obj->_values, [ 10, 12, 42 ],
135 'values can be set in constructor'
138 ok( !$obj->is_empty, 'values is not empty' );
139 is( $obj->count, 3, 'count returns 3' );
141 like( exception { $obj->count(22) }, qr/Cannot call count with any arguments/, 'throws an error when passing an argument passed to count' );
143 is( exception { $obj->push( 1, 2, 3 ) }, undef, 'pushed three new values and lived' );
145 is( exception { $obj->push() }, undef, 'call to push without arguments lives' );
148 is( $obj->unshift( 101, 22 ), 8,
149 'unshift returns size of the new array' );
150 }, undef, 'unshifted two values and lived' );
153 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
154 'unshift changed the value of the array in the object'
157 is( exception { $obj->unshift() }, undef, 'call to unshift without arguments lives' );
159 is( $obj->pop, 3, 'pop returns the last value in the array' );
162 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
163 'pop changed the value of the array in the object'
166 like( exception { $obj->pop(42) }, qr/Cannot call pop with any arguments/, 'call to pop with arguments dies' );
168 is( $obj->shift, 101, 'shift returns the first value' );
170 like( exception { $obj->shift(42) }, qr/Cannot call shift with any arguments/, 'call to shift with arguments dies' );
173 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
174 'shift changed the value of the array in the object'
178 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
179 'call to elements returns values as a list'
182 like( exception { $obj->elements(22) }, qr/Cannot call elements with any arguments/, 'throws an error when passing an argument passed to elements' );
184 $obj->_values( [ 1, 2, 3 ] );
186 is( $obj->get(0), 1, 'get values at index 0' );
187 is( $obj->get(1), 2, 'get values at index 1' );
188 is( $obj->get(2), 3, 'get values at index 2' );
189 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
191 like( exception { $obj->get() }, qr/Cannot call get without at least 1 argument/, 'throws an error when get is called without any arguments' );
193 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' );
195 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' );
197 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' );
199 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' );
202 is( $obj->set( 1, 100 ), 100, 'set returns new value' );
203 }, undef, 'set value at index 1 lives' );
205 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
208 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' );
210 is( exception { $obj->set_curried_1(99) }, undef, 'set_curried_1 lives' );
212 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
214 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' );
216 is( exception { $obj->set_curried_2 }, undef, 'set_curried_2 lives' );
218 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
220 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' );
223 $obj->accessor(1), 98,
224 'accessor with one argument returns value at index 1'
228 is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' );
229 }, undef, 'accessor as writer lives' );
235 qr/Cannot call accessor without at least 1 argument/,
236 'throws an error when accessor is called without arguments'
241 'accessor set value at index 1'
244 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' );
247 $obj->accessor_curried_1, 97,
248 'accessor_curried_1 returns expected value when called with no arguments'
251 is( exception { $obj->accessor_curried_1(95) }, undef, 'accessor_curried_1 as writer lives' );
255 'accessor_curried_1 set value at index 1'
258 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' );
260 is( exception { $obj->accessor_curried_2 }, undef, 'accessor_curried_2 as writer lives' );
264 'accessor_curried_2 set value at index 1'
267 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' );
269 is( exception { $obj->clear }, undef, 'clear lives' );
271 ok( $obj->is_empty, 'values is empty after call to clear' );
274 is( $obj->shift, undef,
275 'shift returns undef on an empty array' );
276 }, undef, 'shifted from an empty array and lived' );
278 $obj->set( 0 => 42 );
280 like( exception { $obj->clear(50) }, qr/Cannot call clear with any arguments/, 'throws an error when clear is called with an argument' );
284 'values is not empty after failed call to clear'
287 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' );
291 $obj->push( 1, 5, 10, 42 ), 4,
292 'pushed 4 elements, got number of elements in the array back'
296 is( $obj->delete(2), 10, 'delete returns deleted value' );
297 }, undef, 'delete lives' );
300 $obj->_values, [ 1, 5, 42 ],
301 'delete removed the specified element'
304 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' );
306 is( exception { $obj->delete_curried }, undef, 'delete_curried lives' );
309 $obj->_values, [ 1, 42 ],
310 'delete removed the specified element'
313 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' );
315 is( exception { $obj->insert( 1, 21 ) }, undef, 'insert lives' );
318 $obj->_values, [ 1, 21, 42 ],
319 'insert added the specified element'
322 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' );
326 [ $obj->splice( 1, 0, 2, 3 ) ],
328 'return value of splice is empty list when not removing elements'
330 }, undef, 'splice lives' );
333 $obj->_values, [ 1, 2, 3, 21, 42 ],
334 'splice added the specified elements'
339 [ $obj->splice( 1, 2, 99 ) ],
341 'splice returns list of removed values'
343 }, undef, 'splice lives' );
346 $obj->_values, [ 1, 99, 21, 42 ],
347 'splice added the specified elements'
350 like( exception { $obj->splice() }, qr/Cannot call splice without at least 1 argument/, 'throws an error when splice is called with no arguments' );
352 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' );
354 is( exception { $obj->splice_curried_1( 2, 101 ) }, undef, 'splice_curried_1 lives' );
357 $obj->_values, [ 1, 101, 42 ],
358 'splice added the specified elements'
361 is( exception { $obj->splice_curried_2(102) }, undef, 'splice_curried_2 lives' );
364 $obj->_values, [ 1, 102 ],
365 'splice added the specified elements'
368 is( exception { $obj->splice_curried_all }, undef, 'splice_curried_all lives' );
371 $obj->_values, [ 1, 3, 4, 5 ],
372 'splice added the specified elements'
376 scalar $obj->splice( 1, 2 ),
378 'splice in scalar context returns last element removed'
382 scalar $obj->splice( 1, 0, 42 ),
384 'splice in scalar context returns undef when no elements are removed'
387 $obj->_values( [ 3, 9, 5, 22, 11 ] );
390 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
391 'sort returns sorted values'
395 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
396 'sort returns values sorted by provided function'
399 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' );
402 $obj->sort( sub { }, 27 );
403 }, qr/Cannot call sort with more than 1 argument/, 'throws an error when passing two arguments to sort' );
405 $obj->_values( [ 3, 9, 5, 22, 11 ] );
410 $obj->_values, [ 11, 22, 3, 5, 9 ],
411 'sort_in_place sorts values'
414 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
417 $obj->_values, [ 3, 5, 9, 11, 22 ],
418 'sort_in_place with function sorts values'
422 $obj->sort_in_place( 27 );
423 }, 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' );
426 $obj->sort_in_place( sub { }, 27 );
427 }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing two arguments to sort_in_place' );
429 $obj->_values( [ 3, 9, 5, 22, 11 ] );
431 $obj->sort_in_place_curried;
434 $obj->_values, [ 22, 11, 9, 5, 3 ],
435 'sort_in_place_curried sorts values'
438 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' );
440 $obj->_values( [ 1 .. 5 ] );
443 [ $obj->map( sub { $_ + 1 } ) ],
445 'map returns the expected values'
448 like( exception { $obj->map }, qr/Cannot call map without at least 1 argument/, 'throws an error when passing no arguments to map' );
451 $obj->map( sub { }, 2 );
452 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing two arguments to map' );
454 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' );
456 $obj->_values( [ 1 .. 5 ] );
459 [ $obj->map_curried ],
461 'map_curried returns the expected values'
465 $obj->map_curried( sub { } );
466 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing one argument passed to map_curried' );
468 $obj->_values( [ 2 .. 9 ] );
471 [ $obj->grep( sub { $_ < 5 } ) ],
473 'grep returns the expected values'
476 like( exception { $obj->grep }, qr/Cannot call grep without at least 1 argument/, 'throws an error when passing no arguments to grep' );
479 $obj->grep( sub { }, 2 );
480 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing two arguments to grep' );
482 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' );
484 my $overloader = Overloader->new( sub { $_ < 5 } );
486 [ $obj->grep($overloader) ],
488 'grep works with obj that overload code dereferencing'
492 [ $obj->grep_curried ],
494 'grep_curried returns the expected values'
498 $obj->grep_curried( sub { } );
499 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing one argument passed to grep_curried' );
501 $obj->_values( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
503 $obj->grep_in_place(sub { $_ > 4 } );
508 'grep_in_place alters attribute vars'
511 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
514 $obj->first( sub { $_ % 2 } ),
516 'first returns expected value'
519 like( exception { $obj->first }, qr/Cannot call first without at least 1 argument/, 'throws an error when passing no arguments to first' );
522 $obj->first( sub { }, 2 );
523 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing two arguments to first' );
525 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' );
530 'first_curried returns expected value'
534 $obj->first_curried( sub { } );
535 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
537 $obj->_values( [ 1 .. 4 ] );
540 $obj->join('-'), '1-2-3-4',
541 'join returns expected result'
545 $obj->join(q{}), '1234',
546 'join returns expected result when joining with empty string'
549 like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
551 like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
553 like( exception { $obj->join( {} ) }, qr/The argument passed to join must be a string/, 'throws an error when passing a non string to join' );
556 [ sort $obj->shuffle ],
558 'shuffle returns all values (cannot check for a random order)'
561 like( exception { $obj->shuffle(2) }, qr/Cannot call shuffle with any arguments/, 'throws an error when passing an argument passed to shuffle' );
563 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
568 'uniq returns expected values (in original order)'
571 like( exception { $obj->uniq(2) }, qr/Cannot call uniq with any arguments/, 'throws an error when passing an argument passed to uniq' );
573 $obj->_values( [ 1 .. 5 ] );
576 $obj->reduce( sub { $_[0] * $_[1] } ),
578 'reduce returns expected value'
581 like( exception { $obj->reduce }, qr/Cannot call reduce without at least 1 argument/, 'throws an error when passing no arguments to reduce' );
584 $obj->reduce( sub { }, 2 );
585 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing two arguments to reduce' );
587 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' );
590 $obj->reduce_curried,
592 'reduce_curried returns expected value'
596 $obj->reduce_curried( sub { } );
597 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing one argument passed to reduce_curried' );
599 $obj->_values( [ 1 .. 6 ] );
601 my $it = $obj->natatime(2);
603 while ( my @v = $it->() ) {
608 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
610 'natatime returns expected iterator'
614 $obj->natatime( 2, sub { push @nat, [@_] } );
617 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
619 'natatime with function returns expected value'
622 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' );
624 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' );
626 $it = $obj->natatime_curried();
628 while ( my @v = $it->() ) {
633 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
635 'natatime_curried returns expected iterator'
639 $obj->natatime_curried( sub { push @nat, [@_] } );
642 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
644 'natatime_curried with function returns expected value'
647 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' );
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'