add some more tests (including some TODOs)
[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::MetaRole;
10 use Moose::Util::TypeConstraints;
11 use NoInlineAttribute;
12 use Test::More;
13 use Test::Exception;
14 use Test::Moose;
15
16 {
17     my %handles = (
18         count    => 'count',
19         elements => 'elements',
20         is_empty => 'is_empty',
21         push     => 'push',
22         push_curried =>
23             [ push => 42, 84 ],
24         unshift => 'unshift',
25         unshift_curried =>
26             [ unshift => 42, 84 ],
27         pop           => 'pop',
28         shift         => 'shift',
29         get           => 'get',
30         get_curried   => [ get => 1 ],
31         set           => 'set',
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 ) ],
37         clear          => 'clear',
38         delete         => 'delete',
39         delete_curried => [ delete => 1 ],
40         insert         => 'insert',
41         insert_curried => [ insert => ( 1, 101 ) ],
42         splice         => 'splice',
43         splice_curried_1   => [ splice => 1 ],
44         splice_curried_2   => [ splice => 1, 2 ],
45         splice_curried_all => [ splice => 1, 2, ( 3, 4, 5 ) ],
46         sort          => 'sort',
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] } ) ],
51         map           => 'map',
52         map_curried   => [ map => ( sub { $_ + 1 } ) ],
53         grep          => 'grep',
54         grep_curried  => [ grep => ( sub { $_ < 5 } ) ],
55         first         => 'first',
56         first_curried => [ first => ( sub { $_ % 2 } ) ],
57         join          => 'join',
58         join_curried => [ join => '-' ],
59         shuffle      => 'shuffle',
60         uniq         => 'uniq',
61         reduce       => 'reduce',
62         reduce_curried => [ reduce => ( sub { $_[0] * $_[1] } ) ],
63         natatime       => 'natatime',
64         natatime_curried => [ natatime => 2 ],
65     );
66
67     my $name = 'Foo1';
68
69     sub build_class {
70         my %attr = @_;
71
72         my $class = Moose::Meta::Class->create(
73             $name++,
74             superclasses => ['Moose::Object'],
75         );
76
77         my @traits = 'Array';
78         push @traits, 'NoInlineAttribute'
79             if delete $attr{no_inline};
80
81         $class->add_attribute(
82             _values => (
83                 traits  => \@traits,
84                 is      => 'rw',
85                 isa     => 'ArrayRef[Int]',
86                 default => sub { [] },
87                 handles => \%handles,
88                 clearer => '_clear_values',
89                 %attr,
90             ),
91         );
92
93         return ( $class->name, \%handles );
94     }
95 }
96
97 {
98     package Overloader;
99
100     use overload
101         '&{}' => sub { ${ $_[0] } },
102         bool  => sub {1};
103
104     sub new {
105         bless \$_[1], $_[0];
106     }
107 }
108
109 {
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 ) );
114
115     # Will force the inlining code to check the entire arrayref when it is modified.
116     subtype 'MyArrayRef', as 'ArrayRef', where { 1 };
117
118     run_tests( build_class( isa => 'MyArrayRef' ) );
119
120     coerce 'MyArrayRef', from 'ArrayRef', via { $_ };
121
122     run_tests( build_class( isa => 'MyArrayRef', coerce => 1 ) );
123 }
124
125 sub run_tests {
126     my ( $class, $handles ) = @_;
127
128     can_ok( $class, $_ ) for sort keys %{$handles};
129
130     with_immutable {
131         my $obj = $class->new( _values => [ 10, 12, 42 ] );
132
133         is_deeply(
134             $obj->_values, [ 10, 12, 42 ],
135             'values can be set in constructor'
136         );
137
138         ok( !$obj->is_empty, 'values is not empty' );
139         is( $obj->count, 3, 'count returns 3' );
140
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';
144
145         lives_ok { $obj->push( 1, 2, 3 ) }
146         'pushed three new values and lived';
147
148         lives_ok { $obj->push() } 'call to push without arguments lives';
149
150         lives_ok { $obj->unshift( 101, 22 ) }
151         'unshifted two values and lived';
152
153         is_deeply(
154             $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
155             'unshift changed the value of the array in the object'
156         );
157
158         lives_ok { $obj->unshift() }
159         'call to unshift without arguments lives';
160
161         is( $obj->pop, 3, 'pop returns the last value in the array' );
162
163         is_deeply(
164             $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
165             'pop changed the value of the array in the object'
166         );
167
168         throws_ok { $obj->pop(42) }
169         qr/Cannot call pop with any arguments/,
170             'call to pop with arguments dies';
171
172         is( $obj->shift, 101, 'shift returns the first value' );
173
174         throws_ok { $obj->shift(42) }
175         qr/Cannot call shift with any arguments/,
176             'call to shift with arguments dies';
177
178         is_deeply(
179             $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
180             'shift changed the value of the array in the object'
181         );
182
183         is_deeply(
184             [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
185             'call to elements returns values as a list'
186         );
187
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';
191
192         $obj->_values( [ 1, 2, 3 ] );
193
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' );
198
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';
202
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';
206
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';
210
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';
214
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';
218
219         lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
220
221         is( $obj->get(1), 100, 'get value at index 1 returns new value' );
222
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';
226
227         lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
228
229         is( $obj->get(1), 99, 'get value at index 1 returns new value' );
230
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';
234
235         lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
236
237         is( $obj->get(1), 98, 'get value at index 1 returns new value' );
238
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';
242
243         is(
244             $obj->accessor(1), 98,
245             'accessor with one argument returns value at index 1'
246         );
247
248         lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
249
250         is(
251             $obj->get(1), 97,
252             'accessor set value at index 1'
253         );
254
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';
258
259         is(
260             $obj->accessor_curried_1, 97,
261             'accessor_curried_1 returns expected value when called with no arguments'
262         );
263
264         lives_ok { $obj->accessor_curried_1(95) }
265         'accessor_curried_1 as writer lives';
266
267         is(
268             $obj->get(1), 95,
269             'accessor_curried_1 set value at index 1'
270         );
271
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';
275
276         lives_ok { $obj->accessor_curried_2 }
277         'accessor_curried_2 as writer lives';
278
279         is(
280             $obj->get(1), 90,
281             'accessor_curried_2 set value at index 1'
282         );
283
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';
287
288         lives_ok { $obj->clear } 'clear lives';
289
290         ok( $obj->is_empty, 'values is empty after call to clear' );
291
292         $obj->set( 0 => 42 );
293
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';
297
298         ok(
299             !$obj->is_empty,
300             'values is not empty after failed call to clear'
301         );
302
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';
306
307         $obj->clear;
308         $obj->push( 1, 5, 10, 42 );
309
310         lives_ok { $obj->delete(2) } 'delete lives';
311
312         is_deeply(
313             $obj->_values, [ 1, 5, 42 ],
314             'delete removed the specified element'
315         );
316
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';
320
321         lives_ok { $obj->delete_curried } 'delete_curried lives';
322
323         is_deeply(
324             $obj->_values, [ 1, 42 ],
325             'delete removed the specified element'
326         );
327
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';
331
332         lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
333
334         is_deeply(
335             $obj->_values, [ 1, 21, 42 ],
336             'insert added the specified element'
337         );
338
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';
342
343         lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
344
345         is_deeply(
346             $obj->_values, [ 1, 2, 3, 21, 42 ],
347             'splice added the specified elements'
348         );
349
350         lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
351
352         is_deeply(
353             $obj->_values, [ 1, 99, 3, 21, 42 ],
354             'splice added the specified elements'
355         );
356
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';
360
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';
364
365         lives_ok { $obj->splice_curried_1( 2, 101 ) }
366         'splice_curried_1 lives';
367
368         is_deeply(
369             $obj->_values, [ 1, 101, 21, 42 ],
370             'splice added the specified elements'
371         );
372
373         lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
374
375         is_deeply(
376             $obj->_values, [ 1, 102, 42 ],
377             'splice added the specified elements'
378         );
379
380         lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
381
382         is_deeply(
383             $obj->_values, [ 1, 3, 4, 5 ],
384             'splice added the specified elements'
385         );
386
387         $obj->_values( [ 3, 9, 5, 22, 11 ] );
388
389         is_deeply(
390             [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
391             'sort returns sorted values'
392         );
393
394         is_deeply(
395             [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
396             'sort returns values sorted by provided function'
397         );
398
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';
402
403         throws_ok {
404             $obj->sort( sub { }, 27 );
405         }
406         qr/Cannot call sort with more than 1 argument/,
407             'throws an error when passing two arguments to sort';
408
409         $obj->_values( [ 3, 9, 5, 22, 11 ] );
410
411         $obj->sort_in_place;
412
413         is_deeply(
414             $obj->_values, [ 11, 22, 3, 5, 9 ],
415             'sort_in_place sorts values'
416         );
417
418         $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
419
420         is_deeply(
421             $obj->_values, [ 3, 5, 9, 11, 22 ],
422             'sort_in_place with function sorts values'
423         );
424
425         throws_ok {
426             $obj->sort_in_place( 27 );
427         }
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';
430
431         throws_ok {
432             $obj->sort_in_place( sub { }, 27 );
433         }
434         qr/Cannot call sort_in_place with more than 1 argument/,
435             'throws an error when passing two arguments to sort_in_place';
436
437         $obj->_values( [ 3, 9, 5, 22, 11 ] );
438
439         $obj->sort_in_place_curried;
440
441         is_deeply(
442             $obj->_values, [ 22, 11, 9, 5, 3 ],
443             'sort_in_place_curried sorts values'
444         );
445
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';
449
450         $obj->_values( [ 1 .. 5 ] );
451
452         is_deeply(
453             [ $obj->map( sub { $_ + 1 } ) ],
454             [ 2 .. 6 ],
455             'map returns the expected values'
456         );
457
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';
461
462         throws_ok {
463             $obj->map( sub { }, 2 );
464         }
465         qr/Cannot call map with more than 1 argument/,
466             'throws an error when passing two arguments to map';
467
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';
471
472         $obj->_values( [ 1 .. 5 ] );
473
474         is_deeply(
475             [ $obj->map_curried ],
476             [ 2 .. 6 ],
477             'map_curried returns the expected values'
478         );
479
480         throws_ok {
481             $obj->map_curried( sub { } );
482         }
483         qr/Cannot call map with more than 1 argument/,
484             'throws an error when passing one argument passed to map_curried';
485
486         $obj->_values( [ 2 .. 9 ] );
487
488         is_deeply(
489             [ $obj->grep( sub { $_ < 5 } ) ],
490             [ 2 .. 4 ],
491             'grep returns the expected values'
492         );
493
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';
497
498         throws_ok {
499             $obj->grep( sub { }, 2 );
500         }
501         qr/Cannot call grep with more than 1 argument/,
502             'throws an error when passing two arguments to grep';
503
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';
507
508         my $overloader = Overloader->new( sub { $_ < 5 } );
509         is_deeply(
510             [ $obj->grep($overloader) ],
511             [ 2 .. 4 ],
512             'grep works with obj that overload code dereferencing'
513         );
514
515         is_deeply(
516             [ $obj->grep_curried ],
517             [ 2 .. 4 ],
518             'grep_curried returns the expected values'
519         );
520
521         throws_ok {
522             $obj->grep_curried( sub { } );
523         }
524         qr/Cannot call grep with more than 1 argument/,
525             'throws an error when passing one argument passed to grep_curried';
526
527         $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
528
529         is(
530             $obj->first( sub { $_ % 2 } ),
531             99,
532             'first returns expected value'
533         );
534
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';
538
539         throws_ok {
540             $obj->first( sub { }, 2 );
541         }
542         qr/Cannot call first with more than 1 argument/,
543             'throws an error when passing two arguments to first';
544
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';
548
549         is(
550             $obj->first_curried,
551             99,
552             'first_curried returns expected value'
553         );
554
555         throws_ok {
556             $obj->first_curried( sub { } );
557         }
558         qr/Cannot call first with more than 1 argument/,
559             'throws an error when passing one argument passed to first_curried';
560
561         $obj->_values( [ 1 .. 4 ] );
562
563         is(
564             $obj->join('-'), '1-2-3-4',
565             'join returns expected result'
566         );
567
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';
571
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';
575
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';
579
580         is_deeply(
581             [ sort $obj->shuffle ],
582             [ 1 .. 4 ],
583             'shuffle returns all values (cannot check for a random order)'
584         );
585
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';
589
590         $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
591
592         is_deeply(
593             [ $obj->uniq ],
594             [ 1 .. 4, 5, 7 ],
595             'uniq returns expected values (in original order)'
596         );
597
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';
601
602         $obj->_values( [ 1 .. 5 ] );
603
604         is(
605             $obj->reduce( sub { $_[0] * $_[1] } ),
606             120,
607             'reduce returns expected value'
608         );
609
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';
613
614         throws_ok {
615             $obj->reduce( sub { }, 2 );
616         }
617         qr/Cannot call reduce with more than 1 argument/,
618             'throws an error when passing two arguments to reduce';
619
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';
623
624         is(
625             $obj->reduce_curried,
626             120,
627             'reduce_curried returns expected value'
628         );
629
630         throws_ok {
631             $obj->reduce_curried( sub { } );
632         }
633         qr/Cannot call reduce with more than 1 argument/,
634             'throws an error when passing one argument passed to reduce_curried';
635
636         $obj->_values( [ 1 .. 6 ] );
637
638         my $it = $obj->natatime(2);
639         my @nat;
640         while ( my @v = $it->() ) {
641             push @nat, \@v;
642         }
643
644         is_deeply(
645             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
646             \@nat,
647             'natatime returns expected iterator'
648         );
649
650         @nat = ();
651         $obj->natatime( 2, sub { push @nat, [@_] } );
652
653         is_deeply(
654             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
655             \@nat,
656             'natatime with function returns expected value'
657         );
658
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';
662
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';
666
667         $it = $obj->natatime_curried();
668         @nat = ();
669         while ( my @v = $it->() ) {
670             push @nat, \@v;
671         }
672
673         is_deeply(
674             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
675             \@nat,
676             'natatime_curried returns expected iterator'
677         );
678
679         @nat = ();
680         $obj->natatime_curried( sub { push @nat, [@_] } );
681
682         is_deeply(
683             [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
684             \@nat,
685             'natatime_curried with function returns expected value'
686         );
687
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';
691
692         if ( $class->meta->get_attribute('_values')->is_lazy ) {
693             my $obj = $class->new;
694
695             is( $obj->count, 2, 'count is 2 (lazy init)' );
696
697             $obj->_clear_values;
698
699             is_deeply(
700                 [ $obj->elements ], [ 42, 84 ],
701                 'elements contains default with lazy init'
702             );
703
704             $obj->_clear_values;
705
706             $obj->push(2);
707
708             is_deeply(
709                 $obj->_values, [ 42, 84, 2 ],
710                 'push works with lazy init'
711             );
712
713             $obj->_clear_values;
714
715             $obj->unshift( 3, 4 );
716
717             is_deeply(
718                 $obj->_values, [ 3, 4, 42, 84 ],
719                 'unshift works with lazy init'
720             );
721         }
722     }
723     $class;
724 }
725
726 done_testing;