added grep_in_place attribute trait
[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 ();
bbeb5798 9use Moose::Util::TypeConstraints;
8b9641b8 10use NoInlineAttribute;
a28e50e4 11use Test::More;
b10dde3a 12use Test::Fatal;
37ecda0d 13use Test::Moose;
d50fc84a 14
e3c07b19 15{
a7821be5 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',
fe65e628 53 grep_in_place => 'grep_in_place',
a7821be5 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
b10dde3a 141 like( exception { $obj->count(22) }, qr/Cannot call count with any arguments/, 'throws an error when passing an argument passed to count' );
e3c07b19 142
b10dde3a 143 is( exception { $obj->push( 1, 2, 3 ) }, undef, 'pushed three new values and lived' );
e3c07b19 144
b10dde3a 145 is( exception { $obj->push() }, undef, 'call to push without arguments lives' );
e3c07b19 146
b10dde3a 147 is( exception {
7f5ec80d 148 is( $obj->unshift( 101, 22 ), 8,
149 'unshift returns size of the new array' );
b10dde3a 150 }, undef, 'unshifted two values and lived' );
e3c07b19 151
e32b7489 152 is_deeply(
153 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
154 'unshift changed the value of the array in the object'
155 );
156
b10dde3a 157 is( exception { $obj->unshift() }, undef, 'call to unshift without arguments lives' );
e3c07b19 158
a7821be5 159 is( $obj->pop, 3, 'pop returns the last value in the array' );
910684ee 160
a7821be5 161 is_deeply(
162 $obj->_values, [ 101, 22, 10, 12, 42, 1, 2 ],
163 'pop changed the value of the array in the object'
164 );
910684ee 165
b10dde3a 166 like( exception { $obj->pop(42) }, qr/Cannot call pop with any arguments/, 'call to pop with arguments dies' );
910684ee 167
a7821be5 168 is( $obj->shift, 101, 'shift returns the first value' );
910684ee 169
b10dde3a 170 like( exception { $obj->shift(42) }, qr/Cannot call shift with any arguments/, 'call to shift with arguments dies' );
e3c07b19 171
a7821be5 172 is_deeply(
173 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
174 'shift changed the value of the array in the object'
175 );
e3c07b19 176
a7821be5 177 is_deeply(
178 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
179 'call to elements returns values as a list'
180 );
e3c07b19 181
b10dde3a 182 like( exception { $obj->elements(22) }, qr/Cannot call elements with any arguments/, 'throws an error when passing an argument passed to elements' );
e3c07b19 183
a7821be5 184 $obj->_values( [ 1, 2, 3 ] );
e3c07b19 185
a7821be5 186 is( $obj->get(0), 1, 'get values at index 0' );
187 is( $obj->get(1), 2, 'get values at index 1' );
188 is( $obj->get(2), 3, 'get values at index 2' );
189 is( $obj->get_curried, 2, 'get_curried returns value at index 1' );
e3c07b19 190
b10dde3a 191 like( exception { $obj->get() }, qr/Cannot call get without at least 1 argument/, 'throws an error when get is called without any arguments' );
e3c07b19 192
b10dde3a 193 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' );
e3c07b19 194
b10dde3a 195 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' );
e3c07b19 196
b10dde3a 197 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' );
e3c07b19 198
b10dde3a 199 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' );
e3c07b19 200
b10dde3a 201 is( exception {
7f5ec80d 202 is( $obj->set( 1, 100 ), 100, 'set returns new value' );
b10dde3a 203 }, undef, 'set value at index 1 lives' );
e3c07b19 204
a7821be5 205 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
e3c07b19 206
7f5ec80d 207
b10dde3a 208 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' );
59de9de4 209
b10dde3a 210 is( exception { $obj->set_curried_1(99) }, undef, 'set_curried_1 lives' );
59de9de4 211
a7821be5 212 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
59de9de4 213
b10dde3a 214 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' );
59de9de4 215
b10dde3a 216 is( exception { $obj->set_curried_2 }, undef, 'set_curried_2 lives' );
59de9de4 217
a7821be5 218 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
59de9de4 219
b10dde3a 220 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' );
59de9de4 221
a7821be5 222 is(
223 $obj->accessor(1), 98,
224 'accessor with one argument returns value at index 1'
225 );
59de9de4 226
b10dde3a 227 is( exception {
7f5ec80d 228 is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' );
b10dde3a 229 }, undef, 'accessor as writer lives' );
59de9de4 230
8fc30049 231 like(
232 exception {
233 $obj->accessor;
234 },
235 qr/Cannot call accessor without at least 1 argument/,
236 'throws an error when accessor is called without arguments'
237 );
238
a7821be5 239 is(
240 $obj->get(1), 97,
241 'accessor set value at index 1'
242 );
862b6081 243
b10dde3a 244 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' );
59de9de4 245
a7821be5 246 is(
247 $obj->accessor_curried_1, 97,
248 'accessor_curried_1 returns expected value when called with no arguments'
249 );
59de9de4 250
b10dde3a 251 is( exception { $obj->accessor_curried_1(95) }, undef, 'accessor_curried_1 as writer lives' );
862b6081 252
a7821be5 253 is(
254 $obj->get(1), 95,
255 'accessor_curried_1 set value at index 1'
256 );
862b6081 257
b10dde3a 258 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' );
862b6081 259
b10dde3a 260 is( exception { $obj->accessor_curried_2 }, undef, 'accessor_curried_2 as writer lives' );
862b6081 261
a7821be5 262 is(
263 $obj->get(1), 90,
264 'accessor_curried_2 set value at index 1'
265 );
862b6081 266
b10dde3a 267 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' );
862b6081 268
b10dde3a 269 is( exception { $obj->clear }, undef, 'clear lives' );
862b6081 270
a7821be5 271 ok( $obj->is_empty, 'values is empty after call to clear' );
862b6081 272
805e018d 273 is( exception {
274 is( $obj->shift, undef,
275 'shift returns undef on an empty array' );
276 }, undef, 'shifted from an empty array and lived' );
277
a7821be5 278 $obj->set( 0 => 42 );
862b6081 279
b10dde3a 280 like( exception { $obj->clear(50) }, qr/Cannot call clear with any arguments/, 'throws an error when clear is called with an argument' );
862b6081 281
a7821be5 282 ok(
283 !$obj->is_empty,
284 'values is not empty after failed call to clear'
285 );
59de9de4 286
b10dde3a 287 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' );
8b9641b8 288
a7821be5 289 $obj->clear;
7f5ec80d 290 is(
291 $obj->push( 1, 5, 10, 42 ), 4,
292 'pushed 4 elements, got number of elements in the array back'
293 );
59de9de4 294
b10dde3a 295 is( exception {
7f5ec80d 296 is( $obj->delete(2), 10, 'delete returns deleted value' );
b10dde3a 297 }, undef, 'delete lives' );
59de9de4 298
a7821be5 299 is_deeply(
300 $obj->_values, [ 1, 5, 42 ],
301 'delete removed the specified element'
302 );
e3c07b19 303
b10dde3a 304 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' );
59de9de4 305
b10dde3a 306 is( exception { $obj->delete_curried }, undef, 'delete_curried lives' );
910684ee 307
a7821be5 308 is_deeply(
309 $obj->_values, [ 1, 42 ],
310 'delete removed the specified element'
311 );
910684ee 312
b10dde3a 313 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' );
e3c07b19 314
b10dde3a 315 is( exception { $obj->insert( 1, 21 ) }, undef, 'insert lives' );
910684ee 316
a7821be5 317 is_deeply(
318 $obj->_values, [ 1, 21, 42 ],
319 'insert added the specified element'
320 );
910684ee 321
b10dde3a 322 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' );
59de9de4 323
b10dde3a 324 is( exception {
7f5ec80d 325 is_deeply(
326 [ $obj->splice( 1, 0, 2, 3 ) ],
327 [],
328 'return value of splice is empty list when not removing elements'
329 );
b10dde3a 330 }, undef, 'splice lives' );
910684ee 331
a7821be5 332 is_deeply(
333 $obj->_values, [ 1, 2, 3, 21, 42 ],
334 'splice added the specified elements'
335 );
910684ee 336
b10dde3a 337 is( exception {
7f5ec80d 338 is_deeply(
339 [ $obj->splice( 1, 2, 99 ) ],
340 [ 2, 3 ],
341 'splice returns list of removed values'
342 );
b10dde3a 343 }, undef, 'splice lives' );
e3c07b19 344
a7821be5 345 is_deeply(
7f5ec80d 346 $obj->_values, [ 1, 99, 21, 42 ],
a7821be5 347 'splice added the specified elements'
348 );
e3c07b19 349
b10dde3a 350 like( exception { $obj->splice() }, qr/Cannot call splice without at least 1 argument/, 'throws an error when splice is called with no arguments' );
910684ee 351
b10dde3a 352 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' );
e3181911 353
b10dde3a 354 is( exception { $obj->splice_curried_1( 2, 101 ) }, undef, 'splice_curried_1 lives' );
910684ee 355
a7821be5 356 is_deeply(
7f5ec80d 357 $obj->_values, [ 1, 101, 42 ],
a7821be5 358 'splice added the specified elements'
359 );
59de9de4 360
b10dde3a 361 is( exception { $obj->splice_curried_2(102) }, undef, 'splice_curried_2 lives' );
862b6081 362
a7821be5 363 is_deeply(
7f5ec80d 364 $obj->_values, [ 1, 102 ],
a7821be5 365 'splice added the specified elements'
366 );
862b6081 367
b10dde3a 368 is( exception { $obj->splice_curried_all }, undef, 'splice_curried_all lives' );
862b6081 369
a7821be5 370 is_deeply(
371 $obj->_values, [ 1, 3, 4, 5 ],
372 'splice added the specified elements'
373 );
59de9de4 374
7f5ec80d 375 is_deeply(
376 scalar $obj->splice( 1, 2 ),
377 4,
378 'splice in scalar context returns last element removed'
379 );
380
381 is_deeply(
382 scalar $obj->splice( 1, 0, 42 ),
383 undef,
384 'splice in scalar context returns undef when no elements are removed'
385 );
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
b10dde3a 399 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' );
910684ee 400
b10dde3a 401 like( exception {
a7821be5 402 $obj->sort( sub { }, 27 );
b10dde3a 403 }, qr/Cannot call sort with more than 1 argument/, 'throws an error when passing two arguments to sort' );
910684ee 404
a7821be5 405 $obj->_values( [ 3, 9, 5, 22, 11 ] );
862b6081 406
a7821be5 407 $obj->sort_in_place;
d50fc84a 408
a7821be5 409 is_deeply(
410 $obj->_values, [ 11, 22, 3, 5, 9 ],
411 'sort_in_place sorts values'
412 );
862b6081 413
a7821be5 414 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
862b6081 415
a7821be5 416 is_deeply(
417 $obj->_values, [ 3, 5, 9, 11, 22 ],
418 'sort_in_place with function sorts values'
419 );
420
b10dde3a 421 like( exception {
e3181911 422 $obj->sort_in_place( 27 );
b10dde3a 423 }, 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' );
e3181911 424
b10dde3a 425 like( exception {
a7821be5 426 $obj->sort_in_place( sub { }, 27 );
b10dde3a 427 }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing two arguments to sort_in_place' );
a7821be5 428
429 $obj->_values( [ 3, 9, 5, 22, 11 ] );
430
431 $obj->sort_in_place_curried;
432
433 is_deeply(
434 $obj->_values, [ 22, 11, 9, 5, 3 ],
435 'sort_in_place_curried sorts values'
436 );
437
b10dde3a 438 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' );
a7821be5 439
440 $obj->_values( [ 1 .. 5 ] );
441
442 is_deeply(
443 [ $obj->map( sub { $_ + 1 } ) ],
444 [ 2 .. 6 ],
445 'map returns the expected values'
446 );
447
b10dde3a 448 like( exception { $obj->map }, qr/Cannot call map without at least 1 argument/, 'throws an error when passing no arguments to map' );
a7821be5 449
b10dde3a 450 like( exception {
a7821be5 451 $obj->map( sub { }, 2 );
b10dde3a 452 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing two arguments to map' );
a7821be5 453
b10dde3a 454 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' );
a7821be5 455
456 $obj->_values( [ 1 .. 5 ] );
457
458 is_deeply(
459 [ $obj->map_curried ],
460 [ 2 .. 6 ],
461 'map_curried returns the expected values'
462 );
463
b10dde3a 464 like( exception {
a7821be5 465 $obj->map_curried( sub { } );
b10dde3a 466 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing one argument passed to map_curried' );
a7821be5 467
468 $obj->_values( [ 2 .. 9 ] );
469
470 is_deeply(
471 [ $obj->grep( sub { $_ < 5 } ) ],
472 [ 2 .. 4 ],
473 'grep returns the expected values'
474 );
475
b10dde3a 476 like( exception { $obj->grep }, qr/Cannot call grep without at least 1 argument/, 'throws an error when passing no arguments to grep' );
a7821be5 477
b10dde3a 478 like( exception {
a7821be5 479 $obj->grep( sub { }, 2 );
b10dde3a 480 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing two arguments to grep' );
a7821be5 481
b10dde3a 482 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' );
a7821be5 483
88e88a7b 484 my $overloader = Overloader->new( sub { $_ < 5 } );
485 is_deeply(
486 [ $obj->grep($overloader) ],
487 [ 2 .. 4 ],
488 'grep works with obj that overload code dereferencing'
489 );
490
a7821be5 491 is_deeply(
492 [ $obj->grep_curried ],
493 [ 2 .. 4 ],
494 'grep_curried returns the expected values'
495 );
496
b10dde3a 497 like( exception {
a7821be5 498 $obj->grep_curried( sub { } );
b10dde3a 499 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing one argument passed to grep_curried' );
a7821be5 500
fe65e628 501 $obj->_values( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
502
503 $obj->grep_in_place(sub { $_ > 4 } );
504
505 is_deeply (
506 $obj->_values,
507 [5, 6, 7, 8],
508 'grep_in_place alters attribute vars'
509 );
510
a7821be5 511 $obj->_values( [ 2, 4, 22, 99, 101, 6 ] );
512
513 is(
514 $obj->first( sub { $_ % 2 } ),
515 99,
516 'first returns expected value'
517 );
518
b10dde3a 519 like( exception { $obj->first }, qr/Cannot call first without at least 1 argument/, 'throws an error when passing no arguments to first' );
a7821be5 520
b10dde3a 521 like( exception {
a7821be5 522 $obj->first( sub { }, 2 );
b10dde3a 523 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing two arguments to first' );
a7821be5 524
b10dde3a 525 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' );
a7821be5 526
527 is(
528 $obj->first_curried,
529 99,
530 'first_curried returns expected value'
531 );
532
b10dde3a 533 like( exception {
a7821be5 534 $obj->first_curried( sub { } );
b10dde3a 535 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
a7821be5 536
537 $obj->_values( [ 1 .. 4 ] );
538
539 is(
540 $obj->join('-'), '1-2-3-4',
541 'join returns expected result'
542 );
543
5394a1c7 544 is(
545 $obj->join(q{}), '1234',
546 'join returns expected result when joining with empty string'
547 );
548
b10dde3a 549 like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
a7821be5 550
b10dde3a 551 like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
a7821be5 552
b10dde3a 553 like( exception { $obj->join( {} ) }, qr/The argument passed to join must be a string/, 'throws an error when passing a non string to join' );
a7821be5 554
555 is_deeply(
556 [ sort $obj->shuffle ],
557 [ 1 .. 4 ],
558 'shuffle returns all values (cannot check for a random order)'
559 );
560
b10dde3a 561 like( exception { $obj->shuffle(2) }, qr/Cannot call shuffle with any arguments/, 'throws an error when passing an argument passed to shuffle' );
a7821be5 562
563 $obj->_values( [ 1 .. 4, 2, 5, 3, 7, 3, 3, 1 ] );
564
565 is_deeply(
566 [ $obj->uniq ],
567 [ 1 .. 4, 5, 7 ],
568 'uniq returns expected values (in original order)'
569 );
570
b10dde3a 571 like( exception { $obj->uniq(2) }, qr/Cannot call uniq with any arguments/, 'throws an error when passing an argument passed to uniq' );
a7821be5 572
573 $obj->_values( [ 1 .. 5 ] );
574
575 is(
576 $obj->reduce( sub { $_[0] * $_[1] } ),
577 120,
578 'reduce returns expected value'
579 );
580
b10dde3a 581 like( exception { $obj->reduce }, qr/Cannot call reduce without at least 1 argument/, 'throws an error when passing no arguments to reduce' );
a7821be5 582
b10dde3a 583 like( exception {
a7821be5 584 $obj->reduce( sub { }, 2 );
b10dde3a 585 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing two arguments to reduce' );
a7821be5 586
b10dde3a 587 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' );
a7821be5 588
589 is(
590 $obj->reduce_curried,
591 120,
592 'reduce_curried returns expected value'
593 );
594
b10dde3a 595 like( exception {
a7821be5 596 $obj->reduce_curried( sub { } );
b10dde3a 597 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing one argument passed to reduce_curried' );
a7821be5 598
599 $obj->_values( [ 1 .. 6 ] );
600
601 my $it = $obj->natatime(2);
602 my @nat;
603 while ( my @v = $it->() ) {
604 push @nat, \@v;
605 }
606
607 is_deeply(
608 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
609 \@nat,
610 'natatime returns expected iterator'
611 );
612
613 @nat = ();
614 $obj->natatime( 2, sub { push @nat, [@_] } );
615
616 is_deeply(
617 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
618 \@nat,
619 'natatime with function returns expected value'
620 );
621
b10dde3a 622 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' );
a7821be5 623
b10dde3a 624 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' );
a7821be5 625
04bcce6a 626 $it = $obj->natatime_curried();
627 @nat = ();
a098a4a3 628 while ( my @v = $it->() ) {
629 push @nat, \@v;
630 }
631
632 is_deeply(
633 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
634 \@nat,
635 'natatime_curried returns expected iterator'
636 );
637
638 @nat = ();
639 $obj->natatime_curried( sub { push @nat, [@_] } );
640
641 is_deeply(
642 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
643 \@nat,
644 'natatime_curried with function returns expected value'
645 );
646
b10dde3a 647 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' );
a098a4a3 648
a7821be5 649 if ( $class->meta->get_attribute('_values')->is_lazy ) {
650 my $obj = $class->new;
651
652 is( $obj->count, 2, 'count is 2 (lazy init)' );
653
654 $obj->_clear_values;
655
a098a4a3 656 is_deeply(
657 [ $obj->elements ], [ 42, 84 ],
658 'elements contains default with lazy init'
659 );
a7821be5 660
661 $obj->_clear_values;
662
663 $obj->push(2);
664
665 is_deeply(
666 $obj->_values, [ 42, 84, 2 ],
667 'push works with lazy init'
668 );
669
670 $obj->_clear_values;
671
672 $obj->unshift( 3, 4 );
673
674 is_deeply(
675 $obj->_values, [ 3, 4, 42, 84 ],
676 'unshift works with lazy init'
677 );
678 }
679 }
680 $class;
862b6081 681}
a28e50e4 682
683done_testing;