forked from darccio/mergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue230_test.go
85 lines (76 loc) · 1.67 KB
/
issue230_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
package mergo_test
import (
"testing"
"dario.cat/mergo"
)
var testDataM = []struct {
M1 mapTest
M2 mapTest
WithOverrideEmptyValue bool
ExpectedMap map[int]int
}{
{
M1: mapTest{
M: map[int]int{1: 1, 3: 3},
},
M2: mapTest{
M: map[int]int{1: 2, 2: 2},
},
WithOverrideEmptyValue: true,
ExpectedMap: map[int]int{1: 1, 3: 3},
},
{
M1: mapTest{
M: map[int]int{1: 1, 3: 3},
},
M2: mapTest{
M: map[int]int{1: 2, 2: 2},
},
WithOverrideEmptyValue: false,
ExpectedMap: map[int]int{1: 1, 2: 2, 3: 3},
},
{
M1: mapTest{
M: map[int]int{},
},
M2: mapTest{
M: map[int]int{1: 2, 2: 2},
},
WithOverrideEmptyValue: true,
ExpectedMap: map[int]int{},
},
{
M1: mapTest{
M: map[int]int{},
},
M2: mapTest{
M: map[int]int{1: 2, 2: 2},
},
WithOverrideEmptyValue: false,
ExpectedMap: map[int]int{1: 2, 2: 2},
},
}
func withOverrideEmptyValue(enable bool) func(*mergo.Config) {
if enable {
return mergo.WithOverwriteWithEmptyValue
}
return mergo.WithOverride
}
func TestMergeMapWithOverride(t *testing.T) {
t.Parallel()
for _, data := range testDataM {
err := mergo.Merge(&data.M2, data.M1, withOverrideEmptyValue(data.WithOverrideEmptyValue))
if err != nil {
t.Errorf("Error while merging %s", err)
}
if len(data.M2.M) != len(data.ExpectedMap) {
t.Errorf("Got %d elements in map, but expected %d", len(data.M2.M), len(data.ExpectedMap))
return
}
for i, val := range data.M2.M {
if val != data.ExpectedMap[i] {
t.Errorf("Expected value: %d, but got %d while merging map", data.ExpectedMap[i], val)
}
}
}
}