forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_test.go
81 lines (76 loc) · 1.96 KB
/
translate_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
80
81
package lib
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestTranslateKeys(t *testing.T) {
fromJSON := func(s string) map[string]interface{} {
var m map[string]interface{}
if err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatal(err)
}
return m
}
tests := []struct {
desc string
in map[string]interface{}
out map[string]interface{}
dict map[string]string
}{
{
desc: "x->y",
in: map[string]interface{}{"a": "aa", "x": "xx"},
out: map[string]interface{}{"a": "aa", "y": "xx"},
dict: map[string]string{"x": "y"},
},
{
desc: "discard x",
in: map[string]interface{}{"a": "aa", "x": "xx", "y": "yy"},
out: map[string]interface{}{"a": "aa", "y": "yy"},
dict: map[string]string{"x": "y"},
},
{
desc: "b.x->b.y",
in: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"x": "xx"}},
out: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"y": "xx"}},
dict: map[string]string{"x": "y"},
},
{
desc: "json: x->y",
in: fromJSON(`{"a":"aa","x":"xx"}`),
out: fromJSON(`{"a":"aa","y":"xx"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: X->y",
in: fromJSON(`{"a":"aa","X":"xx"}`),
out: fromJSON(`{"a":"aa","y":"xx"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: discard x",
in: fromJSON(`{"a":"aa","x":"xx","y":"yy"}`),
out: fromJSON(`{"a":"aa","y":"yy"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: b.x->b.y",
in: fromJSON(`{"a":"aa","b":{"x":"xx"}}`),
out: fromJSON(`{"a":"aa","b":{"y":"xx"}}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: b[0].x->b[0].y",
in: fromJSON(`{"a":"aa","b":[{"x":"xx"}]}`),
out: fromJSON(`{"a":"aa","b":[{"y":"xx"}]}`),
dict: map[string]string{"x": "y"},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
TranslateKeys(tt.in, tt.dict)
require.Equal(t, tt.out, tt.in)
})
}
}