-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_test.go
120 lines (108 loc) · 2.16 KB
/
merge_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package data
import (
"testing"
"github.com/huandu/go-assert"
"github.com/huandu/go-clone"
)
func TestMerge(t *testing.T) {
cases := [][]Data{
{ // 空值
Data{},
},
{ // 简单的数组复制
Make(RawData{"a": 1}),
Make(RawData{"a": 1}),
},
{ // 典型的数据结构
Make(RawData{
"map": RawData(nil), // 故意放一个空数据
}),
Make(RawData{
"str": "abcdefg",
"int": 1234,
"float": -43.21,
"slice": []string{"first", "second"},
"map": RawData{
"a": true,
"b": "string",
"d": []int8{1, 2, 3},
},
}),
Make(RawData{
"str": "zyxwvu",
"uint": 5678,
"nil": nil,
"slice": []string{"third", "forth"},
"map": RawData{
"a": false,
"c": 123,
"d": []uint{4, 5},
},
}),
Make(RawData{
"str": "zyxwvu",
"int": 1234,
"uint": 5678,
"float": -43.21,
"slice": []string{"first", "second", "third", "forth"},
"map": RawData{
"a": false,
"b": "string",
"c": 123,
"d": []uint{4, 5}, // 类型不同,应该覆盖而不是追加
},
}),
},
}
const notExistKey = "this-is-a-key-not-exist"
a := assert.New(t)
for i, c := range cases {
a.Use(&i, &c)
input := c[:len(c)-1]
expected := c[len(c)-1]
if len(input) > 1 {
target := Data{
data: clone.Clone(input[0].data).(RawData),
}
MergeTo(&target, input[1:]...)
a.Equal(expected, target)
}
actual := Merge(input...)
a.Equal(expected, actual)
if actual.Len() == 0 {
continue
}
// 确保 actual 是从 c 中拷贝出来的,修改 actual 不会修改任何 c 的数据
actual.data[notExistKey] = true
for _, d := range input {
_, ok := d.data[notExistKey]
a.Assert(!ok)
}
}
}
func BenchmarkMerge(b *testing.B) {
input := []Data{
Make(RawData{
"str": "abcdefg",
"int": 1234,
"float": -43.21,
"slice": []string{"first", "second"},
"map": RawData{
"a": true,
"b": "string",
},
}),
Make(RawData{
"str": "zyxwvu",
"uint": 5678,
"slice": []string{"third", "forth"},
"map": RawData{
"a": false,
"c": 123,
},
}),
}
for i := 0; i < b.N; i++ {
Merge(input...)
}
}