File: 008_flatten.t

package info (click to toggle)
libmoose-autobox-perl 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 124
  • sloc: perl: 1,057; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 9;
use Moose::Autobox;

my $array = [ qw(1 2 3 4 ) ];
is_deeply(
  [ $array->flatten ],
  [ 1, 2, 3, 4 ],
  "flattening an array returns a list",
);

my $hash = { a => 1, b => 2 };
is_deeply(
  [ sort $hash->flatten ],
  [ qw(1 2 a b) ],
  "flattening a hash returns a list",
);

my $scalar = 1;
is_deeply(
  [ $scalar->flatten ],
  [ 1 ],
  "flattening a scalar returns the scalar",
);

my $scalar_ref = \$scalar;
is_deeply(
  [ $scalar_ref->flatten ],
  [ \$scalar ],
  "flattening a reference to a scalar returns the same scalar reference",
);

# flatten_deep on array
is_deeply(
  [ 1 .. 9 ]->flatten_deep,
  [ 1 .. 9 ],
 "default flatten_deep on shallow array returns correct array"
);

is_deeply(
  [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep,
  [ 1 .. 9 ],
  "default flatten_deep on array with depth completely flattens array"
);

is_deeply(
  [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(undef),
  [ 1 .. 9 ],
  "flatten_deep with an undef argument on array with depth completely flattens array"
);

is_deeply(
  [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(0),
  [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ],
  "flatten_deep with depth 0 specified on array returns array unchanged"
);

is_deeply(
  [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(2),
  [ 1 .. 6, [ 7 .. 9 ] ],
  "flatten_deep with depth specified returns correct array"
);