use_ok stinks; just load the library
[gitmo/Moose-Autobox.git] / t / 008_flatten.t
CommitLineData
30715849 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e477b088 6use Test::More tests => 9;
30715849 7use Moose::Autobox;
8
9my $array = [ qw(1 2 3 4 ) ];
10is_deeply(
11 [ $array->flatten ],
12 [ 1, 2, 3, 4 ],
13 "flattening an array returns a list",
14);
15
16my $hash = { a => 1, b => 2 };
17is_deeply(
18 [ sort $hash->flatten ],
19 [ qw(1 2 a b) ],
20 "flattening a hash returns a list",
21);
e29de5ed 22
23my $scalar = 1;
24is_deeply(
25 [ $scalar->flatten ],
26 [ 1 ],
27 "flattening a scalar returns the scalar",
28);
29
30my $scalar_ref = \$scalar;
31is_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
38is_deeply(
39 [ 1 .. 9 ]->flatten_deep,
40 [ 1 .. 9 ],
41 "default flatten_deep on shallow array returns correct array"
42);
43
44is_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
50is_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
56is_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
62is_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);