Debian Codegraph @

tests/maps_test.go

raw
1 package koanf_test
2
3 import (
4 "testing"
5
6 "github.com/knadh/koanf/maps"
7 "github.com/stretchr/testify/assert"
8 )
9
10 var testMap = map[string]any{
11 "parent": map[string]any{
12 "child": map[string]any{
13 "key": 123,
14 "key.with.dot": 456,
15 },
16 },
17 "top": 789,
18 "empty": map[string]any{},
19 }
20 var testMap2 = map[string]any{
21 "list": []any{
22 map[string]any{
23 "child": map[string]any{
24 "key": 123,
25 },
26 },
27 map[string]any{
28 "child": map[string]any{
29 "key": 123,
30 },
31 },
32 },
33 "parent": map[string]any{
34 "child": map[string]any{
35 "key": 123,
36 },
37 },
38 "top": 789,
39 "empty": map[string]any{},
40 }
41
42 var testMap3 = map[string]any{
43 "list": []any{
44 map[string]any{
45 "child": map[string]any{
46 "key": 123,
47 "child": map[string]any{
48 "key": 123,
49 "child": map[string]any{
50 "key": 123,
51 "child": map[string]any{
52 "key": 123,
53 "child": map[string]any{
54 "key": 123,
55 "child": map[string]any{
56 "key": 123,
57 "child": map[string]any{
58 "key": 123,
59 },
60 },
61 },
62 },
63 },
64 },
65 },
66 },
67 map[string]any{
68 "child": map[string]any{
69 "key": 123,
70 "child": map[string]any{
71 "key": 123,
72 },
73 },
74 },
75 },
76 "parent": map[string]any{
77 "child": map[string]any{
78 "key": 123,
79 "child": map[string]any{
80 "key": 123,
81 "child": map[string]any{
82 "key": 123,
83 "child": map[string]any{
84 "key": 123,
85 "child": map[string]any{
86 "key": 123,
87 },
88 },
89 },
90 },
91 },
92 },
93 "top": 789,
94 "child": map[string]any{
95 "key": 123,
96 "child": map[string]any{
97 "key": 123,
98 },
99 },
100 "empty": map[string]any{},
101 }
102
103 func TestFlatten(t *testing.T) {
104 f, k := maps.Flatten(testMap, nil, delim)
105 assert.Equal(t, map[string]any{
106 "parent.child.key": 123,
107 "parent.child.key.with.dot": 456,
108 "top": 789,
109 "empty": map[string]any{},
110 }, f)
111 assert.Equal(t, map[string][]string{
112 "parent.child.key": {"parent", "child", "key"},
113 "parent.child.key.with.dot": {"parent", "child", "key.with.dot"},
114 "top": {"top"},
115 "empty": {"empty"},
116 }, k)
117 }
118
119 func BenchmarkFlatten(b *testing.B) {
120 for n := 0; n < b.N; n++ {
121 maps.Flatten(testMap3, nil, delim)
122 }
123 }
124
125 func TestUnflatten(t *testing.T) {
126 m, _ := maps.Flatten(testMap, nil, delim)
127 um := maps.Unflatten(m, delim)
128 assert.NotEqual(t, um, testMap)
129
130 m, _ = maps.Flatten(testMap2, nil, delim)
131 um = maps.Unflatten(m, delim)
132 assert.Equal(t, um, testMap2)
133 }
134
135 func TestIntfaceKeysToStrings(t *testing.T) {
136 m := map[string]any{
137 "list": []any{
138 map[any]any{
139 "child": map[any]any{
140 "key": 123,
141 },
142 },
143 map[any]any{
144 "child": map[any]any{
145 "key": 123,
146 },
147 },
148 },
149 "parent": map[any]any{
150 "child": map[any]any{
151 "key": 123,
152 },
153 },
154 "top": 789,
155 "empty": map[any]any{},
156 }
157 maps.IntfaceKeysToStrings(m)
158 assert.Equal(t, testMap2, m)
159 }
160
161 func TestMapMerge(t *testing.T) {
162 m1 := map[string]any{
163 "parent": map[string]any{
164 "child": map[string]any{
165 "key": 123,
166 },
167 "child2": map[string]any{
168 "key": 123,
169 },
170 },
171 "top": 789,
172 "empty": map[string]any{},
173 "key": 1,
174 }
175 m2 := map[string]any{
176 "parent": map[string]any{
177 "child": map[string]any{
178 "key": 456,
179 "val": 789,
180 },
181 },
182 "child": map[string]any{
183 "key": 456,
184 },
185 "newtop": 999,
186 "empty": []int{1, 2, 3},
187 "key": "string",
188 }
189 maps.Merge(m2, m1)
190
191 out := map[string]any{
192 "parent": map[string]any{
193 "child": map[string]any{
194 "key": 456,
195 "val": 789,
196 },
197 "child2": map[string]any{
198 "key": 123,
199 },
200 },
201 "child": map[string]any{
202 "key": 456,
203 },
204 "top": 789,
205 "newtop": 999,
206 "empty": []int{1, 2, 3},
207 "key": "string",
208 }
209 assert.Equal(t, out, m1)
210 }
211
212 func TestMapMerge2(t *testing.T) {
213 src := map[string]any{
214 "globals": map[string]any{
215 "features": map[string]any{
216 "testing": map[string]any{
217 "enabled": false,
218 },
219 },
220 },
221 }
222
223 dest := map[string]any{
224 "globals": map[string]any{
225 "features": map[string]any{
226 "testing": map[string]any{
227 "enabled": true,
228 "anotherKey": "value",
229 },
230 },
231 },
232 }
233
234 maps.Merge(src, dest)
235 }
236
237 func TestMergeStrict(t *testing.T) {
238 m1 := map[string]any{
239 "parent": map[string]any{
240 "child": map[string]any{
241 "key": "123",
242 },
243 "child2": map[string]any{
244 "key": 123,
245 },
246 },
247 "top": 789,
248 "empty": []int{},
249 "key": 1,
250 }
251 m2 := map[string]any{
252 "parent": map[string]any{
253 "child": map[string]any{
254 "key": 456,
255 "val": 789,
256 },
257 },
258 "child": map[string]any{
259 "key": 456,
260 },
261 "newtop": 999,
262 "empty": []int{1, 2, 3},
263 "key": "string",
264 }
265 err := maps.MergeStrict(m2, m1)
266 assert.Error(t, err)
267 }
268
269 func TestMapDelete(t *testing.T) {
270 testMap := map[string]any{
271 "parent": map[string]any{
272 "child": map[string]any{
273 "key": 123,
274 "key.with.dot": 456,
275 },
276 },
277 "top": 789,
278 "empty": map[string]any{},
279 }
280 testMap2 := map[string]any{
281 "list": []any{
282 map[string]any{
283 "child": map[string]any{
284 "key": 123,
285 },
286 },
287 map[string]any{
288 "child": map[string]any{
289 "key": 123,
290 },
291 },
292 },
293 "parent": map[string]any{
294 "child": map[string]any{
295 "key": 123,
296 },
297 },
298 "top": 789,
299 "empty": map[string]any{},
300 }
301
302 maps.Delete(testMap, []string{"parent", "child"})
303 assert.Equal(t, map[string]any{
304 "top": 789,
305 "empty": map[string]any{},
306 }, testMap)
307
308 maps.Delete(testMap2, []string{"list"})
309 maps.Delete(testMap2, []string{"empty"})
310 assert.Equal(t, map[string]any{
311 "parent": map[string]any{
312 "child": map[string]any{
313 "key": 123,
314 },
315 },
316 "top": 789,
317 }, testMap2)
318 }
319
320 func TestSearch(t *testing.T) {
321 assert.Equal(t, 123, maps.Search(testMap, []string{"parent", "child", "key"}))
322 assert.Equal(t, map[string]any{
323 "key": 123,
324 "key.with.dot": 456,
325 }, maps.Search(testMap, []string{"parent", "child"}))
326 assert.Equal(t, 456, maps.Search(testMap, []string{"parent", "child", "key.with.dot"}))
327 assert.Equal(t, 789, maps.Search(testMap, []string{"top"}))
328 assert.Equal(t, map[string]any{}, maps.Search(testMap, []string{"empty"}))
329 assert.Nil(t, maps.Search(testMap, []string{"xxx", "xxx"}))
330 }
331
332 func TestCopy(t *testing.T) {
333 mp := map[string]any{
334 "parent": map[string]any{
335 "child": map[string]any{
336 "key": float64(123),
337 "key.with.dot": float64(456),
338 },
339 },
340 "top": float64(789),
341 "empty": map[string]any{},
342 }
343 assert.Equal(t, mp, maps.Copy(mp))
344 }
345
346 func TestLookupMaps(t *testing.T) {
347 assert.Equal(t, map[string]bool{"a": true, "b": true}, maps.StringSliceToLookupMap([]string{"a", "b"}))
348 assert.Equal(t, map[string]bool{}, maps.StringSliceToLookupMap(nil))
349 assert.Equal(t, map[int64]bool{1: true, 2: true}, maps.Int64SliceToLookupMap([]int64{1, 2}))
350 assert.Equal(t, map[int64]bool{}, maps.Int64SliceToLookupMap(nil))
351
352 }