Add test - custom error class is causing a warning from make_immutable
[gitmo/Moose.git] / t / native_traits / 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',
53 grep_curried => [ grep => ( sub { $_ < 5 } ) ],
54 first => 'first',
55 first_curried => [ first => ( sub { $_ % 2 } ) ],
175f9180 56 first_index => 'first_index',
57 first_index_curried => [ first_index => ( sub { $_ % 2 } ) ],
a7821be5 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 ],
e3c07b19 66 );
a7821be5 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
8b9641b8 78 my @traits = 'Array';
79 push @traits, 'NoInlineAttribute'
80 if delete $attr{no_inline};
81
a7821be5 82 $class->add_attribute(
83 _values => (
8b9641b8 84 traits => \@traits,
a7821be5 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 }
e3c07b19 96}
97
862b6081 98{
88e88a7b 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{
a7821be5 111 run_tests(build_class);
112 run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
cf0da4e2 113 run_tests( build_class( trigger => sub { } ) );
8b9641b8 114 run_tests( build_class( no_inline => 1 ) );
bbeb5798 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 ) );
a7821be5 124}
e3c07b19 125
a7821be5 126sub run_tests {
127 my ( $class, $handles ) = @_;
e3c07b19 128
a7821be5 129 can_ok( $class, $_ ) for sort keys %{$handles};
e3c07b19 130
a7821be5 131 with_immutable {
132 my $obj = $class->new( _values => [ 10, 12, 42 ] );
e3c07b19 133
a7821be5 134 is_deeply(
135 $obj->_values, [ 10, 12, 42 ],
136 'values can be set in constructor'
137 );
e3c07b19 138
a7821be5 139 ok( !$obj->is_empty, 'values is not empty' );
140 is( $obj->count, 3, 'count returns 3' );
e3c07b19 141
b10dde3a 142 like( exception { $obj->count(22) }, qr/Cannot call count with any arguments/, 'throws an error when passing an argument passed to count' );
e3c07b19 143
b10dde3a 144 is( exception { $obj->push( 1, 2, 3 ) }, undef, 'pushed three new values and lived' );
e3c07b19 145
b10dde3a 146 is( exception { $obj->push() }, undef, 'call to push without arguments lives' );
e3c07b19 147
b10dde3a 148 is( exception {
7f5ec80d 149 is( $obj->unshift( 101, 22 ), 8,
150 'unshift returns size of the new array' );
b10dde3a 151 }, undef, '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
b10dde3a 158 is( exception { $obj->unshift() }, undef, 'call to unshift without arguments lives' );
e3c07b19 159
a7821be5 160 is( $obj->pop, 3, 'pop returns the last value in the array' );
910684ee 161
a7821be5 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 );
910684ee 166
b10dde3a 167 like( exception { $obj->pop(42) }, qr/Cannot call pop with any arguments/, 'call to pop with arguments dies' );
910684ee 168
a7821be5 169 is( $obj->shift, 101, 'shift returns the first value' );
910684ee 170
b10dde3a 171 like( exception { $obj->shift(42) }, qr/Cannot call shift with any arguments/, 'call to shift with arguments dies' );
e3c07b19 172
a7821be5 173 is_deeply(
174 $obj->_values, [ 22, 10, 12, 42, 1, 2 ],
175 'shift changed the value of the array in the object'
176 );
e3c07b19 177
a7821be5 178 is_deeply(
179 [ $obj->elements ], [ 22, 10, 12, 42, 1, 2 ],
180 'call to elements returns values as a list'
181 );
e3c07b19 182
b10dde3a 183 like( exception { $obj->elements(22) }, qr/Cannot call elements with any arguments/, 'throws an error when passing an argument passed to elements' );
e3c07b19 184
a7821be5 185 $obj->_values( [ 1, 2, 3 ] );
e3c07b19 186
a7821be5 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' );
e3c07b19 191
b10dde3a 192 like( exception { $obj->get() }, qr/Cannot call get without at least 1 argument/, 'throws an error when get is called without any arguments' );
e3c07b19 193
b10dde3a 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' );
e3c07b19 195
b10dde3a 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' );
e3c07b19 197
b10dde3a 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' );
e3c07b19 199
b10dde3a 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' );
e3c07b19 201
b10dde3a 202 is( exception {
7f5ec80d 203 is( $obj->set( 1, 100 ), 100, 'set returns new value' );
b10dde3a 204 }, undef, 'set value at index 1 lives' );
e3c07b19 205
a7821be5 206 is( $obj->get(1), 100, 'get value at index 1 returns new value' );
e3c07b19 207
7f5ec80d 208
b10dde3a 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' );
59de9de4 210
b10dde3a 211 is( exception { $obj->set_curried_1(99) }, undef, 'set_curried_1 lives' );
59de9de4 212
a7821be5 213 is( $obj->get(1), 99, 'get value at index 1 returns new value' );
59de9de4 214
b10dde3a 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' );
59de9de4 216
b10dde3a 217 is( exception { $obj->set_curried_2 }, undef, 'set_curried_2 lives' );
59de9de4 218
a7821be5 219 is( $obj->get(1), 98, 'get value at index 1 returns new value' );
59de9de4 220
b10dde3a 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' );
59de9de4 222
a7821be5 223 is(
224 $obj->accessor(1), 98,
225 'accessor with one argument returns value at index 1'
226 );
59de9de4 227
b10dde3a 228 is( exception {
7f5ec80d 229 is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' );
b10dde3a 230 }, undef, 'accessor as writer lives' );
59de9de4 231
8fc30049 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
a7821be5 240 is(
241 $obj->get(1), 97,
242 'accessor set value at index 1'
243 );
862b6081 244
b10dde3a 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' );
59de9de4 246
a7821be5 247 is(
248 $obj->accessor_curried_1, 97,
249 'accessor_curried_1 returns expected value when called with no arguments'
250 );
59de9de4 251
b10dde3a 252 is( exception { $obj->accessor_curried_1(95) }, undef, 'accessor_curried_1 as writer lives' );
862b6081 253
a7821be5 254 is(
255 $obj->get(1), 95,
256 'accessor_curried_1 set value at index 1'
257 );
862b6081 258
b10dde3a 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' );
862b6081 260
b10dde3a 261 is( exception { $obj->accessor_curried_2 }, undef, 'accessor_curried_2 as writer lives' );
862b6081 262
a7821be5 263 is(
264 $obj->get(1), 90,
265 'accessor_curried_2 set value at index 1'
266 );
862b6081 267
b10dde3a 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' );
862b6081 269
b10dde3a 270 is( exception { $obj->clear }, undef, 'clear lives' );
862b6081 271
a7821be5 272 ok( $obj->is_empty, 'values is empty after call to clear' );
862b6081 273
805e018d 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
a7821be5 279 $obj->set( 0 => 42 );
862b6081 280
b10dde3a 281 like( exception { $obj->clear(50) }, qr/Cannot call clear with any arguments/, 'throws an error when clear is called with an argument' );
862b6081 282
a7821be5 283 ok(
284 !$obj->is_empty,
285 'values is not empty after failed call to clear'
286 );
59de9de4 287
b10dde3a 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' );
8b9641b8 289
a7821be5 290 $obj->clear;
7f5ec80d 291 is(
292 $obj->push( 1, 5, 10, 42 ), 4,
293 'pushed 4 elements, got number of elements in the array back'
294 );
59de9de4 295
b10dde3a 296 is( exception {
7f5ec80d 297 is( $obj->delete(2), 10, 'delete returns deleted value' );
b10dde3a 298 }, undef, 'delete lives' );
59de9de4 299
a7821be5 300 is_deeply(
301 $obj->_values, [ 1, 5, 42 ],
302 'delete removed the specified element'
303 );
e3c07b19 304
b10dde3a 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' );
59de9de4 306
b10dde3a 307 is( exception { $obj->delete_curried }, undef, 'delete_curried lives' );
910684ee 308
a7821be5 309 is_deeply(
310 $obj->_values, [ 1, 42 ],
311 'delete removed the specified element'
312 );
910684ee 313
b10dde3a 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' );
e3c07b19 315
b10dde3a 316 is( exception { $obj->insert( 1, 21 ) }, undef, 'insert lives' );
910684ee 317
a7821be5 318 is_deeply(
319 $obj->_values, [ 1, 21, 42 ],
320 'insert added the specified element'
321 );
910684ee 322
b10dde3a 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' );
59de9de4 324
b10dde3a 325 is( exception {
7f5ec80d 326 is_deeply(
327 [ $obj->splice( 1, 0, 2, 3 ) ],
328 [],
329 'return value of splice is empty list when not removing elements'
330 );
b10dde3a 331 }, undef, 'splice lives' );
910684ee 332
a7821be5 333 is_deeply(
334 $obj->_values, [ 1, 2, 3, 21, 42 ],
335 'splice added the specified elements'
336 );
910684ee 337
b10dde3a 338 is( exception {
7f5ec80d 339 is_deeply(
340 [ $obj->splice( 1, 2, 99 ) ],
341 [ 2, 3 ],
342 'splice returns list of removed values'
343 );
b10dde3a 344 }, undef, 'splice lives' );
e3c07b19 345
a7821be5 346 is_deeply(
7f5ec80d 347 $obj->_values, [ 1, 99, 21, 42 ],
a7821be5 348 'splice added the specified elements'
349 );
e3c07b19 350
b10dde3a 351 like( exception { $obj->splice() }, qr/Cannot call splice without at least 1 argument/, 'throws an error when splice is called with no arguments' );
910684ee 352
b10dde3a 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' );
e3181911 354
b10dde3a 355 is( exception { $obj->splice_curried_1( 2, 101 ) }, undef, 'splice_curried_1 lives' );
910684ee 356
a7821be5 357 is_deeply(
7f5ec80d 358 $obj->_values, [ 1, 101, 42 ],
a7821be5 359 'splice added the specified elements'
360 );
59de9de4 361
b10dde3a 362 is( exception { $obj->splice_curried_2(102) }, undef, 'splice_curried_2 lives' );
862b6081 363
a7821be5 364 is_deeply(
7f5ec80d 365 $obj->_values, [ 1, 102 ],
a7821be5 366 'splice added the specified elements'
367 );
862b6081 368
b10dde3a 369 is( exception { $obj->splice_curried_all }, undef, 'splice_curried_all lives' );
862b6081 370
a7821be5 371 is_deeply(
372 $obj->_values, [ 1, 3, 4, 5 ],
373 'splice added the specified elements'
374 );
59de9de4 375
7f5ec80d 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
a7821be5 388 $obj->_values( [ 3, 9, 5, 22, 11 ] );
862b6081 389
a7821be5 390 is_deeply(
391 [ $obj->sort ], [ 11, 22, 3, 5, 9 ],
392 'sort returns sorted values'
393 );
862b6081 394
a7821be5 395 is_deeply(
396 [ $obj->sort( sub { $_[0] <=> $_[1] } ) ], [ 3, 5, 9, 11, 22 ],
397 'sort returns values sorted by provided function'
398 );
910684ee 399
b10dde3a 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' );
910684ee 401
b10dde3a 402 like( exception {
a7821be5 403 $obj->sort( sub { }, 27 );
b10dde3a 404 }, qr/Cannot call sort with more than 1 argument/, 'throws an error when passing two arguments to sort' );
910684ee 405
a7821be5 406 $obj->_values( [ 3, 9, 5, 22, 11 ] );
862b6081 407
a7821be5 408 $obj->sort_in_place;
d50fc84a 409
a7821be5 410 is_deeply(
411 $obj->_values, [ 11, 22, 3, 5, 9 ],
412 'sort_in_place sorts values'
413 );
862b6081 414
a7821be5 415 $obj->sort_in_place( sub { $_[0] <=> $_[1] } );
862b6081 416
a7821be5 417 is_deeply(
418 $obj->_values, [ 3, 5, 9, 11, 22 ],
419 'sort_in_place with function sorts values'
420 );
421
b10dde3a 422 like( exception {
e3181911 423 $obj->sort_in_place( 27 );
b10dde3a 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' );
e3181911 425
b10dde3a 426 like( exception {
a7821be5 427 $obj->sort_in_place( sub { }, 27 );
b10dde3a 428 }, qr/Cannot call sort_in_place with more than 1 argument/, 'throws an error when passing two arguments to sort_in_place' );
a7821be5 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
b10dde3a 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' );
a7821be5 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
b10dde3a 449 like( exception { $obj->map }, qr/Cannot call map without at least 1 argument/, 'throws an error when passing no arguments to map' );
a7821be5 450
b10dde3a 451 like( exception {
a7821be5 452 $obj->map( sub { }, 2 );
b10dde3a 453 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing two arguments to map' );
a7821be5 454
b10dde3a 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' );
a7821be5 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
b10dde3a 465 like( exception {
a7821be5 466 $obj->map_curried( sub { } );
b10dde3a 467 }, qr/Cannot call map with more than 1 argument/, 'throws an error when passing one argument passed to map_curried' );
a7821be5 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
b10dde3a 477 like( exception { $obj->grep }, qr/Cannot call grep without at least 1 argument/, 'throws an error when passing no arguments to grep' );
a7821be5 478
b10dde3a 479 like( exception {
a7821be5 480 $obj->grep( sub { }, 2 );
b10dde3a 481 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing two arguments to grep' );
a7821be5 482
b10dde3a 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' );
a7821be5 484
88e88a7b 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
a7821be5 492 is_deeply(
493 [ $obj->grep_curried ],
494 [ 2 .. 4 ],
495 'grep_curried returns the expected values'
496 );
497
b10dde3a 498 like( exception {
a7821be5 499 $obj->grep_curried( sub { } );
b10dde3a 500 }, qr/Cannot call grep with more than 1 argument/, 'throws an error when passing one argument passed to grep_curried' );
a7821be5 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
b10dde3a 510 like( exception { $obj->first }, qr/Cannot call first without at least 1 argument/, 'throws an error when passing no arguments to first' );
a7821be5 511
b10dde3a 512 like( exception {
a7821be5 513 $obj->first( sub { }, 2 );
b10dde3a 514 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing two arguments to first' );
a7821be5 515
b10dde3a 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' );
a7821be5 517
518 is(
519 $obj->first_curried,
520 99,
521 'first_curried returns expected value'
522 );
523
b10dde3a 524 like( exception {
a7821be5 525 $obj->first_curried( sub { } );
b10dde3a 526 }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
a7821be5 527
175f9180 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
a7821be5 554 $obj->_values( [ 1 .. 4 ] );
555
556 is(
557 $obj->join('-'), '1-2-3-4',
558 'join returns expected result'
559 );
560
5394a1c7 561 is(
562 $obj->join(q{}), '1234',
563 'join returns expected result when joining with empty string'
564 );
565
b10dde3a 566 like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
a7821be5 567
b10dde3a 568 like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
a7821be5 569
b10dde3a 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' );
a7821be5 571
572 is_deeply(
573 [ sort $obj->shuffle ],
574 [ 1 .. 4 ],
575 'shuffle returns all values (cannot check for a random order)'
576 );
577
b10dde3a 578 like( exception { $obj->shuffle(2) }, qr/Cannot call shuffle with any arguments/, 'throws an error when passing an argument passed to shuffle' );
a7821be5 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
b10dde3a 588 like( exception { $obj->uniq(2) }, qr/Cannot call uniq with any arguments/, 'throws an error when passing an argument passed to uniq' );
a7821be5 589
590 $obj->_values( [ 1 .. 5 ] );
591
592 is(
593 $obj->reduce( sub { $_[0] * $_[1] } ),
594 120,
595 'reduce returns expected value'
596 );
597
b10dde3a 598 like( exception { $obj->reduce }, qr/Cannot call reduce without at least 1 argument/, 'throws an error when passing no arguments to reduce' );
a7821be5 599
b10dde3a 600 like( exception {
a7821be5 601 $obj->reduce( sub { }, 2 );
b10dde3a 602 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing two arguments to reduce' );
a7821be5 603
b10dde3a 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' );
a7821be5 605
606 is(
607 $obj->reduce_curried,
608 120,
609 'reduce_curried returns expected value'
610 );
611
b10dde3a 612 like( exception {
a7821be5 613 $obj->reduce_curried( sub { } );
b10dde3a 614 }, qr/Cannot call reduce with more than 1 argument/, 'throws an error when passing one argument passed to reduce_curried' );
a7821be5 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
b10dde3a 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' );
a7821be5 640
b10dde3a 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' );
a7821be5 642
04bcce6a 643 $it = $obj->natatime_curried();
644 @nat = ();
a098a4a3 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
b10dde3a 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' );
a098a4a3 665
a7821be5 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
a098a4a3 673 is_deeply(
674 [ $obj->elements ], [ 42, 84 ],
675 'elements contains default with lazy init'
676 );
a7821be5 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;
862b6081 698}
a28e50e4 699
229a897a 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
a28e50e4 718done_testing;