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