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