-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
241 lines (225 loc) · 5.53 KB
/
common_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package qcl
import (
"reflect"
"testing"
"time"
)
type (
TestConfig struct {
Host string
Port int
}
TestNestedConfig struct {
Host string
Port int
SSL bool
DB TestDBConfig
}
TestDBConfig struct {
Host string
Port int
SSL bool
}
AllSupportedTypes struct {
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float float32
Float8 float64
Duration time.Duration
}
TestSliceConfig struct {
Hosts []string
Ports []int
}
TestMapConfig struct {
Hosts map[string]string
Ports map[string]int
}
TestPointerConfig struct {
Host *string
Port *int
}
TestNestedPointerConfig struct {
Host *string
Port *int
SSL *bool
DB *TestDBConfig
}
TestConfigWithStructTag struct {
NotHost string `mytag:"HOST"`
NotPort int `mytag:"PORT"`
}
TestEmbeddedConfig struct {
TestConfig
}
)
func Test_splitOnWordBoundaries(t *testing.T) {
tests := map[string]struct {
want []string
}{
"": {
want: []string{},
},
"h": {
want: []string{"h"},
},
"Host": {
want: []string{"Host"},
},
"HostPort": {
want: []string{"Host", "Port"},
},
"HostPortSSL": {
want: []string{"Host", "Port", "SSL"},
},
"HostPortSSLTimeout": {
want: []string{"Host", "Port", "SSL", "Timeout"},
},
"HostPortSSLTimeoutMaxIdleConns": {
want: []string{"Host", "Port", "SSL", "Timeout", "Max", "Idle", "Conns"},
},
"HostPortSSLTimeoutMaxIdleConnsMaxIdleConnsPerHost": {
want: []string{"Host", "Port", "SSL", "Timeout", "Max", "Idle", "Conns", "Max", "Idle", "Conns", "Per", "Host"},
},
"HostPortSSLTimeoutMaxIdleConnsMaxIdleConnsPerHostDB": {
want: []string{"Host", "Port", "SSL", "Timeout", "Max", "Idle", "Conns", "Max", "Idle", "Conns", "Per", "Host", "DB"},
},
}
for input, test := range tests {
t.Run(input, func(t *testing.T) {
if got := splitOnWordBoundaries(input); !reflect.DeepEqual(got, test.want) {
t.Errorf("splitOnWordBoundaries(%v) = %v, want %v", input, got, test.want)
}
})
}
}
func Test_setMapKeysAndValues(t *testing.T) {
tests := map[string]struct {
inputKeys []string
inputVals []string
want any
wantErr bool
}{
"empty": {
inputKeys: []string{},
inputVals: []string{},
want: map[string]string{},
},
"one": {
inputKeys: []string{"key"},
inputVals: []string{"val"},
want: map[string]string{"key": "val"},
},
"two": {
inputKeys: []string{"key1", "key2"},
inputVals: []string{"val1", "val2"},
want: map[string]string{"key1": "val1", "key2": "val2"},
},
"key value mismatch": {
inputKeys: []string{"key1", "key2"},
inputVals: []string{"val1"},
wantErr: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := make(map[string]string)
err := setMapKeysAndValues(reflect.ValueOf(got), test.inputKeys, test.inputVals, "")
if (err != nil) != test.wantErr {
t.Errorf("setMapKeysAndValues() error = %v, wantErr %v", err, test.wantErr)
return
}
if !test.wantErr && !reflect.DeepEqual(got, test.want) {
t.Errorf("setMapKeysAndValues() = %v, want %v", got, test.want)
}
})
}
t.Run("not a map", func(t *testing.T) {
got := ""
err := setMapKeysAndValues(reflect.ValueOf(got), []string{}, []string{}, "")
if err == nil {
t.Errorf("setMapKeysAndValues() error = %v, wantErr %v", err, true)
}
})
t.Run("unsettable type", func(t *testing.T) {
got := map[string]int{}
err := setMapKeysAndValues(reflect.ValueOf(got), []string{"something"}, []string{"this isn't an int"}, "")
if err == nil {
t.Errorf("setMapKeysAndValues() error = %v, wantErr %v", err, true)
}
})
}
func Test_setSliceValues(t *testing.T) {
tests := map[string]struct {
input []string
want any
}{
"empty": {
input: []string{},
want: []string{},
},
"one": {
input: []string{"one"},
want: []string{"one"},
},
"two": {
input: []string{"one", "two"},
want: []string{"one", "two"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := make([]string, 0)
err := setSliceValues(reflect.ValueOf(&got).Elem(), test.input, "")
if err != nil {
t.Errorf("setSliceValues() error = %v", err)
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("setSliceValues() = %v, want %v", got, test.want)
}
})
}
t.Run("not a slice", func(t *testing.T) {
got := ""
err := setSliceValues(reflect.ValueOf(got), []string{}, "")
if err == nil {
t.Errorf("setSliceValues() error = %v, wantErr %v", err, true)
}
})
t.Run("unsettable type", func(t *testing.T) {
got := []int{}
err := setSliceValues(reflect.ValueOf(got), []string{"this isn't an int"}, "")
if err == nil {
t.Errorf("setSliceValues() error = %v, wantErr %v", err, true)
}
})
}
func Test_setField(t *testing.T) {
t.Run("unsettable", func(t *testing.T) {
got := make(chan int, 1)
err := setField(reflect.ValueOf(got), "something", "")
if err == nil {
t.Errorf("setField() error = %v, wantErr %v", err, true)
}
})
}
func Test_errors(t *testing.T) {
var err error = InvalidMapValueError{[]string{"key"}, []string{"val"}}
if err.Error() != "keys -> values mismatch: [key] -> [val]" {
t.Errorf("InvalidMapValueError.Error() = %v, want %v", err.Error(), "keys -> values mismatch: [key] -> [val]")
}
err = UnsupportedTypeError{reflect.TypeOf(1).Kind()}
if err.Error() != "unsupported type: int" {
t.Errorf("UnsupportedTypeError.Error() = %v, want %v", err.Error(), "unsupported type: int")
}
}