60b2d3374c73860cc1ac632619f742ca68e70156
[kivitendo-erp.git] / t / request / flatten.t
1 use Test::More;
2 use Test::Deep qw(cmp_deeply);
3 use Data::Dumper;
4
5 use_ok 'SL::Request', qw(flatten unflatten);
6
7 use constant DEBUG => 0;
8
9 sub f ($$$) {
10   my $flat = flatten($_[0]);
11   print Dumper($flat) if DEBUG;
12
13   my $unflat = unflatten($flat);
14   print Dumper($unflat) if DEBUG;
15
16   cmp_deeply($flat, $_[1], $_[2] . " flatten");
17   cmp_deeply($unflat, $_[0], $_[2] . " unflatten");
18 }
19
20 f {
21   test => 1,
22   whut => 2
23 },
24 [
25   [ test => 1 ],
26   [ whut => 2 ],
27 ], 'simple case';
28
29 f { a => { b => 2 } },
30 [
31  [ 'a.b' => 2 ]
32 ], 'simple hash nesting';
33
34 f { a => [ 2,  4 ] },
35 [
36  [  'a[]' => 2 ],
37  [  'a[]' => 4 ],
38 ], 'simple array';
39
40 f { a => [ { c => 1, d => 2 }, { c => 3, d => 4 }, ] },
41 [
42   [ 'a[+].c', 1 ],
43   [ 'a[].d', 2 ],
44   [ 'a[+].c', 3 ],
45   [ 'a[].d', 4  ],
46 ], 'array of hashes';
47
48 f { a => [ { a => 1, b => 2 }, { a => 3, c => 4 }, ] },
49 [
50   [ 'a[+].a', 1 ],
51   [ 'a[].b', 2 ],
52   [ 'a[+].a', 3 ],
53   [ 'a[].c', 4  ],
54 ], 'array of hashes with not existing keys';
55
56
57 # tests from Hash::Flatten below
58 f {
59   'x' => 1,
60   'y' => {
61     'a' => 2,
62     'b' => {
63       'p' => 3,
64       'q' => 4
65     },
66   }
67 }, [
68  [ 'x'     => 1, ],
69  [ 'y.a'   => 2, ],
70  [ 'y.b.p' => 3, ],
71  [ 'y.b.q' => 4  ],
72 ], 'Hash::Flatten 1';
73
74
75 f {
76   'x' => 1,
77   '0' => {
78     '1' => 2,
79   },
80   'a' => [1,2,3],
81 },
82 [
83  ['0.1'  => 2, ],
84  ['a[]'  => 1, ],
85  ['a[]'  => 2, ],
86  ['a[]'  => 3, ],
87  ['x'    => 1, ],
88 ], 'Hash::Flatten 2 - weird keys and values';
89
90
91 f {
92   'x' => 1,
93   'ay' => {
94     'a' => 2,
95     'b' => {
96       'p' => 3,
97       'q' => 4
98     },
99   },
100   'y' => [
101     'a', 2,
102     {
103       'baz' => 'bum',
104     },
105   ]
106 },
107 [
108   [ 'ay.a'    => 2,       ],
109   [ 'ay.b.p'  => 3,       ],
110   [ 'ay.b.q'  => 4,       ],
111   [ 'x'       => 1,       ],
112   [ 'y[]'     => 'a',    ],
113   [ 'y[]'     => 2        ],
114   [ 'y[+].baz' => 'bum',  ],
115 ], 'Hash::Flatten 3 - mixed';
116
117 f {
118   'x' => 1,
119   'y' => [
120     [
121       'a', 'fool', 'is',
122     ],
123     [
124       'easily', [ 'parted', 'from' ], 'his'
125     ],
126     'money',
127   ]
128 },
129 [
130  [ 'x'        => 1,        ],
131  [ 'y[+][]'   => 'a',      ],
132  [ 'y[][]'    => 'fool',   ],
133  [ 'y[][]'    => 'is'      ],
134  [ 'y[+][]'   => 'easily', ],
135  [ 'y[][+][]' => 'parted', ],
136  [ 'y[][][]'  => 'from',   ],
137  [ 'y[][]'    => 'his',    ],
138  [ 'y[]'      => 'money',  ],
139 ], 'Hash::Flatten 4 - array nesting';
140
141 f {
142   'x' => 1,
143   'ay' => {
144     'a' => 2,
145     'b' => {
146       'p' => 3,
147       'q' => 4
148     },
149   },
150   's' => 'hey',
151   'y' => [
152     'a', 2, {
153       'baz' => 'bum',
154     },
155   ]
156 },
157 [
158   [ 'ay.a'     => 2,     ],
159   [ 'ay.b.p'   => 3,     ],
160   [ 'ay.b.q'   => 4,     ],
161   [ 's'        => 'hey', ],
162   [ 'x'        => 1,     ],
163   [ 'y[]'      => 'a',   ],
164   [ 'y[]'      => 2      ],
165   [ 'y[+].baz' => 'bum', ],
166 ], 'Hash::Flatten 5 - deep mix';
167
168 done_testing();