Commit | Line | Data |
30715849 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e477b088 |
6 | use Test::More tests => 9; |
30715849 |
7 | use Moose::Autobox; |
8 | |
9 | my $array = [ qw(1 2 3 4 ) ]; |
10 | is_deeply( |
11 | [ $array->flatten ], |
12 | [ 1, 2, 3, 4 ], |
13 | "flattening an array returns a list", |
14 | ); |
15 | |
16 | my $hash = { a => 1, b => 2 }; |
17 | is_deeply( |
18 | [ sort $hash->flatten ], |
19 | [ qw(1 2 a b) ], |
20 | "flattening a hash returns a list", |
21 | ); |
e29de5ed |
22 | |
23 | my $scalar = 1; |
24 | is_deeply( |
25 | [ $scalar->flatten ], |
26 | [ 1 ], |
27 | "flattening a scalar returns the scalar", |
28 | ); |
29 | |
30 | my $scalar_ref = \$scalar; |
31 | is_deeply( |
32 | [ $scalar_ref->flatten ], |
33 | [ \$scalar ], |
34 | "flattening a reference to a scalar returns the same scalar reference", |
35 | ); |
e477b088 |
36 | |
37 | # flatten_deep on array |
38 | is_deeply( |
39 | [ 1 .. 9 ]->flatten_deep, |
40 | [ 1 .. 9 ], |
41 | "default flatten_deep on shallow array returns correct array" |
42 | ); |
43 | |
44 | is_deeply( |
45 | [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep, |
46 | [ 1 .. 9 ], |
47 | "default flatten_deep on array with depth completely flattens array" |
48 | ); |
49 | |
50 | is_deeply( |
51 | [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(undef), |
52 | [ 1 .. 9 ], |
53 | "flatten_deep with an undef argument on array with depth completely flattens array" |
54 | ); |
55 | |
56 | is_deeply( |
57 | [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(0), |
58 | [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ], |
59 | "flatten_deep with depth 0 specified on array returns array unchanged" |
60 | ); |
61 | |
62 | is_deeply( |
63 | [ [ 1 .. 3 ], [[ 4 .. 6 ]], [[[ 7 .. 9 ]]] ]->flatten_deep(2), |
64 | [ 1 .. 6, [ 7 .. 9 ] ], |
65 | "flatten_deep with depth specified returns correct array" |
66 | ); |