-
Notifications
You must be signed in to change notification settings - Fork 5
/
reflectutil_test.go
104 lines (102 loc) · 2.33 KB
/
reflectutil_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
package fixtory
import (
"reflect"
"testing"
)
func TestMapNotZeroFields(t *testing.T) {
type args struct {
from testStruct
to *testStruct
}
tests := []struct {
name string
args args
want *testStruct
wantErr bool
}{
{
name: "fields of from struct overwrite fields of to struct",
args: args{
from: testStruct{
String: "after",
StringPtr: func() *string { s := "after"; return &s }(),
Int: 10,
Float: 10.5,
Array: []int{9, 8, 7},
Map: map[string]bool{"after": true},
ChildStruct: &childStruct{
String: "after",
Int: 10,
},
},
to: &testStruct{
String: "a",
StringPtr: func() *string { s := "a"; return &s }(),
Int: 1,
Float: 0.5,
Array: []int{1, 2, 3},
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: "b",
Int: 2,
},
},
},
want: &testStruct{
String: "after",
StringPtr: func() *string { s := "after"; return &s }(),
Int: 10,
Float: 10.5,
Array: []int{9, 8, 7},
Map: map[string]bool{"after": true},
ChildStruct: &childStruct{
String: "after",
Int: 10,
},
},
},
{
name: "zero value fields of from struct do not overwrite",
args: args{
from: testStruct{},
to: &testStruct{
String: "a",
StringPtr: func() *string { s := "a"; return &s }(),
Int: 1,
Float: 0.5,
Array: []int{1, 2, 3},
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: "b",
Int: 2,
},
},
},
want: &testStruct{
String: "a",
StringPtr: func() *string { s := "a"; return &s }(),
Int: 1,
Float: 0.5,
Array: []int{1, 2, 3},
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: "b",
Int: 2,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if err := recover(); (err != nil) != tt.wantErr {
t.Errorf("MapNotZeroFields() = %v, wantErr %v", err, tt.wantErr)
}
}()
MapNotZeroFields[testStruct](tt.args.from, tt.args.to)
if !reflect.DeepEqual(tt.args.to, tt.want) {
t.Errorf("MapNotZeroFields() = %v, want %v", tt.args.to, tt.want)
}
})
}
}