7 use Moose::Util::TypeConstraints;
15 elements => 'elements',
16 is_empty => 'is_empty',
22 [ unshift => 42, 84 ],
26 get_curried => [ get => 1 ],
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 ) ],
35 delete_curried => [ delete => 1 ],
37 insert_curried => [ insert => ( 1, 101 ) ],
39 splice_curried_1 => [ splice => 1 ],
40 splice_curried_2 => [ splice => 1, 2 ],
41 splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
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] } ) ],
48 map_curried => [ map => ( sub { $_ + 1 } ) ],
50 grep_curried => [ grep => ( sub { $_ < 5 } ) ],
52 first_curried => [ first => ( sub { $_ % 2 } ) ],
54 join_curried => [ join => '-' ],
58 reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
59 natatime => 'natatime',
60 natatime_curried => [ natatime => 2 ],
68 my $class = Moose::Meta::Class->create(
70 superclasses => ['Moose::Object'],
73 $class->add_attribute(
77 isa => 'ArrayRef[Int]',
78 default => sub { [] },
80 clearer => '_clear_values',
85 return ( $class->name, \%handles );
90 run_tests(build_class);
91 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
92 run_tests( build_class( trigger => sub { } ) );
94 # Will force the inlining code to check the entire arrayref when it is modified.
95 subtype 'MyArrayRef', as 'ArrayRef', where { 1 };
97 run_tests( build_class( isa => 'MyArrayRef' ) );
99 coerce 'MyArrayRef', from 'ArrayRef', via { $_ };
101 run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) );
105 my ( $class, $handles ) = @_;
107 can_ok( $class, $_ ) for sort keys %{$handles};
110 my $obj = $class->new( _values => [ 10, 12, 42 ] );
113 $obj->_values, [ 10, 12, 42 ],
114 'values can be set in constructor'
117 ok( !$obj->is_empty, 'values is not empty' );
118 is( $obj->count, 3, 'count returns 3' );
120 throws_ok { $obj->count(22) }
121 qr/Cannot call count with any arguments/,
122 'throws an error when passing an argument passed to count';
124 lives_ok { $obj->push( 1, 2, 3 ) }
125 'pushed three new values and lived';
127 lives_ok { $obj->push() } 'call to push without arguments lives';
129 lives_ok { $obj->unshift( 101, 22 ) }
130 'unshifted two values and lived';
133 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
134 'unshift changed the value of the array in the object'
137 lives_ok { $obj->unshift() }
138 'call to unshift without arguments lives';
140 is( $obj->pop, 3, 'pop returns the last value in the array' );
143 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
144 'pop changed the value of the array in the object'
147 throws_ok { $obj->pop(42) }
148 qr/Cannot call pop with any arguments/,
149 'call to pop with arguments dies';
151 is( $obj->shift, 101, 'shift returns the first value' );
153 throws_ok { $obj->shift(42) }
154 qr/Cannot call shift with any arguments/,
155 'call to shift with arguments dies';
158 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
159 'shift changed the value of the array in the object'
163 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
164 'call to elements returns values as a list'
167 throws_ok { $obj->elements(22) }
168 qr/Cannot call elements with any arguments/,
169 'throws an error when passing an argument passed to elements';
171 $obj->_values( [ 1, 2, 3 ] );
173 is( $obj->get(0), 1, 'get values at index 0' );
174 is( $obj->get(1), 2, 'get values at index 1' );
175 is( $obj->get(2), 3, 'get values at index 2' );
176 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
178 throws_ok { $obj->get() }
179 qr/Cannot call get without at least 1 argument/,
180 'throws an error when get is called without any arguments';
182 throws_ok { $obj->get( {} ) }
183 qr/The index passed to get must be an integer/,
184 'throws an error when get is called with an invalid argument';
186 throws_ok { $obj->get(2.2) }
187 qr/The index passed to get must be an integer/,
188 'throws an error when get is called with an invalid argument';
190 throws_ok { $obj->get('foo') }
191 qr/The index passed to get must be an integer/,
192 'throws an error when get is called with an invalid argument';
194 throws_ok { $obj->get_curried(2) }
195 qr/Cannot call get with more than 1 argument/,
196 'throws an error when get_curried is called with an argument';
198 lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
200 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
202 throws_ok { $obj->set( 1, 99, 42 ) }
203 qr/Cannot call set with more than 2 arguments/,
204 'throws an error when set is called with three arguments';
206 lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
208 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
210 throws_ok { $obj->set_curried_1( 99, 42 ) }
211 qr/Cannot call set with more than 2 arguments/,
212 'throws an error when set_curried_1 is called with two arguments';
214 lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
216 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
218 throws_ok { $obj->set_curried_2(42) }
219 qr/Cannot call set with more than 2 arguments/,
220 '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'
227 lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
231 'accessor set value at index 1'
234 throws_ok { $obj->accessor( 1, 96, 42 ) }
235 qr/Cannot call accessor with more than 2 arguments/,
236 'throws an error when accessor is called with three arguments';
239 $obj->accessor_curried_1, 97,
240 'accessor_curried_1 returns expected value when called with no arguments'
243 lives_ok { $obj->accessor_curried_1(95) }
244 'accessor_curried_1 as writer lives';
248 'accessor_curried_1 set value at index 1'
251 throws_ok { $obj->accessor_curried_1( 96, 42 ) }
252 qr/Cannot call accessor with more than 2 arguments/,
253 'throws an error when accessor_curried_1 is called with two arguments';
255 lives_ok { $obj->accessor_curried_2 }
256 'accessor_curried_2 as writer lives';
260 'accessor_curried_2 set value at index 1'
263 throws_ok { $obj->accessor_curried_2(42) }
264 qr/Cannot call accessor with more than 2 arguments/,
265 'throws an error when accessor_curried_2 is called with one argument';
267 lives_ok { $obj->clear } 'clear lives';
269 ok( $obj->is_empty, 'values is empty after call to clear' );
271 $obj->set( 0 => 42 );
273 throws_ok { $obj->clear(50) }
274 qr/Cannot call clear with any arguments/,
275 'throws an error when clear is called with an argument';
279 'values is not empty after failed call to clear'
283 $obj->push( 1, 5, 10, 42 );
285 lives_ok { $obj->delete(2) } 'delete lives';
288 $obj->_values, [ 1, 5, 42 ],
289 'delete removed the specified element'
292 throws_ok { $obj->delete( 2, 3 ) }
293 qr/Cannot call delete with more than 1 argument/,
294 'throws an error when delete is called with two arguments';
296 lives_ok { $obj->delete_curried } 'delete_curried lives';
299 $obj->_values, [ 1, 42 ],
300 'delete removed the specified element'
303 throws_ok { $obj->delete_curried(2) }
304 qr/Cannot call delete with more than 1 argument/,
305 'throws an error when delete_curried is called with one argument';
307 lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
310 $obj->_values, [ 1, 21, 42 ],
311 'insert added the specified element'
314 throws_ok { $obj->insert( 1, 22, 44 ) }
315 qr/Cannot call insert with more than 2 arguments/,
316 'throws an error when insert is called with three arguments';
318 lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
321 $obj->_values, [ 1, 2, 3, 21, 42 ],
322 'splice added the specified elements'
325 lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
328 $obj->_values, [ 1, 99, 3, 21, 42 ],
329 'splice added the specified elements'
332 throws_ok { $obj->splice() }
333 qr/Cannot call splice without at least 1 argument/,
334 'throws an error when splice is called with no arguments';
336 throws_ok { $obj->splice( 1, 'foo', ) }
337 qr/The length argument passed to splice must be an integer/,
338 'throws an error when splice is called with an invalid length';
340 lives_ok { $obj->splice_curried_1( 2, 101 ) }
341 'splice_curried_1 lives';
344 $obj->_values, [ 1, 101, 21, 42 ],
345 'splice added the specified elements'
348 lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
351 $obj->_values, [ 1, 102, 42 ],
352 'splice added the specified elements'
355 lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
358 $obj->_values, [ 1, 3, 4, 5 ],
359 'splice added the specified elements'
362 $obj->_values( [ 3, 9, 5, 22, 11 ] );
365 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
366 'sort returns sorted values'
370 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
371 'sort returns values sorted by provided function'
374 throws_ok { $obj->sort(1) }
375 qr/The argument passed to sort must be a code reference/,
376 'throws an error when passing a non coderef to sort';
379 $obj->sort( sub { }, 27 );
381 qr/Cannot call sort with more than 1 argument/,
382 'throws an error when passing two arguments to sort';
384 $obj->_values( [ 3, 9, 5, 22, 11 ] );
389 $obj->_values, [ 11, 22, 3, 5, 9 ],
390 'sort_in_place sorts values'
393 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
396 $obj->_values, [ 3, 5, 9, 11, 22 ],
397 'sort_in_place with function sorts values'
401 $obj->sort_in_place( 27 );
403 qr/The argument passed to sort_in_place must be a code reference/,
404 'throws an error when passing a non coderef to sort_in_place';
407 $obj->sort_in_place( sub { }, 27 );
409 qr/Cannot call sort_in_place with more than 1 argument/,
410 'throws an error when passing two arguments to sort_in_place';
412 $obj->_values( [ 3, 9, 5, 22, 11 ] );
414 $obj->sort_in_place_curried;
417 $obj->_values, [ 22, 11, 9, 5, 3 ],
418 'sort_in_place_curried sorts values'
421 throws_ok { $obj->sort_in_place_curried(27) }
422 qr/Cannot call sort_in_place with more than 1 argument/,
423 'throws an error when passing one argument passed to sort_in_place_curried';
425 $obj->_values( [ 1 .. 5 ] );
428 [ $obj->map( sub { $_ + 1 } ) ],
430 'map returns the expected values'
433 throws_ok { $obj->map }
434 qr/Cannot call map without at least 1 argument/,
435 'throws an error when passing no arguments to map';
438 $obj->map( sub { }, 2 );
440 qr/Cannot call map with more than 1 argument/,
441 'throws an error when passing two arguments to map';
443 throws_ok { $obj->map( {} ) }
444 qr/The argument passed to map must be a code reference/,
445 'throws an error when passing a non coderef to map';
447 $obj->_values( [ 1 .. 5 ] );
450 [ $obj->map_curried ],
452 'map_curried returns the expected values'
456 $obj->map_curried( sub { } );
458 qr/Cannot call map with more than 1 argument/,
459 'throws an error when passing one argument passed to map_curried';
461 $obj->_values( [ 2 .. 9 ] );
464 [ $obj->grep( sub { $_ < 5 } ) ],
466 'grep returns the expected values'
469 throws_ok { $obj->grep }
470 qr/Cannot call grep without at least 1 argument/,
471 'throws an error when passing no arguments to grep';
474 $obj->grep( sub { }, 2 );
476 qr/Cannot call grep with more than 1 argument/,
477 'throws an error when passing two arguments to grep';
479 throws_ok { $obj->grep( {} ) }
480 qr/The argument passed to grep must be a code reference/,
481 'throws an error when passing a non coderef to grep';
484 [ $obj->grep_curried ],
486 'grep_curried returns the expected values'
490 $obj->grep_curried( sub { } );
492 qr/Cannot call grep with more than 1 argument/,
493 'throws an error when passing one argument passed to grep_curried';
495 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
498 $obj->first( sub { $_ % 2 } ),
500 'first returns expected value'
503 throws_ok { $obj->first }
504 qr/Cannot call first without at least 1 argument/,
505 'throws an error when passing no arguments to first';
508 $obj->first( sub { }, 2 );
510 qr/Cannot call first with more than 1 argument/,
511 'throws an error when passing two arguments to first';
513 throws_ok { $obj->first( {} ) }
514 qr/The argument passed to first must be a code reference/,
515 'throws an error when passing a non coderef to first';
520 'first_curried returns expected value'
524 $obj->first_curried( sub { } );
526 qr/Cannot call first with more than 1 argument/,
527 'throws an error when passing one argument passed to first_curried';
529 $obj->_values( [ 1 .. 4 ] );
532 $obj->join('-'), '1-2-3-4',
533 'join returns expected result'
536 throws_ok { $obj->join }
537 qr/Cannot call join without at least 1 argument/,
538 'throws an error when passing no arguments to join';
540 throws_ok { $obj->join( '-', 2 ) }
541 qr/Cannot call join with more than 1 argument/,
542 'throws an error when passing two arguments to join';
544 throws_ok { $obj->join( {} ) }
545 qr/The argument passed to join must be a string/,
546 'throws an error when passing a non string to join';
549 [ sort $obj->shuffle ],
551 'shuffle returns all values (cannot check for a random order)'
554 throws_ok { $obj->shuffle(2) }
555 qr/Cannot call shuffle with any arguments/,
556 'throws an error when passing an argument passed to shuffle';
558 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
563 'uniq returns expected values (in original order)'
566 throws_ok { $obj->uniq(2) }
567 qr/Cannot call uniq with any arguments/,
568 'throws an error when passing an argument passed to uniq';
570 $obj->_values( [ 1 .. 5 ] );
573 $obj->reduce( sub { $_[0] * $_[1] } ),
575 'reduce returns expected value'
578 throws_ok { $obj->reduce }
579 qr/Cannot call reduce without at least 1 argument/,
580 'throws an error when passing no arguments to reduce';
583 $obj->reduce( sub { }, 2 );
585 qr/Cannot call reduce with more than 1 argument/,
586 'throws an error when passing two arguments to reduce';
588 throws_ok { $obj->reduce( {} ) }
589 qr/The argument passed to reduce must be a code reference/,
590 'throws an error when passing a non coderef to reduce';
593 $obj->reduce_curried,
595 'reduce_curried returns expected value'
599 $obj->reduce_curried( sub { } );
601 qr/Cannot call reduce with more than 1 argument/,
602 'throws an error when passing one argument passed to reduce_curried';
604 $obj->_values( [ 1 .. 6 ] );
606 my $it = $obj->natatime(2);
608 while ( my @v = $it->() ) {
613 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
615 'natatime returns expected iterator'
619 $obj->natatime( 2, sub { push @nat, [@_] } );
622 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
624 'natatime with function returns expected value'
627 throws_ok { $obj->natatime( {} ) }
628 qr/The n value passed to natatime must be an integer/,
629 'throws an error when passing a non integer to natatime';
631 throws_ok { $obj->natatime( 2, {} ) }
632 qr/The second argument passed to natatime must be a code reference/,
633 'throws an error when passing a non code ref to natatime';
635 $it = $obj->natatime_curried();
637 while ( my @v = $it->() ) {
642 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
644 'natatime_curried returns expected iterator'
648 $obj->natatime_curried( sub { push @nat, [@_] } );
651 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
653 'natatime_curried with function returns expected value'
656 throws_ok { $obj->natatime_curried( {} ) }
657 qr/The second argument passed to natatime must be a code reference/,
658 'throws an error when passing a non code ref to natatime_curried';
660 if ( $class->meta->get_attribute('_values')->is_lazy ) {
661 my $obj = $class->new;
663 is( $obj->count, 2, 'count is 2 (lazy init)' );
668 [ $obj->elements ], [ 42, 84 ],
669 'elements contains default with lazy init'
677 $obj->_values, [ 42, 84, 2 ],
678 'push works with lazy init'
683 $obj->unshift( 3, 4 );
686 $obj->_values, [ 3, 4, 42, 84 ],
687 'unshift works with lazy init'