Fix error when array accessor delegation method is called with no arguments
[gitmo/Moose.git] / t / 070_native_traits / 010_trait_array.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 use Moose ();
9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
11 use Test::More;
12 use Test::Fatal;
13 use Test::Moose;
14
15 {
16     my %handles = (
17         count    => 'count',
18         elements => 'elements',
19         is_empty => 'is_empty',
20         push     => 'push',
21         push_curried =>
22             [ push => 42, 84 ],
23         unshift => 'unshift',
24         unshift_curried =>
25             [ unshift => 42, 84 ],
26         pop           => 'pop',
27         shift         => 'shift',
28         get           => 'get',
29         get_curried   => [ get => 1 ],
30         set           => 'set',
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 ) ],
36         clear          => 'clear',
37         delete         => 'delete',
38         delete_curried => [ delete => 1 ],
39         insert         => 'insert',
40         insert_curried => [ insert => ( 1, 101 ) ],
41         splice         => 'splice',
42         splice_curried_1   => [ splice => 1 ],
43         splice_curried_2   => [ splice => 1, 2 ],
44         splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
45         sort          => 'sort',
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] } ) ],
50         map           => 'map',
51         map_curried   => [ map => ( sub { $_ + 1 } ) ],
52         grep          => 'grep',
53         grep_curried  => [ grep => ( sub { $_ < 5 } ) ],
54         first         => 'first',
55         first_curried => [ first => ( sub { $_ % 2 } ) ],
56         join          => 'join',
57         join_curried => [ join => '-' ],
58         shuffle      => 'shuffle',
59         uniq         => 'uniq',
60         reduce       => 'reduce',
61         reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
62         natatime       => 'natatime',
63         natatime_curried => [ natatime => 2 ],
64     );
65
66     my $name = 'Foo1';
67
68     sub build_class {
69         my %attr = @_;
70
71         my $class = Moose::Meta::Class->create(
72             $name++,
73             superclasses => ['Moose::Object'],
74         );
75
76         my @traits = 'Array';
77         push @traits, 'NoInlineAttribute'
78             if delete $attr{no_inline};
79
80         $class->add_attribute(
81             _values => (
82                 traits  => \@traits,
83                 is      => 'rw',
84                 isa     => 'ArrayRef[Int]',
85                 default => sub { [] },
86                 handles => \%handles,
87                 clearer => '_clear_values',
88                 %attr,
89             ),
90         );
91
92         return ( $class->name, \%handles );
93     }
94 }
95
96 {
97     package Overloader;
98
99     use overload
100         '&{}' => sub { ${ $_[0] } },
101         bool  => sub {1};
102
103     sub new {
104         bless \$_[1], $_[0];
105     }
106 }
107
108 {
109     run_tests(build_class);
110     run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
111     run_tests( build_class( trigger => sub { } ) );
112     run_tests( build_class( no_inline => 1 ) );
113
114     # Will force the inlining code to check the entire arrayref when it is modified.
115     subtype 'MyArrayRef', as 'ArrayRef', where { 1 };
116
117     run_tests( build_class( isa => 'MyArrayRef' ) );
118
119     coerce 'MyArrayRef', from 'ArrayRef', via { $_ };
120
121     run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) );
122 }
123
124 sub run_tests {
125     my ( $class, $handles ) = @_;
126
127     can_ok( $class, $_ ) for sort keys %{$handles};
128
129     with_immutable {
130         my $obj = $class->new( _values => [ 10, 12, 42 ] );
131
132         is_deeply(
133             $obj->_values, [ 10, 12, 42 ],
134             'values can be set in constructor'
135         );
136
137         ok( !$obj->is_empty, 'values is not empty' );
138         is( $obj->count, 3, 'count returns 3' );
139
140         like( exception { $obj->count(22) }, qr/Cannot call count with any arguments/, 'throws an error when passing an argument passed to count' );
141
142         is( exception { $obj->push( 1, 2, 3 ) }, undef, 'pushed three new values and lived' );
143
144         is( exception { $obj->push() }, undef, 'call to push without arguments lives' );
145
146         is( exception {
147             is( $obj->unshift( 101, 22 ), 8,
148                 'unshift returns size of the new array' );
149         }, undef, 'unshifted two values and lived' );
150
151         is_deeply(
152             $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
153             'unshift changed the value of the array in the object'
154         );
155
156         is( exception { $obj->unshift() }, undef, 'call to unshift without arguments lives' );
157
158         is( $obj->pop, 3, 'pop returns the last value in the array' );
159
160         is_deeply(
161             $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
162             'pop changed the value of the array in the object'
163         );
164
165         like( exception { $obj->pop(42) }, qr/Cannot call pop with any arguments/, 'call to pop with arguments dies' );
166
167         is( $obj->shift, 101, 'shift returns the first value' );
168
169         like( exception { $obj->shift(42) }, qr/Cannot call shift with any arguments/, 'call to shift with arguments dies' );
170
171         is_deeply(
172             $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
173             'shift changed the value of the array in the object'
174         );
175
176         is_deeply(
177             [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
178             'call to elements returns values as a list'
179         );
180
181         like( exception { $obj->elements(22) }, qr/Cannot call elements with any arguments/, 'throws an error when passing an argument passed to elements' );
182
183         $obj->_values( [ 1, 2, 3 ] );
184
185         is( $obj->get(0),      1, 'get values at index 0' );
186         is( $obj->get(1),      2, 'get values at index 1' );
187         is( $obj->get(2),      3, 'get values at index 2' );
188         is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
189
190         like( exception { $obj->get() }, qr/Cannot call get without at least 1 argument/, 'throws an error when get is called without any arguments' );
191
192         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' );
193
194         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' );
195
196         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' );
197
198         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' );
199
200         is( exception {
201             is( $obj->set( 1, 100 ), 100, 'set returns new value' );
202         }, undef, 'set value at index 1 lives' );
203
204         is( $obj->get(1), 100, 'get value at index 1 returns new value' );
205
206
207         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' );
208
209         is( exception { $obj->set_curried_1(99) }, undef, 'set_curried_1 lives' );
210
211         is( $obj->get(1), 99, 'get value at index 1 returns new value' );
212
213         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' );
214
215         is( exception { $obj->set_curried_2 }, undef, 'set_curried_2 lives' );
216
217         is( $obj->get(1), 98, 'get value at index 1 returns new value' );
218
219         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' );
220
221         is(
222             $obj->accessor(1), 98,
223             'accessor with one argument returns value at index 1'
224         );
225
226         is( exception {
227             is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' );
228         }, undef, 'accessor as writer lives' );
229
230         like(
231             exception {
232                 $obj->accessor;
233             },
234             qr/Cannot call accessor without at least 1 argument/,
235             'throws an error when accessor is called without arguments'
236         );
237
238         is(
239             $obj->get(1), 97,
240             'accessor set value at index 1'
241         );
242
243         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' );
244
245         is(
246             $obj->accessor_curried_1, 97,
247             'accessor_curried_1 returns expected value when called with no arguments'
248         );
249
250         is( exception { $obj->accessor_curried_1(95) }, undef, 'accessor_curried_1 as writer lives' );
251
252         is(
253             $obj->get(1), 95,
254             'accessor_curried_1 set value at index 1'
255         );
256
257         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' );
258
259         is( exception { $obj->accessor_curried_2 }, undef, 'accessor_curried_2 as writer lives' );
260
261         is(
262             $obj->get(1), 90,
263             'accessor_curried_2 set value at index 1'
264         );
265
266         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' );
267
268         is( exception { $obj->clear }, undef, 'clear lives' );
269
270         ok( $obj->is_empty, 'values is empty after call to clear' );
271
272         $obj->set( 0 => 42 );
273
274         like( exception { $obj->clear(50) }, qr/Cannot call clear with any arguments/, 'throws an error when clear is called with an argument' );
275
276         ok(
277             !$obj->is_empty,
278             'values is not empty after failed call to clear'
279         );
280
281         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' );
282
283         $obj->clear;
284         is(
285             $obj->push( 1, 5, 10, 42 ), 4,
286             'pushed 4 elements, got number of elements in the array back'
287         );
288
289         is( exception {
290             is( $obj->delete(2), 10, 'delete returns deleted value' );
291         }, undef, 'delete lives' );
292
293         is_deeply(
294             $obj->_values, [ 1, 5, 42 ],
295             'delete removed the specified element'
296         );
297
298         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' );
299
300         is( exception { $obj->delete_curried }, undef, 'delete_curried lives' );
301
302         is_deeply(
303             $obj->_values, [ 1, 42 ],
304             'delete removed the specified element'
305         );
306
307         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' );
308
309         is( exception { $obj->insert( 1, 21 ) }, undef, 'insert lives' );
310
311         is_deeply(
312             $obj->_values, [ 1, 21, 42 ],
313             'insert added the specified element'
314         );
315
316         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' );
317
318         is( exception {
319             is_deeply(
320                 [ $obj->splice( 1, 0, 2, 3 ) ],
321                 [],
322                 'return value of splice is empty list when not removing elements'
323             );
324         }, undef, 'splice lives' );
325
326         is_deeply(
327             $obj->_values, [ 1, 2, 3, 21, 42 ],
328             'splice added the specified elements'
329         );
330
331         is( exception {
332             is_deeply(
333                 [ $obj->splice( 1, 2, 99 ) ],
334                 [ 2, 3 ],
335                 'splice returns list of removed values'
336             );
337         }, undef, 'splice lives' );
338
339         is_deeply(
340             $obj->_values, [ 1, 99, 21, 42 ],
341             'splice added the specified elements'
342         );
343
344         like( exception { $obj->splice() }, qr/Cannot call splice without at least 1 argument/, 'throws an error when splice is called with no arguments' );
345
346         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' );
347
348         is( exception { $obj->splice_curried_1( 2, 101 ) }, undef, 'splice_curried_1 lives' );
349
350         is_deeply(
351             $obj->_values, [ 1, 101, 42 ],
352             'splice added the specified elements'
353         );
354
355         is( exception { $obj->splice_curried_2(102) }, undef, 'splice_curried_2 lives' );
356
357         is_deeply(
358             $obj->_values, [ 1, 102 ],
359             'splice added the specified elements'
360         );
361
362         is( exception { $obj->splice_curried_all }, undef, 'splice_curried_all lives' );
363
364         is_deeply(
365             $obj->_values, [ 1, 3, 4, 5 ],
366             'splice added the specified elements'
367         );
368
369         is_deeply(
370             scalar $obj->splice( 1, 2 ),
371             4,
372             'splice in scalar context returns last element removed'
373         );
374
375         is_deeply(
376             scalar $obj->splice( 1, 0, 42 ),
377             undef,
378             'splice in scalar context returns undef when no elements are removed'
379         );
380
381         $obj->_values( [ 3, 9, 5, 22, 11 ] );
382
383         is_deeply(
384             [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
385             'sort returns sorted values'
386         );
387
388         is_deeply(
389             [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
390             'sort returns values sorted by provided function'
391         );
392
393         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' );
394
395         like( exception {
396             $obj->sort( sub { }, 27 );
397         }, qr/Cannot call sort with more than 1 argument/, 'throws an error when passing two arguments to sort' );
398
399         $obj->_values( [ 3, 9, 5, 22, 11 ] );
400
401         $obj->sort_in_place;
402
403         is_deeply(
404             $obj->_values, [ 11, 22, 3, 5, 9 ],
405             'sort_in_place sorts values'
406         );
407
408         $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
409
410         is_deeply(
411             $obj->_values, [ 3, 5, 9, 11, 22 ],
412             'sort_in_place with function sorts values'
413         );
414
415         like( exception {
416             $obj->sort_in_place( 27 );
417         }, 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' );
418
419         like( exception {
420             $obj->sort_in_place( sub { }, 27 );
421         }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing two arguments to sort_in_place' );
422
423         $obj->_values( [ 3, 9, 5, 22, 11 ] );
424
425         $obj->sort_in_place_curried;
426
427         is_deeply(
428             $obj->_values, [ 22, 11, 9, 5, 3 ],
429             'sort_in_place_curried sorts values'
430         );
431
432         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' );
433
434         $obj->_values( [ 1 .. 5 ] );
435
436         is_deeply(
437             [ $obj->map( sub { $_ + 1 } ) ],
438             [ 2 .. 6 ],
439             'map returns the expected values'
440         );
441
442         like( exception { $obj->map }, qr/Cannot call map without at least 1 argument/, 'throws an error when passing no arguments to map' );
443
444         like( exception {
445             $obj->map( sub { }, 2 );
446         }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing two arguments to map' );
447
448         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' );
449
450         $obj->_values( [ 1 .. 5 ] );
451
452         is_deeply(
453             [ $obj->map_curried ],
454             [ 2 .. 6 ],
455             'map_curried returns the expected values'
456         );
457
458         like( exception {
459             $obj->map_curried( sub { } );
460         }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing one argument passed to map_curried' );
461
462         $obj->_values( [ 2 .. 9 ] );
463
464         is_deeply(
465             [ $obj->grep( sub { $_ < 5 } ) ],
466             [ 2 .. 4 ],
467             'grep returns the expected values'
468         );
469
470         like( exception { $obj->grep }, qr/Cannot call grep without at least 1 argument/, 'throws an error when passing no arguments to grep' );
471
472         like( exception {
473             $obj->grep( sub { }, 2 );
474         }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing two arguments to grep' );
475
476         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' );
477
478         my $overloader = Overloader->new( sub { $_ < 5 } );
479         is_deeply(
480             [ $obj->grep($overloader) ],
481             [ 2 .. 4 ],
482             'grep works with obj that overload code dereferencing'
483         );
484
485         is_deeply(
486             [ $obj->grep_curried ],
487             [ 2 .. 4 ],
488             'grep_curried returns the expected values'
489         );
490
491         like( exception {
492             $obj->grep_curried( sub { } );
493         }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing one argument passed to grep_curried' );
494
495         $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
496
497         is(
498             $obj->first( sub { $_ % 2 } ),
499             99,
500             'first returns expected value'
501         );
502
503         like( exception { $obj->first }, qr/Cannot call first without at least 1 argument/, 'throws an error when passing no arguments to first' );
504
505         like( exception {
506             $obj->first( sub { }, 2 );
507         }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing two arguments to first' );
508
509         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' );
510
511         is(
512             $obj->first_curried,
513             99,
514             'first_curried returns expected value'
515         );
516
517         like( exception {
518             $obj->first_curried( sub { } );
519         }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
520
521         $obj->_values( [ 1 .. 4 ] );
522
523         is(
524             $obj->join('-'), '1-2-3-4',
525             'join returns expected result'
526         );
527
528         is(
529             $obj->join(q{}), '1234',
530             'join returns expected result when joining with empty string'
531         );
532
533         like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
534
535         like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
536
537         like( exception { $obj->join( {} ) }, qr/The argument passed to join must be a string/, 'throws an error when passing a non string to join' );
538
539         is_deeply(
540             [ sort $obj->shuffle ],
541             [ 1 .. 4 ],
542             'shuffle returns all values (cannot check for a random order)'
543         );
544
545         like( exception { $obj->shuffle(2) }, qr/Cannot call shuffle with any arguments/, 'throws an error when passing an argument passed to shuffle' );
546
547         $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
548
549         is_deeply(
550             [ $obj->uniq ],
551             [ 1 .. 4, 5, 7 ],
552             'uniq returns expected values (in original order)'
553         );
554
555         like( exception { $obj->uniq(2) }, qr/Cannot call uniq with any arguments/, 'throws an error when passing an argument passed to uniq' );
556
557         $obj->_values( [ 1 .. 5 ] );
558
559         is(
560             $obj->reduce( sub { $_[0] * $_[1] } ),
561             120,
562             'reduce returns expected value'
563         );
564
565         like( exception { $obj->reduce }, qr/Cannot call reduce without at least 1 argument/, 'throws an error when passing no arguments to reduce' );
566
567         like( exception {
568             $obj->reduce( sub { }, 2 );
569         }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing two arguments to reduce' );
570
571         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' );
572
573         is(
574             $obj->reduce_curried,
575             120,
576             'reduce_curried returns expected value'
577         );
578
579         like( exception {
580             $obj->reduce_curried( sub { } );
581         }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing one argument passed to reduce_curried' );
582
583         $obj->_values( [ 1 .. 6 ] );
584
585         my $it = $obj->natatime(2);
586         my @nat;
587         while ( my @v = $it->() ) {
588             push @nat, \@v;
589         }
590
591         is_deeply(
592             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
593             \@nat,
594             'natatime returns expected iterator'
595         );
596
597         @nat = ();
598         $obj->natatime( 2, sub { push @nat, [@_] } );
599
600         is_deeply(
601             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
602             \@nat,
603             'natatime with function returns expected value'
604         );
605
606         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' );
607
608         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' );
609
610         $it = $obj->natatime_curried();
611         @nat = ();
612         while ( my @v = $it->() ) {
613             push @nat, \@v;
614         }
615
616         is_deeply(
617             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
618             \@nat,
619             'natatime_curried returns expected iterator'
620         );
621
622         @nat = ();
623         $obj->natatime_curried( sub { push @nat, [@_] } );
624
625         is_deeply(
626             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
627             \@nat,
628             'natatime_curried with function returns expected value'
629         );
630
631         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' );
632
633         if ( $class->meta->get_attribute('_values')->is_lazy ) {
634             my $obj = $class->new;
635
636             is( $obj->count, 2, 'count is 2 (lazy init)' );
637
638             $obj->_clear_values;
639
640             is_deeply(
641                 [ $obj->elements ], [ 42, 84 ],
642                 'elements contains default with lazy init'
643             );
644
645             $obj->_clear_values;
646
647             $obj->push(2);
648
649             is_deeply(
650                 $obj->_values, [ 42, 84, 2 ],
651                 'push works with lazy init'
652             );
653
654             $obj->_clear_values;
655
656             $obj->unshift( 3, 4 );
657
658             is_deeply(
659                 $obj->_values, [ 3, 4, 42, 84 ],
660                 'unshift works with lazy init'
661             );
662         }
663     }
664     $class;
665 }
666
667 done_testing;