9 use Moose::Util::MetaRole;
10 use Moose::Util::TypeConstraints;
11 use NoInlineAttribute;
19 elements => 'elements',
20 is_empty => 'is_empty',
26 [ unshift => 42, 84 ],
30 get_curried => [ get => 1 ],
32 set_curried_1 => [ set => 1 ],
33 set_curried_2 => [ set => ( 1, 98 ) ],
34 accessor => 'accessor',
35 accessor_curried_1 => [ accessor => 1 ],
36 accessor_curried_2 => [ accessor => ( 1, 90 ) ],
39 delete_curried => [ delete => 1 ],
41 insert_curried => [ insert => ( 1, 101 ) ],
43 splice_curried_1 => [ splice => 1 ],
44 splice_curried_2 => [ splice => 1, 2 ],
45 splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
47 sort_curried => [ sort => ( sub { $_[1] <=> $_[0] } ) ],
48 sort_in_place => 'sort_in_place',
49 sort_in_place_curried =>
50 [ sort_in_place => ( sub { $_[1] <=> $_[0] } ) ],
52 map_curried => [ map => ( sub { $_ + 1 } ) ],
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 throws_ok { $obj->count(22) }
142 qr/Cannot call count with any arguments/,
143 'throws an error when passing an argument passed to count';
145 lives_ok { $obj->push( 1, 2, 3 ) }
146 'pushed three new values and lived';
148 lives_ok { $obj->push() } 'call to push without arguments lives';
150 lives_ok { $obj->unshift( 101, 22 ) }
151 '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 lives_ok { $obj->unshift() }
159 'call to unshift without arguments lives';
161 is( $obj->pop, 3, 'pop returns the last value in the array' );
164 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
165 'pop changed the value of the array in the object'
168 throws_ok { $obj->pop(42) }
169 qr/Cannot call pop with any arguments/,
170 'call to pop with arguments dies';
172 is( $obj->shift, 101, 'shift returns the first value' );
174 throws_ok { $obj->shift(42) }
175 qr/Cannot call shift with any arguments/,
176 'call to shift with arguments dies';
179 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
180 'shift changed the value of the array in the object'
184 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
185 'call to elements returns values as a list'
188 throws_ok { $obj->elements(22) }
189 qr/Cannot call elements with any arguments/,
190 'throws an error when passing an argument passed to elements';
192 $obj->_values( [ 1, 2, 3 ] );
194 is( $obj->get(0), 1, 'get values at index 0' );
195 is( $obj->get(1), 2, 'get values at index 1' );
196 is( $obj->get(2), 3, 'get values at index 2' );
197 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
199 throws_ok { $obj->get() }
200 qr/Cannot call get without at least 1 argument/,
201 'throws an error when get is called without any arguments';
203 throws_ok { $obj->get( {} ) }
204 qr/The index passed to get must be an integer/,
205 'throws an error when get is called with an invalid argument';
207 throws_ok { $obj->get(2.2) }
208 qr/The index passed to get must be an integer/,
209 'throws an error when get is called with an invalid argument';
211 throws_ok { $obj->get('foo') }
212 qr/The index passed to get must be an integer/,
213 'throws an error when get is called with an invalid argument';
215 throws_ok { $obj->get_curried(2) }
216 qr/Cannot call get with more than 1 argument/,
217 'throws an error when get_curried is called with an argument';
219 lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
221 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
223 throws_ok { $obj->set( 1, 99, 42 ) }
224 qr/Cannot call set with more than 2 arguments/,
225 'throws an error when set is called with three arguments';
227 lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
229 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
231 throws_ok { $obj->set_curried_1( 99, 42 ) }
232 qr/Cannot call set with more than 2 arguments/,
233 'throws an error when set_curried_1 is called with two arguments';
235 lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
237 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
239 throws_ok { $obj->set_curried_2(42) }
240 qr/Cannot call set with more than 2 arguments/,
241 'throws an error when set_curried_2 is called with one argument';
244 $obj->accessor(1), 98,
245 'accessor with one argument returns value at index 1'
248 lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
252 'accessor set value at index 1'
255 throws_ok { $obj->accessor( 1, 96, 42 ) }
256 qr/Cannot call accessor with more than 2 arguments/,
257 'throws an error when accessor is called with three arguments';
260 $obj->accessor_curried_1, 97,
261 'accessor_curried_1 returns expected value when called with no arguments'
264 lives_ok { $obj->accessor_curried_1(95) }
265 'accessor_curried_1 as writer lives';
269 'accessor_curried_1 set value at index 1'
272 throws_ok { $obj->accessor_curried_1( 96, 42 ) }
273 qr/Cannot call accessor with more than 2 arguments/,
274 'throws an error when accessor_curried_1 is called with two arguments';
276 lives_ok { $obj->accessor_curried_2 }
277 'accessor_curried_2 as writer lives';
281 'accessor_curried_2 set value at index 1'
284 throws_ok { $obj->accessor_curried_2(42) }
285 qr/Cannot call accessor with more than 2 arguments/,
286 'throws an error when accessor_curried_2 is called with one argument';
288 lives_ok { $obj->clear } 'clear lives';
290 ok( $obj->is_empty, 'values is empty after call to clear' );
292 $obj->set( 0 => 42 );
294 throws_ok { $obj->clear(50) }
295 qr/Cannot call clear with any arguments/,
296 'throws an error when clear is called with an argument';
300 'values is not empty after failed call to clear'
303 throws_ok { $obj->is_empty(50) }
304 qr/Cannot call is_empty with any arguments/,
305 'throws an error when is_empty is called with an argument';
308 $obj->push( 1, 5, 10, 42 );
310 lives_ok { $obj->delete(2) } 'delete lives';
313 $obj->_values, [ 1, 5, 42 ],
314 'delete removed the specified element'
317 throws_ok { $obj->delete( 2, 3 ) }
318 qr/Cannot call delete with more than 1 argument/,
319 'throws an error when delete is called with two arguments';
321 lives_ok { $obj->delete_curried } 'delete_curried lives';
324 $obj->_values, [ 1, 42 ],
325 'delete removed the specified element'
328 throws_ok { $obj->delete_curried(2) }
329 qr/Cannot call delete with more than 1 argument/,
330 'throws an error when delete_curried is called with one argument';
332 lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
335 $obj->_values, [ 1, 21, 42 ],
336 'insert added the specified element'
339 throws_ok { $obj->insert( 1, 22, 44 ) }
340 qr/Cannot call insert with more than 2 arguments/,
341 'throws an error when insert is called with three arguments';
343 lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
346 $obj->_values, [ 1, 2, 3, 21, 42 ],
347 'splice added the specified elements'
350 lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
353 $obj->_values, [ 1, 99, 3, 21, 42 ],
354 'splice added the specified elements'
357 throws_ok { $obj->splice() }
358 qr/Cannot call splice without at least 1 argument/,
359 'throws an error when splice is called with no arguments';
361 throws_ok { $obj->splice( 1, 'foo', ) }
362 qr/The length argument passed to splice must be an integer/,
363 'throws an error when splice is called with an invalid length';
365 lives_ok { $obj->splice_curried_1( 2, 101 ) }
366 'splice_curried_1 lives';
369 $obj->_values, [ 1, 101, 21, 42 ],
370 'splice added the specified elements'
373 lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
376 $obj->_values, [ 1, 102, 42 ],
377 'splice added the specified elements'
380 lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
383 $obj->_values, [ 1, 3, 4, 5 ],
384 'splice added the specified elements'
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 throws_ok { $obj->sort(1) }
400 qr/The argument passed to sort must be a code reference/,
401 'throws an error when passing a non coderef to sort';
404 $obj->sort( sub { }, 27 );
406 qr/Cannot call sort with more than 1 argument/,
407 'throws an error when passing two arguments to sort';
409 $obj->_values( [ 3, 9, 5, 22, 11 ] );
414 $obj->_values, [ 11, 22, 3, 5, 9 ],
415 'sort_in_place sorts values'
418 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
421 $obj->_values, [ 3, 5, 9, 11, 22 ],
422 'sort_in_place with function sorts values'
426 $obj->sort_in_place( 27 );
428 qr/The argument passed to sort_in_place must be a code reference/,
429 'throws an error when passing a non coderef to sort_in_place';
432 $obj->sort_in_place( sub { }, 27 );
434 qr/Cannot call sort_in_place with more than 1 argument/,
435 'throws an error when passing two arguments to sort_in_place';
437 $obj->_values( [ 3, 9, 5, 22, 11 ] );
439 $obj->sort_in_place_curried;
442 $obj->_values, [ 22, 11, 9, 5, 3 ],
443 'sort_in_place_curried sorts values'
446 throws_ok { $obj->sort_in_place_curried(27) }
447 qr/Cannot call sort_in_place with more than 1 argument/,
448 'throws an error when passing one argument passed to sort_in_place_curried';
450 $obj->_values( [ 1 .. 5 ] );
453 [ $obj->map( sub { $_ + 1 } ) ],
455 'map returns the expected values'
458 throws_ok { $obj->map }
459 qr/Cannot call map without at least 1 argument/,
460 'throws an error when passing no arguments to map';
463 $obj->map( sub { }, 2 );
465 qr/Cannot call map with more than 1 argument/,
466 'throws an error when passing two arguments to map';
468 throws_ok { $obj->map( {} ) }
469 qr/The argument passed to map must be a code reference/,
470 'throws an error when passing a non coderef to map';
472 $obj->_values( [ 1 .. 5 ] );
475 [ $obj->map_curried ],
477 'map_curried returns the expected values'
481 $obj->map_curried( sub { } );
483 qr/Cannot call map with more than 1 argument/,
484 'throws an error when passing one argument passed to map_curried';
486 $obj->_values( [ 2 .. 9 ] );
489 [ $obj->grep( sub { $_ < 5 } ) ],
491 'grep returns the expected values'
494 throws_ok { $obj->grep }
495 qr/Cannot call grep without at least 1 argument/,
496 'throws an error when passing no arguments to grep';
499 $obj->grep( sub { }, 2 );
501 qr/Cannot call grep with more than 1 argument/,
502 'throws an error when passing two arguments to grep';
504 throws_ok { $obj->grep( {} ) }
505 qr/The argument passed to grep must be a code reference/,
506 'throws an error when passing a non coderef to grep';
508 my $overloader = Overloader->new( sub { $_ < 5 } );
510 [ $obj->grep($overloader) ],
512 'grep works with obj that overload code dereferencing'
516 [ $obj->grep_curried ],
518 'grep_curried returns the expected values'
522 $obj->grep_curried( sub { } );
524 qr/Cannot call grep with more than 1 argument/,
525 'throws an error when passing one argument passed to grep_curried';
527 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
530 $obj->first( sub { $_ % 2 } ),
532 'first returns expected value'
535 throws_ok { $obj->first }
536 qr/Cannot call first without at least 1 argument/,
537 'throws an error when passing no arguments to first';
540 $obj->first( sub { }, 2 );
542 qr/Cannot call first with more than 1 argument/,
543 'throws an error when passing two arguments to first';
545 throws_ok { $obj->first( {} ) }
546 qr/The argument passed to first must be a code reference/,
547 'throws an error when passing a non coderef to first';
552 'first_curried returns expected value'
556 $obj->first_curried( sub { } );
558 qr/Cannot call first with more than 1 argument/,
559 'throws an error when passing one argument passed to first_curried';
561 $obj->_values( [ 1 .. 4 ] );
564 $obj->join('-'), '1-2-3-4',
565 'join returns expected result'
568 throws_ok { $obj->join }
569 qr/Cannot call join without at least 1 argument/,
570 'throws an error when passing no arguments to join';
572 throws_ok { $obj->join( '-', 2 ) }
573 qr/Cannot call join with more than 1 argument/,
574 'throws an error when passing two arguments to join';
576 throws_ok { $obj->join( {} ) }
577 qr/The argument passed to join must be a string/,
578 'throws an error when passing a non string to join';
581 [ sort $obj->shuffle ],
583 'shuffle returns all values (cannot check for a random order)'
586 throws_ok { $obj->shuffle(2) }
587 qr/Cannot call shuffle with any arguments/,
588 'throws an error when passing an argument passed to shuffle';
590 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
595 'uniq returns expected values (in original order)'
598 throws_ok { $obj->uniq(2) }
599 qr/Cannot call uniq with any arguments/,
600 'throws an error when passing an argument passed to uniq';
602 $obj->_values( [ 1 .. 5 ] );
605 $obj->reduce( sub { $_[0] * $_[1] } ),
607 'reduce returns expected value'
610 throws_ok { $obj->reduce }
611 qr/Cannot call reduce without at least 1 argument/,
612 'throws an error when passing no arguments to reduce';
615 $obj->reduce( sub { }, 2 );
617 qr/Cannot call reduce with more than 1 argument/,
618 'throws an error when passing two arguments to reduce';
620 throws_ok { $obj->reduce( {} ) }
621 qr/The argument passed to reduce must be a code reference/,
622 'throws an error when passing a non coderef to reduce';
625 $obj->reduce_curried,
627 'reduce_curried returns expected value'
631 $obj->reduce_curried( sub { } );
633 qr/Cannot call reduce with more than 1 argument/,
634 'throws an error when passing one argument passed to reduce_curried';
636 $obj->_values( [ 1 .. 6 ] );
638 my $it = $obj->natatime(2);
640 while ( my @v = $it->() ) {
645 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
647 'natatime returns expected iterator'
651 $obj->natatime( 2, sub { push @nat, [@_] } );
654 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
656 'natatime with function returns expected value'
659 throws_ok { $obj->natatime( {} ) }
660 qr/The n value passed to natatime must be an integer/,
661 'throws an error when passing a non integer to natatime';
663 throws_ok { $obj->natatime( 2, {} ) }
664 qr/The second argument passed to natatime must be a code reference/,
665 'throws an error when passing a non code ref to natatime';
667 $it = $obj->natatime_curried();
669 while ( my @v = $it->() ) {
674 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
676 'natatime_curried returns expected iterator'
680 $obj->natatime_curried( sub { push @nat, [@_] } );
683 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
685 'natatime_curried with function returns expected value'
688 throws_ok { $obj->natatime_curried( {} ) }
689 qr/The second argument passed to natatime must be a code reference/,
690 'throws an error when passing a non code ref to natatime_curried';
692 if ( $class->meta->get_attribute('_values')->is_lazy ) {
693 my $obj = $class->new;
695 is( $obj->count, 2, 'count is 2 (lazy init)' );
700 [ $obj->elements ], [ 42, 84 ],
701 'elements contains default with lazy init'
709 $obj->_values, [ 42, 84, 2 ],
710 'push works with lazy init'
715 $obj->unshift( 3, 4 );
718 $obj->_values, [ 3, 4, 42, 84 ],
719 'unshift works with lazy init'