590f5cf28ad72f6942390a2a394ce2a06bfeb80d
[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 # tests from Hash::Flatten below
49 f {
50   'x' => 1,
51   'y' => {
52     'a' => 2,
53     'b' => {
54       'p' => 3,
55       'q' => 4
56     },
57   }
58 }, [
59  [ 'x'     => 1, ],
60  [ 'y.a'   => 2, ],
61  [ 'y.b.p' => 3, ],
62  [ 'y.b.q' => 4  ],
63 ], 'Hash::Flatten 1';
64
65
66 f {
67   'x' => 1,
68   '0' => {
69     '1' => 2,
70   },
71   'a' => [1,2,3],
72 },
73 [
74  ['0.1'  => 2, ],
75  ['a[]'  => 1, ],
76  ['a[]'  => 2, ],
77  ['a[]'  => 3, ],
78  ['x'    => 1, ],
79 ], 'Hash::Flatten 2 - weird keys and values';
80
81
82 f {
83   'x' => 1,
84   'ay' => {
85     'a' => 2,
86     'b' => {
87       'p' => 3,
88       'q' => 4
89     },
90   },
91   'y' => [
92     'a', 2,
93     {
94       'baz' => 'bum',
95     },
96   ]
97 },
98 [
99   [ 'ay.a'    => 2,       ],
100   [ 'ay.b.p'  => 3,       ],
101   [ 'ay.b.q'  => 4,       ],
102   [ 'x'       => 1,       ],
103   [ 'y[]'     => 'a',    ],
104   [ 'y[]'     => 2        ],
105   [ 'y[+].baz' => 'bum',  ],
106 ], 'Hash::Flatten 3 - mixed';
107
108 f {
109   'x' => 1,
110   'y' => [
111     [
112       'a', 'fool', 'is',
113     ],
114     [
115       'easily', [ 'parted', 'from' ], 'his'
116     ],
117     'money',
118   ]
119 },
120 [
121  [ 'x'        => 1,        ],
122  [ 'y[+][]'   => 'a',      ],
123  [ 'y[][]'    => 'fool',   ],
124  [ 'y[][]'    => 'is'      ],
125  [ 'y[+][]'   => 'easily', ],
126  [ 'y[][+][]' => 'parted', ],
127  [ 'y[][][]'  => 'from',   ],
128  [ 'y[][]'    => 'his',    ],
129  [ 'y[]'      => 'money',  ],
130 ], 'Hash::Flatten 4 - array nesting';
131
132 f {
133   'x' => 1,
134   'ay' => {
135     'a' => 2,
136     'b' => {
137       'p' => 3,
138       'q' => 4
139     },
140   },
141   's' => 'hey',
142   'y' => [
143     'a', 2, {
144       'baz' => 'bum',
145     },
146   ]
147 },
148 [
149   [ 'ay.a'     => 2,     ],
150   [ 'ay.b.p'   => 3,     ],
151   [ 'ay.b.q'   => 4,     ],
152   [ 's'        => 'hey', ],
153   [ 'x'        => 1,     ],
154   [ 'y[]'      => 'a',   ],
155   [ 'y[]'      => 2      ],
156   [ 'y[+].baz' => 'bum', ],
157 ], 'Hash::Flatten 5 - deep mix';
158
159 done_testing();