1use Test::More;
2use strict; use warnings FATAL => 'all';
3
4use List::Objects::WithUtils 'array';
5
6is_deeply
7  [ array->flatten ],
8  [ ],
9  'empty array flatten with no args ok';
10
11is_deeply
12  [ array->flatten(1) ],
13  [ ],
14  'empty array flatten-to-depth ok';
15
16my $arr = array( 1, 2, [ 3, 4, [ 5, 6 ], 7 ] );
17is_deeply
18  [ $arr->flatten ],
19  [ $arr->all ],
20  'flatten with no args same as all() ok';
21
22is_deeply
23  [ $arr->flatten(0) ],
24  [ $arr->all ],
25  'flatten to depth 0 same as all() ok';
26
27is_deeply
28  [ $arr->flatten(-1) ],
29  [ $arr->all ],
30  'flatten to negative depth same as all() ok';
31
32is_deeply
33  [ $arr->flatten(1) ],
34  [ 1, 2, 3, 4, [ 5, 6 ], 7 ],
35  'flatten to depth 1 ok';
36
37is_deeply
38  [ $arr->flatten(2) ],
39  [ 1, 2, 3, 4, 5, 6, 7 ],
40  'flatten to depth 2 ok';
41
42$arr = array(
43  1, 2,
44  [ 3, 4, [ 5, 6 ] ],
45  [ 7, 8, [ 9, 10 ] ],
46);
47
48is_deeply
49  [ $arr->flatten(1) ],
50  [ 1, 2, 3, 4, [ 5, 6 ], 7, 8, [ 9, 10 ] ],
51  'flatten complex array ok';
52
53{ package My::ArrayType;
54  use strict; use warnings FATAL => 'all';
55  sub new { bless [1], shift }
56}
57
58my $foo = My::ArrayType->new;
59$arr = array(
60  array(1, 2, 3),
61  $foo,
62  [ 4, 5, 6 ],
63);
64
65is_deeply
66  [ $arr->flatten(1) ],
67  [ 1, 2, 3, $foo, 4, 5, 6 ],
68  'flatten skipped ARRAY-type obj ok';
69
70done_testing;
71