-
Notifications
You must be signed in to change notification settings - Fork 19
/
mergemap_test.go
89 lines (83 loc) · 1.83 KB
/
mergemap_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
82
83
84
85
86
87
88
89
package mergemap
import (
"bytes"
"encoding/json"
"testing"
)
func TestMerge(t *testing.T) {
for _, tuple := range []struct {
src string
dst string
expected string
}{
{
src: `{}`,
dst: `{}`,
expected: `{}`,
},
{
src: `{"b":2}`,
dst: `{"a":1}`,
expected: `{"a":1,"b":2}`,
},
{
src: `{"a":0}`,
dst: `{"a":1}`,
expected: `{"a":0}`,
},
{
src: `{"a":{ "y":2}}`,
dst: `{"a":{"x":1 }}`,
expected: `{"a":{"x":1, "y":2}}`,
},
{
src: `{"a":{"x":2}}`,
dst: `{"a":{"x":1}}`,
expected: `{"a":{"x":2}}`,
},
{
src: `{"a":{ "y":7, "z":8}}`,
dst: `{"a":{"x":1, "y":2 }}`,
expected: `{"a":{"x":1, "y":7, "z":8}}`,
},
{
src: `{"1": { "b":1, "2": { "3": { "b":3, "n":[1,2]} } }}`,
dst: `{"1": { "2": { "3": {"a":"A", "n":"xxx"} }, "a":3 }}`,
expected: `{"1": { "b":1, "2": { "3": {"a":"A", "b":3, "n":[1,2]} }, "a":3 }}`,
},
} {
var dst map[string]interface{}
if err := json.Unmarshal([]byte(tuple.dst), &dst); err != nil {
t.Error(err)
continue
}
var src map[string]interface{}
if err := json.Unmarshal([]byte(tuple.src), &src); err != nil {
t.Error(err)
continue
}
var expected map[string]interface{}
if err := json.Unmarshal([]byte(tuple.expected), &expected); err != nil {
t.Error(err)
continue
}
got := Merge(dst, src)
assert(t, expected, got)
}
}
func assert(t *testing.T, expected, got map[string]interface{}) {
expectedBuf, err := json.Marshal(expected)
if err != nil {
t.Error(err)
return
}
gotBuf, err := json.Marshal(got)
if err != nil {
t.Error(err)
return
}
if bytes.Compare(expectedBuf, gotBuf) != 0 {
t.Errorf("expected %s, got %s", string(expectedBuf), string(gotBuf))
return
}
}