require CMOP 1.10
[gitmo/Moose.git] / t / 070_native_traits / 010_trait_array.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8b9641b8 6use lib 't/lib';
7
a7821be5 8use Moose ();
8b9641b8 9use Moose::Util::MetaRole;
bbeb5798 10use Moose::Util::TypeConstraints;
8b9641b8 11use NoInlineAttribute;
a28e50e4 12use Test::More;
e3c07b19 13use Test::Exception;
37ecda0d 14use Test::Moose;
d50fc84a 15
e3c07b19 16{
a7821be5 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 ],
e3c07b19 65 );
a7821be5 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
8b9641b8 77 my @traits = 'Array';
78 push @traits, 'NoInlineAttribute'
79 if delete $attr{no_inline};
80
a7821be5 81 $class->add_attribute(
82 _values => (
8b9641b8 83 traits => \@traits,
a7821be5 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 }
e3c07b19 95}
96
862b6081 97{
88e88a7b 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{
a7821be5 110 run_tests(build_class);
111 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
cf0da4e2 112 run_tests( build_class( trigger => sub { } ) );
8b9641b8 113 run_tests( build_class( no_inline => 1 ) );
bbeb5798 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 ) );
a7821be5 123}
e3c07b19 124
a7821be5 125sub run_tests {
126 my ( $class, $handles ) = @_;
e3c07b19 127
a7821be5 128 can_ok( $class, $_ ) for sort keys %{$handles};
e3c07b19 129
a7821be5 130 with_immutable {
131 my $obj = $class->new( _values => [ 10, 12, 42 ] );
e3c07b19 132
a7821be5 133 is_deeply(
134 $obj->_values, [ 10, 12, 42 ],
135 'values can be set in constructor'
136 );
e3c07b19 137
a7821be5 138 ok( !$obj->is_empty, 'values is not empty' );
139 is( $obj->count, 3, 'count returns 3' );
e3c07b19 140
a7821be5 141 throws_ok { $obj->count(22) }
142 qr/Cannot call count with any arguments/,
e3181911 143 'throws an error when passing an argument passed to count';
e3c07b19 144
a7821be5 145 lives_ok { $obj->push( 1, 2, 3 ) }
146 'pushed three new values and lived';
e3c07b19 147
a7821be5 148 lives_ok { $obj->push() } 'call to push without arguments lives';
e3c07b19 149
a7821be5 150 lives_ok { $obj->unshift( 101, 22 ) }
151 'unshifted two values and lived';
e3c07b19 152
e32b7489 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
a7821be5 158 lives_ok { $obj->unshift() }
159 'call to unshift without arguments lives';
e3c07b19 160
a7821be5 161 is( $obj->pop, 3, 'pop returns the last value in the array' );
910684ee 162
a7821be5 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 );
910684ee 167
a7821be5 168 throws_ok { $obj->pop(42) }
169 qr/Cannot call pop with any arguments/,
170 'call to pop with arguments dies';
910684ee 171
a7821be5 172 is( $obj->shift, 101, 'shift returns the first value' );
910684ee 173
a7821be5 174 throws_ok { $obj->shift(42) }
175 qr/Cannot call shift with any arguments/,
176 'call to shift with arguments dies';
e3c07b19 177
a7821be5 178 is_deeply(
179 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
180 'shift changed the value of the array in the object'
181 );
e3c07b19 182
a7821be5 183 is_deeply(
184 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
185 'call to elements returns values as a list'
186 );
e3c07b19 187
a7821be5 188 throws_ok { $obj->elements(22) }
189 qr/Cannot call elements with any arguments/,
e3181911 190 'throws an error when passing an argument passed to elements';
e3c07b19 191
a7821be5 192 $obj->_values( [ 1, 2, 3 ] );
e3c07b19 193
a7821be5 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' );
e3c07b19 198
a7821be5 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';
e3c07b19 202
a7821be5 203 throws_ok { $obj->get( {} ) }
e3181911 204 qr/The index passed to get must be an integer/,
a7821be5 205 'throws an error when get is called with an invalid argument';
e3c07b19 206
a7821be5 207 throws_ok { $obj->get(2.2) }
e3181911 208 qr/The index passed to get must be an integer/,
a7821be5 209 'throws an error when get is called with an invalid argument';
e3c07b19 210
a7821be5 211 throws_ok { $obj->get('foo') }
e3181911 212 qr/The index passed to get must be an integer/,
a7821be5 213 'throws an error when get is called with an invalid argument';
e3c07b19 214
a7821be5 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';
e3c07b19 218
a7821be5 219 lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives';
e3c07b19 220
a7821be5 221 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
e3c07b19 222
a7821be5 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';
59de9de4 226
a7821be5 227 lives_ok { $obj->set_curried_1(99) } 'set_curried_1 lives';
59de9de4 228
a7821be5 229 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
59de9de4 230
a7821be5 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';
59de9de4 234
a7821be5 235 lives_ok { $obj->set_curried_2 } 'set_curried_2 lives';
59de9de4 236
a7821be5 237 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
59de9de4 238
a7821be5 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';
59de9de4 242
a7821be5 243 is(
244 $obj->accessor(1), 98,
245 'accessor with one argument returns value at index 1'
246 );
59de9de4 247
a7821be5 248 lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives';
59de9de4 249
a7821be5 250 is(
251 $obj->get(1), 97,
252 'accessor set value at index 1'
253 );
862b6081 254
a7821be5 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';
59de9de4 258
a7821be5 259 is(
260 $obj->accessor_curried_1, 97,
261 'accessor_curried_1 returns expected value when called with no arguments'
262 );
59de9de4 263
a7821be5 264 lives_ok { $obj->accessor_curried_1(95) }
265 'accessor_curried_1 as writer lives';
862b6081 266
a7821be5 267 is(
268 $obj->get(1), 95,
269 'accessor_curried_1 set value at index 1'
270 );
862b6081 271
a7821be5 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';
862b6081 275
a7821be5 276 lives_ok { $obj->accessor_curried_2 }
277 'accessor_curried_2 as writer lives';
862b6081 278
a7821be5 279 is(
280 $obj->get(1), 90,
281 'accessor_curried_2 set value at index 1'
282 );
862b6081 283
a7821be5 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';
862b6081 287
a7821be5 288 lives_ok { $obj->clear } 'clear lives';
862b6081 289
a7821be5 290 ok( $obj->is_empty, 'values is empty after call to clear' );
862b6081 291
a7821be5 292 $obj->set( 0 => 42 );
862b6081 293
a7821be5 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';
862b6081 297
a7821be5 298 ok(
299 !$obj->is_empty,
300 'values is not empty after failed call to clear'
301 );
59de9de4 302
8b9641b8 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
a7821be5 307 $obj->clear;
308 $obj->push( 1, 5, 10, 42 );
59de9de4 309
a7821be5 310 lives_ok { $obj->delete(2) } 'delete lives';
59de9de4 311
a7821be5 312 is_deeply(
313 $obj->_values, [ 1, 5, 42 ],
314 'delete removed the specified element'
315 );
e3c07b19 316
a7821be5 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';
59de9de4 320
a7821be5 321 lives_ok { $obj->delete_curried } 'delete_curried lives';
910684ee 322
a7821be5 323 is_deeply(
324 $obj->_values, [ 1, 42 ],
325 'delete removed the specified element'
326 );
910684ee 327
a7821be5 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';
e3c07b19 331
a7821be5 332 lives_ok { $obj->insert( 1, 21 ) } 'insert lives';
910684ee 333
a7821be5 334 is_deeply(
335 $obj->_values, [ 1, 21, 42 ],
336 'insert added the specified element'
337 );
910684ee 338
a7821be5 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';
59de9de4 342
a7821be5 343 lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives';
910684ee 344
a7821be5 345 is_deeply(
346 $obj->_values, [ 1, 2, 3, 21, 42 ],
347 'splice added the specified elements'
348 );
910684ee 349
a7821be5 350 lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives';
e3c07b19 351
a7821be5 352 is_deeply(
353 $obj->_values, [ 1, 99, 3, 21, 42 ],
354 'splice added the specified elements'
355 );
e3c07b19 356
a7821be5 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';
910684ee 360
e3181911 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
a7821be5 365 lives_ok { $obj->splice_curried_1( 2, 101 ) }
366 'splice_curried_1 lives';
910684ee 367
a7821be5 368 is_deeply(
369 $obj->_values, [ 1, 101, 21, 42 ],
370 'splice added the specified elements'
371 );
59de9de4 372
a7821be5 373 lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives';
862b6081 374
a7821be5 375 is_deeply(
376 $obj->_values, [ 1, 102, 42 ],
377 'splice added the specified elements'
378 );
862b6081 379
a7821be5 380 lives_ok { $obj->splice_curried_all } 'splice_curried_all lives';
862b6081 381
a7821be5 382 is_deeply(
383 $obj->_values, [ 1, 3, 4, 5 ],
384 'splice added the specified elements'
385 );
59de9de4 386
a7821be5 387 $obj->_values( [ 3, 9, 5, 22, 11 ] );
862b6081 388
a7821be5 389 is_deeply(
390 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
391 'sort returns sorted values'
392 );
862b6081 393
a7821be5 394 is_deeply(
395 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
396 'sort returns values sorted by provided function'
397 );
910684ee 398
a7821be5 399 throws_ok { $obj->sort(1) }
e3181911 400 qr/The argument passed to sort must be a code reference/,
401 'throws an error when passing a non coderef to sort';
910684ee 402
a7821be5 403 throws_ok {
404 $obj->sort( sub { }, 27 );
405 }
406 qr/Cannot call sort with more than 1 argument/,
e3181911 407 'throws an error when passing two arguments to sort';
910684ee 408
a7821be5 409 $obj->_values( [ 3, 9, 5, 22, 11 ] );
862b6081 410
a7821be5 411 $obj->sort_in_place;
d50fc84a 412
a7821be5 413 is_deeply(
414 $obj->_values, [ 11, 22, 3, 5, 9 ],
415 'sort_in_place sorts values'
416 );
862b6081 417
a7821be5 418 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
862b6081 419
a7821be5 420 is_deeply(
421 $obj->_values, [ 3, 5, 9, 11, 22 ],
422 'sort_in_place with function sorts values'
423 );
424
425 throws_ok {
e3181911 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 {
a7821be5 432 $obj->sort_in_place( sub { }, 27 );
433 }
434 qr/Cannot call sort_in_place with more than 1 argument/,
e3181911 435 'throws an error when passing two arguments to sort_in_place';
a7821be5 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/,
e3181911 448 'throws an error when passing one argument passed to sort_in_place_curried';
a7821be5 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/,
e3181911 460 'throws an error when passing no arguments to map';
a7821be5 461
462 throws_ok {
463 $obj->map( sub { }, 2 );
464 }
465 qr/Cannot call map with more than 1 argument/,
e3181911 466 'throws an error when passing two arguments to map';
a7821be5 467
468 throws_ok { $obj->map( {} ) }
e3181911 469 qr/The argument passed to map must be a code reference/,
470 'throws an error when passing a non coderef to map';
a7821be5 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/,
e3181911 484 'throws an error when passing one argument passed to map_curried';
a7821be5 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/,
e3181911 496 'throws an error when passing no arguments to grep';
a7821be5 497
498 throws_ok {
499 $obj->grep( sub { }, 2 );
500 }
501 qr/Cannot call grep with more than 1 argument/,
e3181911 502 'throws an error when passing two arguments to grep';
a7821be5 503
504 throws_ok { $obj->grep( {} ) }
e3181911 505 qr/The argument passed to grep must be a code reference/,
506 'throws an error when passing a non coderef to grep';
a7821be5 507
88e88a7b 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
a7821be5 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/,
e3181911 525 'throws an error when passing one argument passed to grep_curried';
a7821be5 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/,
e3181911 537 'throws an error when passing no arguments to first';
a7821be5 538
539 throws_ok {
540 $obj->first( sub { }, 2 );
541 }
542 qr/Cannot call first with more than 1 argument/,
e3181911 543 'throws an error when passing two arguments to first';
a7821be5 544
545 throws_ok { $obj->first( {} ) }
e3181911 546 qr/The argument passed to first must be a code reference/,
547 'throws an error when passing a non coderef to first';
a7821be5 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/,
e3181911 559 'throws an error when passing one argument passed to first_curried';
a7821be5 560
561 $obj->_values( [ 1 .. 4 ] );
562
563 is(
564 $obj->join('-'), '1-2-3-4',
565 'join returns expected result'
566 );
567
5394a1c7 568 is(
569 $obj->join(q{}), '1234',
570 'join returns expected result when joining with empty string'
571 );
572
a7821be5 573 throws_ok { $obj->join }
574 qr/Cannot call join without at least 1 argument/,
e3181911 575 'throws an error when passing no arguments to join';
a7821be5 576
577 throws_ok { $obj->join( '-', 2 ) }
578 qr/Cannot call join with more than 1 argument/,
e3181911 579 'throws an error when passing two arguments to join';
a7821be5 580
581 throws_ok { $obj->join( {} ) }
e3181911 582 qr/The argument passed to join must be a string/,
583 'throws an error when passing a non string to join';
a7821be5 584
585 is_deeply(
586 [ sort $obj->shuffle ],
587 [ 1 .. 4 ],
588 'shuffle returns all values (cannot check for a random order)'
589 );
590
591 throws_ok { $obj->shuffle(2) }
592 qr/Cannot call shuffle with any arguments/,
e3181911 593 'throws an error when passing an argument passed to shuffle';
a7821be5 594
595 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
596
597 is_deeply(
598 [ $obj->uniq ],
599 [ 1 .. 4, 5, 7 ],
600 'uniq returns expected values (in original order)'
601 );
602
603 throws_ok { $obj->uniq(2) }
604 qr/Cannot call uniq with any arguments/,
e3181911 605 'throws an error when passing an argument passed to uniq';
a7821be5 606
607 $obj->_values( [ 1 .. 5 ] );
608
609 is(
610 $obj->reduce( sub { $_[0] * $_[1] } ),
611 120,
612 'reduce returns expected value'
613 );
614
615 throws_ok { $obj->reduce }
616 qr/Cannot call reduce without at least 1 argument/,
e3181911 617 'throws an error when passing no arguments to reduce';
a7821be5 618
619 throws_ok {
620 $obj->reduce( sub { }, 2 );
621 }
622 qr/Cannot call reduce with more than 1 argument/,
e3181911 623 'throws an error when passing two arguments to reduce';
a7821be5 624
625 throws_ok { $obj->reduce( {} ) }
e3181911 626 qr/The argument passed to reduce must be a code reference/,
627 'throws an error when passing a non coderef to reduce';
a7821be5 628
629 is(
630 $obj->reduce_curried,
631 120,
632 'reduce_curried returns expected value'
633 );
634
635 throws_ok {
636 $obj->reduce_curried( sub { } );
637 }
638 qr/Cannot call reduce with more than 1 argument/,
e3181911 639 'throws an error when passing one argument passed to reduce_curried';
a7821be5 640
641 $obj->_values( [ 1 .. 6 ] );
642
643 my $it = $obj->natatime(2);
644 my @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 returns expected iterator'
653 );
654
655 @nat = ();
656 $obj->natatime( 2, sub { push @nat, [@_] } );
657
658 is_deeply(
659 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
660 \@nat,
661 'natatime with function returns expected value'
662 );
663
664 throws_ok { $obj->natatime( {} ) }
e3181911 665 qr/The n value passed to natatime must be an integer/,
666 'throws an error when passing a non integer to natatime';
a7821be5 667
668 throws_ok { $obj->natatime( 2, {} ) }
e3181911 669 qr/The second argument passed to natatime must be a code reference/,
670 'throws an error when passing a non code ref to natatime';
a7821be5 671
04bcce6a 672 $it = $obj->natatime_curried();
673 @nat = ();
a098a4a3 674 while ( my @v = $it->() ) {
675 push @nat, \@v;
676 }
677
678 is_deeply(
679 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
680 \@nat,
681 'natatime_curried returns expected iterator'
682 );
683
684 @nat = ();
685 $obj->natatime_curried( sub { push @nat, [@_] } );
686
687 is_deeply(
688 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
689 \@nat,
690 'natatime_curried with function returns expected value'
691 );
692
693 throws_ok { $obj->natatime_curried( {} ) }
e3181911 694 qr/The second argument passed to natatime must be a code reference/,
695 'throws an error when passing a non code ref to natatime_curried';
a098a4a3 696
a7821be5 697 if ( $class->meta->get_attribute('_values')->is_lazy ) {
698 my $obj = $class->new;
699
700 is( $obj->count, 2, 'count is 2 (lazy init)' );
701
702 $obj->_clear_values;
703
a098a4a3 704 is_deeply(
705 [ $obj->elements ], [ 42, 84 ],
706 'elements contains default with lazy init'
707 );
a7821be5 708
709 $obj->_clear_values;
710
711 $obj->push(2);
712
713 is_deeply(
714 $obj->_values, [ 42, 84, 2 ],
715 'push works with lazy init'
716 );
717
718 $obj->_clear_values;
719
720 $obj->unshift( 3, 4 );
721
722 is_deeply(
723 $obj->_values, [ 3, 4, 42, 84 ],
724 'unshift works with lazy init'
725 );
726 }
727 }
728 $class;
862b6081 729}
a28e50e4 730
731done_testing;