File: flatten.t

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: perl: 1,957; makefile: 17; sh: 1
file content (70 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use Test::More;
use strict; use warnings FATAL => 'all';

use List::Objects::WithUtils 'array';

is_deeply
  [ array->flatten ],
  [ ],
  'empty array flatten with no args ok';

is_deeply
  [ array->flatten(1) ],
  [ ],
  'empty array flatten-to-depth ok';

my $arr = array( 1, 2, [ 3, 4, [ 5, 6 ], 7 ] );
is_deeply
  [ $arr->flatten ],
  [ $arr->all ],
  'flatten with no args same as all() ok';

is_deeply
  [ $arr->flatten(0) ],
  [ $arr->all ],
  'flatten to depth 0 same as all() ok';

is_deeply
  [ $arr->flatten(-1) ],
  [ $arr->all ],
  'flatten to negative depth same as all() ok';

is_deeply
  [ $arr->flatten(1) ],
  [ 1, 2, 3, 4, [ 5, 6 ], 7 ],
  'flatten to depth 1 ok';

is_deeply
  [ $arr->flatten(2) ],
  [ 1, 2, 3, 4, 5, 6, 7 ],
  'flatten to depth 2 ok';

$arr = array(
  1, 2,
  [ 3, 4, [ 5, 6 ] ],
  [ 7, 8, [ 9, 10 ] ],
);

is_deeply
  [ $arr->flatten(1) ],
  [ 1, 2, 3, 4, [ 5, 6 ], 7, 8, [ 9, 10 ] ],
  'flatten complex array ok';

{ package My::ArrayType;
  use strict; use warnings FATAL => 'all';
  sub new { bless [1], shift }
}

my $foo = My::ArrayType->new;
$arr = array(
  array(1, 2, 3),
  $foo,
  [ 4, 5, 6 ],
);

is_deeply
  [ $arr->flatten(1) ],
  [ 1, 2, 3, $foo, 4, 5, 6 ],
  'flatten skipped ARRAY-type obj ok';

done_testing;