-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_test.go
79 lines (64 loc) · 1.51 KB
/
map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gutil
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestKeyValuesToMap(t *testing.T) {
list := []interface{}{"a", "b", "1", "2"}
m := map[string]interface{}{
"a": "b",
"1": "2",
}
result, err := KeyValuesToMap(list...)
if assert.Nil(t, err) {
assert.Equal(t, m, result)
}
}
func TestMapToKeyValue(t *testing.T) {
m := map[string]interface{}{
"1": "2",
"a": "b",
}
list := []interface{}{"1", "2", "a", "b"}
result := MapToKeyValue(m)
assert.Equal(t, list, result)
}
func TestMapKeysExtractor_Extract(t *testing.T) {
m := map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"d": 1,
},
"c": 1,
},
}
depths := []struct {
tag string
depth int
keys []string
}{
{"t1", 1, []string{"a"}},
{"t2", 2, []string{"a", "a.b", "a.c"}},
{"t3", 3, []string{"a", "a.b", "a.b.d", "a.c"}},
{"t4", 10, []string{"a", "a.b", "a.b.d", "a.c"}},
}
for _, inp := range depths {
// FIXME: return result is map, map keys order is in random, so this
// assertion is not true always, but in real test is true. so chang
// this assertion.
keys := MapPathExtractor{Depth: inp.depth, Separator: "."}.Extract(m)
assert.Equal(t, inp.keys, keys, inp.tag)
}
}
func TestMapKeysExtractor_ExtractInvalidDepth(t *testing.T) {
m := map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"d": 1,
},
"c": 1,
},
}
keys := MapPathExtractor{Depth: 0, Separator: "."}.Extract(m)
assert.Nil(t, keys)
}