-
Notifications
You must be signed in to change notification settings - Fork 2
/
checker_test.go
165 lines (157 loc) · 4.03 KB
/
checker_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package structsconv
import (
"bytes"
"log"
"os"
"reflect"
"strings"
"testing"
)
func Test_checkRootValuesTypes(t *testing.T) {
type args struct {
source reflect.Value
target reflect.Value
}
strValue := "strValue"
tests := []struct {
name string
args args
want interface{}
}{
{
name: "checkRootValuesTypes,(str,str)",
args: args{
source: reflect.ValueOf(strValue),
target: reflect.ValueOf(strValue),
},
want: "rules error: source must be a pointer",
},
{
name: "checkRootValuesTypes,(*str,str)",
args: args{
source: reflect.ValueOf(&strValue),
target: reflect.ValueOf(strValue),
},
want: "rules error: target must be a pointer",
},
{
name: "checkRootValuesTypes,(*str,*str)",
args: args{
source: reflect.ValueOf(&strValue),
target: reflect.ValueOf(&strValue),
},
want: "rules error: source must be a pointer to a struct",
},
{
name: "checkRootValuesTypes,(*struct,*str)",
args: args{
source: reflect.ValueOf(&struct{}{}),
target: reflect.ValueOf(&strValue),
},
want: "rules error: target must be a pointer to a struct",
},
{
name: "checkRootValuesTypes,(*struct,*struct)",
args: args{
source: reflect.ValueOf(&struct{}{}),
target: reflect.ValueOf(&struct{}{}),
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkRootValuesTypes(tt.args.source, tt.args.target); err != nil && err.Error() != tt.want {
t.Errorf("logs expected to contain '%s', got '%s'", tt.want, err.Error())
}
})
}
}
func Test_checkMapperRules_panics(t *testing.T) {
key := buildKey(TestSource{}, TestTarget{})
tests := []struct {
name string
rules RulesSet
wantContains string
}{
{
name: "Non-existent target key name,panic expected",
wantContains: "Field 'otherFieldName' is not present in target struct structsconv.TestTarget",
rules: RulesSet{
"otherFieldName": "fieldS1",
},
},
{
name: "Non-existent source field name,panic expected",
wantContains: "Field 'otherFieldName' is not present in source struct structsconv.TestSource",
rules: RulesSet{
"fieldT1": "otherFieldName",
},
},
{
name: "Field kind is the different in origin and target struct,panic expected",
wantContains: "Field 'fieldT1' has different type in source (fieldS2:int) and target (fieldT1:string) structs",
rules: RulesSet{
"fieldT1": "fieldS2",
},
},
{
name: "Custom function returns different type than target,panic expected",
wantContains: "Function 'fieldT1' must return type 'string', currently returns 'int'. Function = 'func() int'",
rules: RulesSet{
"fieldT1": func() int { return 314 },
},
},
{
name: "Not valid rule, int value,panic expected",
wantContains: "Rule 'fieldT1' is not valid",
rules: RulesSet{
"fieldT1": 123,
},
},
{
name: "Not valid rule, int value,panic expected",
wantContains: "Rule 'fieldT1' is not valid",
rules: RulesSet{
"fieldT1": struct{}{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var f = func() { checkMapperRules(key, tt.rules) }
assertPanic(f, tt.wantContains, t)
})
}
}
func Test_checkMapperRules_ignorable(t *testing.T) {
key := buildKey(TestSource{}, TestTarget{})
tests := []struct {
name string
rules RulesSet
wantContains string
}{
{
name: "Ignorable rule, no panic expected",
wantContains: "Field 'ignorableTargetField' is marked as ignored",
rules: RulesSet{
"ignorableTargetField": nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var f = func() { checkMapperRules(key, tt.rules) }
assertLog(f, tt.wantContains, t)
})
}
}
func assertLog(f func(), wantContains string, t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
f()
if !strings.Contains(buf.String(), wantContains) {
t.Errorf("logs expected to contain '%s', got '%s'", wantContains, buf.String())
}
}