1
1
package config
2
2
3
3
import (
4
- "fmt"
5
4
"math"
6
5
"testing"
7
6
"time"
@@ -51,15 +50,15 @@ func TestValidateRequired(t *testing.T) {
51
50
}
52
51
53
52
// unset
54
- assert .ErrorContains (t , o .Validate (o ), "required-option is required" )
53
+ require .ErrorContains (t , o .Validate (o ), "required-option is required" )
55
54
56
55
// set with blank value
57
56
require .NoError (t , o .setValue ("" ))
58
- assert .ErrorContains (t , o .Validate (o ), "required-option is required" )
57
+ require .ErrorContains (t , o .Validate (o ), "required-option is required" )
59
58
60
59
// set with valid value
61
60
require .NoError (t , o .setValue ("not-blank" ))
62
- assert .NoError (t , o .Validate (o ))
61
+ require .NoError (t , o .Validate (o ))
63
62
}
64
63
65
64
func TestValidatePositiveUint32 (t * testing.T ) {
@@ -71,15 +70,15 @@ func TestValidatePositiveUint32(t *testing.T) {
71
70
}
72
71
73
72
// unset
74
- assert .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
73
+ require .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
75
74
76
75
// set with 0 value
77
76
require .NoError (t , o .setValue (uint32 (0 )))
78
- assert .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
77
+ require .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
79
78
80
79
// set with valid value
81
80
require .NoError (t , o .setValue (uint32 (1 )))
82
- assert .NoError (t , o .Validate (o ))
81
+ require .NoError (t , o .Validate (o ))
83
82
}
84
83
85
84
func TestValidatePositiveInt (t * testing.T ) {
@@ -91,19 +90,19 @@ func TestValidatePositiveInt(t *testing.T) {
91
90
}
92
91
93
92
// unset
94
- assert .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
93
+ require .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
95
94
96
95
// set with 0 value
97
96
require .NoError (t , o .setValue (0 ))
98
- assert .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
97
+ require .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
99
98
100
99
// set with negative value
101
100
require .NoError (t , o .setValue (- 1 ))
102
- assert .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
101
+ require .ErrorContains (t , o .Validate (o ), "positive-option must be positive" )
103
102
104
103
// set with valid value
105
104
require .NoError (t , o .setValue (1 ))
106
- assert .NoError (t , o .Validate (o ))
105
+ require .NoError (t , o .Validate (o ))
107
106
}
108
107
109
108
func TestUnassignableField (t * testing.T ) {
@@ -126,143 +125,105 @@ func TestNoParserForFlag(t *testing.T) {
126
125
require .Contains (t , err .Error (), "no parser for flag mykey" )
127
126
}
128
127
129
- func TestSetValue (t * testing.T ) {
128
+ func TestSetValueBool (t * testing.T ) {
130
129
var b bool
130
+ testCases := []struct {
131
+ name string
132
+ value interface {}
133
+ err string
134
+ }{
135
+ {"valid-bool" , true , "" },
136
+ {"valid-bool-string" , "true" , "" },
137
+ {"valid-bool-string-false" , "false" , "" },
138
+ {"valid-bool-string-uppercase" , "TRUE" , "" },
139
+ {"invalid-bool-string" , "foobar" , "invalid boolean value invalid-bool-string: foobar" },
140
+ }
141
+ runTestCases (t , & b , testCases )
142
+ }
143
+
144
+ func TestSetValueInt (t * testing.T ) {
131
145
var i int
146
+ testCases := []struct {
147
+ name string
148
+ value interface {}
149
+ err string
150
+ }{
151
+ {"valid-int" , 1 , "" },
152
+ {"valid-int-string" , "1" , "" },
153
+ {"invalid-int-string" , "abcd" , "strconv.ParseInt: parsing \" abcd\" : invalid syntax" },
154
+ }
155
+ runTestCases (t , & i , testCases )
156
+ }
157
+
158
+ func TestSetValueUint32 (t * testing.T ) {
132
159
var u32 uint32
160
+ testCases := []struct {
161
+ name string
162
+ value interface {}
163
+ err string
164
+ }{
165
+ {"valid-uint32" , 1 , "" },
166
+ {"overflow-uint32" , uint64 (math .MaxUint32 ) + 1 , "overflow-uint32 overflows uint32" },
167
+ {"negative-uint32" , - 1 , "negative-uint32 cannot be negative" },
168
+ }
169
+ runTestCases (t , & u32 , testCases )
170
+ }
171
+
172
+ func TestSetValueUint64 (t * testing.T ) {
133
173
var u64 uint64
174
+ testCases := []struct {
175
+ name string
176
+ value interface {}
177
+ err string
178
+ }{
179
+ {"valid-uint" , 1 , "" },
180
+ {"negative-uint" , - 1 , "negative-uint cannot be negative" },
181
+ }
182
+ runTestCases (t , & u64 , testCases )
183
+ }
184
+
185
+ func TestSetValueFloat64 (t * testing.T ) {
134
186
var f64 float64
135
- var s string
187
+ testCases := []struct {
188
+ name string
189
+ value interface {}
190
+ err string
191
+ }{
192
+ {"valid-float" , 1.05 , "" },
193
+ {"valid-float-int" , int64 (1234 ), "" },
194
+ {"valid-float-string" , "1.05" , "" },
195
+ {"invalid-float-string" , "foobar" , "strconv.ParseFloat: parsing \" foobar\" : invalid syntax" },
196
+ }
197
+ runTestCases (t , & f64 , testCases )
198
+ }
136
199
137
- for _ , scenario := range []struct {
200
+ func TestSetValueString (t * testing.T ) {
201
+ var s string
202
+ testCases := []struct {
138
203
name string
139
- key interface {}
140
204
value interface {}
141
- err error
205
+ err string
142
206
}{
143
- {
144
- name : "valid-bool" ,
145
- key : & b ,
146
- value : true ,
147
- err : nil ,
148
- },
149
- {
150
- name : "valid-bool-string" ,
151
- key : & b ,
152
- value : "true" ,
153
- err : nil ,
154
- },
155
- {
156
- name : "valid-bool-string-false" ,
157
- key : & b ,
158
- value : "false" ,
159
- err : nil ,
160
- },
161
- {
162
- name : "valid-bool-string-uppercase" ,
163
- key : & b ,
164
- value : "TRUE" ,
165
- err : nil ,
166
- },
167
- {
168
- name : "invalid-bool-string" ,
169
- key : & b ,
170
- value : "foobar" ,
171
- err : fmt .Errorf ("invalid boolean value invalid-bool-string: foobar" ),
172
- },
173
- {
174
- name : "invalid-bool-string" ,
175
- key : & b ,
176
- value : "foobar" ,
177
- err : fmt .Errorf ("invalid boolean value invalid-bool-string: foobar" ),
178
- },
179
- {
180
- name : "valid-int" ,
181
- key : & i ,
182
- value : 1 ,
183
- err : nil ,
184
- },
185
- {
186
- name : "valid-int-string" ,
187
- key : & i ,
188
- value : "1" ,
189
- err : nil ,
190
- },
191
- {
192
- name : "invalid-int-string" ,
193
- key : & i ,
194
- value : "abcd" ,
195
- err : fmt .Errorf ("strconv.ParseInt: parsing \" abcd\" : invalid syntax" ),
196
- },
197
- {
198
- name : "valid-uint32" ,
199
- key : & u32 ,
200
- value : 1 ,
201
- err : nil ,
202
- },
203
- {
204
- name : "overflow-uint32" ,
205
- key : & u32 ,
206
- value : uint64 (math .MaxUint32 ) + 1 ,
207
- err : fmt .Errorf ("overflow-uint32 overflows uint32" ),
208
- },
209
- {
210
- name : "negative-uint32" ,
211
- key : & u32 ,
212
- value : - 1 ,
213
- err : fmt .Errorf ("negative-uint32 cannot be negative" ),
214
- },
215
- {
216
- name : "valid-uint" ,
217
- key : & u64 ,
218
- value : 1 ,
219
- err : nil ,
220
- },
221
- {
222
- name : "negative-uint" ,
223
- key : & u64 ,
224
- value : - 1 ,
225
- err : fmt .Errorf ("negative-uint cannot be negative" ),
226
- },
227
- {
228
- name : "valid-float" ,
229
- key : & f64 ,
230
- value : 1.05 ,
231
- err : nil ,
232
- },
233
- {
234
- name : "valid-float-int" ,
235
- key : & f64 ,
236
- value : int64 (1234 ),
237
- err : nil ,
238
- },
239
- {
240
- name : "valid-float-string" ,
241
- key : & f64 ,
242
- value : "1.05" ,
243
- err : nil ,
244
- },
245
- {
246
- name : "invalid-float-string" ,
247
- key : & f64 ,
248
- value : "foobar" ,
249
- err : fmt .Errorf ("strconv.ParseFloat: parsing \" foobar\" : invalid syntax" ),
250
- },
251
- {
252
- name : "valid-string" ,
253
- key : & s ,
254
- value : "foobar" ,
255
- err : nil ,
256
- },
257
- } {
258
- t .Run (scenario .name , func (t * testing.T ) {
207
+ {"valid-string" , "foobar" , "" },
208
+ }
209
+ runTestCases (t , & s , testCases )
210
+ }
211
+
212
+ func runTestCases (t * testing.T , key interface {}, testCases []struct {
213
+ name string
214
+ value interface {}
215
+ err string
216
+ },
217
+ ) {
218
+ for _ , tc := range testCases {
219
+ t .Run (tc .name , func (t * testing.T ) {
259
220
co := Option {
260
- Name : scenario .name ,
261
- ConfigKey : scenario . key ,
221
+ Name : tc .name ,
222
+ ConfigKey : key ,
262
223
}
263
- err := co .setValue (scenario .value )
264
- if scenario .err != nil {
265
- require .EqualError (t , err , scenario .err . Error () )
224
+ err := co .setValue (tc .value )
225
+ if tc .err != "" {
226
+ require .EqualError (t , err , tc .err )
266
227
} else {
267
228
require .NoError (t , err )
268
229
}
0 commit comments