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