forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_test.go
More file actions
356 lines (306 loc) · 10.7 KB
/
assert_test.go
File metadata and controls
356 lines (306 loc) · 10.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package gosnowflake
import (
"bytes"
"errors"
"fmt"
"math"
"reflect"
"regexp"
"slices"
"strings"
"testing"
"time"
)
func assertNilE(t *testing.T, actual any, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNilF(t *testing.T, actual any, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNotNilE(t *testing.T, actual any, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertErrIsF(t *testing.T, actual, expected error, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateErrIs(actual, expected, descriptions...))
}
func assertErrIsE(t *testing.T, actual, expected error, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateErrIs(actual, expected, descriptions...))
}
func assertErrorsAsF(t *testing.T, err error, target any, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateErrorsAs(err, target, descriptions...))
}
func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertEqualIgnoringWhitespaceE(t *testing.T, actual string, expected string, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEqualIgnoringWhitespace(actual, expected, descriptions...))
}
func assertEqualEpsilonE(t *testing.T, actual, expected, epsilon float64, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEqualEpsilon(actual, expected, epsilon, descriptions...))
}
func assertDeepEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateDeepEqual(actual, expected, descriptions...))
}
func assertNotEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateNotEqual(actual, expected, descriptions...))
}
func assertNotEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateNotEqual(actual, expected, descriptions...))
}
func assertBytesEqualE(t *testing.T, actual []byte, expected []byte, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateBytesEqual(actual, expected, descriptions...))
}
func assertTrueF(t *testing.T, actual bool, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateEqual(actual, true, descriptions...))
}
func assertTrueE(t *testing.T, actual bool, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEqual(actual, true, descriptions...))
}
func assertFalseF(t *testing.T, actual bool, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateEqual(actual, false, descriptions...))
}
func assertFalseE(t *testing.T, actual bool, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEqual(actual, false, descriptions...))
}
func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}
func assertStringContainsF(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}
func assertEmptyStringE(t *testing.T, actual string, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEmptyString(actual, descriptions...))
}
func assertHasPrefixF(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
t.Helper()
fatalOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
}
func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
}
func assertBetweenE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateValueBetween(value, min, max, descriptions...))
}
func assertBetweenInclusiveE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateValueBetweenInclusive(value, min, max, descriptions...))
}
func assertEmptyE[T any](t *testing.T, actual []T, descriptions ...string) {
t.Helper()
errorOnNonEmpty(t, validateEmpty(actual, descriptions...))
}
func fatalOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Helper()
t.Fatal(formatErrorMessage(errMsg))
}
}
func errorOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Helper()
t.Error(formatErrorMessage(errMsg))
}
}
func formatErrorMessage(errMsg string) string {
return fmt.Sprintf("[%s] %s", time.Now().Format(time.RFC3339Nano), maskSecrets(errMsg))
}
func validateNil(actual any, descriptions ...string) string {
if isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", maskSecrets(fmt.Sprintf("%v", actual)), desc)
}
func validateNotNil(actual any, descriptions ...string) string {
if !isNil(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected to be not nil but was not. %s", desc)
}
func validateErrIs(actual, expected error, descriptions ...string) string {
if errors.Is(actual, expected) {
return ""
}
desc := joinDescriptions(descriptions...)
actualStr := "nil"
expectedStr := "nil"
if actual != nil {
actualStr = maskSecrets(actual.Error())
}
if expected != nil {
expectedStr = maskSecrets(expected.Error())
}
return fmt.Sprintf("expected %v to be %v. %s", actualStr, expectedStr, desc)
}
func validateErrorsAs(err error, target any, descriptions ...string) string {
if errors.As(err, target) {
return ""
}
desc := joinDescriptions(descriptions...)
errStr := "nil"
if err != nil {
errStr = maskSecrets(err.Error())
}
targetType := reflect.TypeOf(target)
return fmt.Sprintf("expected error %v to be assignable to %v but was not. %s", errStr, targetType, desc)
}
func validateEqual(actual any, expected any, descriptions ...string) string {
if expected == actual {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s",
maskSecrets(fmt.Sprintf("%v", actual)),
maskSecrets(fmt.Sprintf("%v", expected)),
desc)
}
func removeWhitespaces(s string) string {
pattern, err := regexp.Compile(`\s+`)
if err != nil {
panic(err)
}
return pattern.ReplaceAllString(s, "")
}
func validateEqualIgnoringWhitespace(actual string, expected string, descriptions ...string) string {
if removeWhitespaces(expected) == removeWhitespaces(actual) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s",
maskSecrets(actual),
maskSecrets(expected),
desc)
}
func validateEqualEpsilon(actual, expected, epsilon float64, descriptions ...string) string {
if math.Abs(actual-expected) < epsilon {
return ""
}
return fmt.Sprintf("expected \"%f\" to be equal to \"%f\" within epsilon \"%f\" but was not. %s", actual, expected, epsilon, joinDescriptions(descriptions...))
}
func validateDeepEqual(actual any, expected any, descriptions ...string) string {
if reflect.DeepEqual(actual, expected) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s",
maskSecrets(fmt.Sprintf("%v", actual)),
maskSecrets(fmt.Sprintf("%v", expected)),
desc)
}
func validateNotEqual(actual any, expected any, descriptions ...string) string {
if expected != actual {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" not to be equal to \"%s\" but they were the same. %s",
maskSecrets(fmt.Sprintf("%v", actual)),
maskSecrets(fmt.Sprintf("%v", expected)),
desc)
}
func validateBytesEqual(actual []byte, expected []byte, descriptions ...string) string {
if bytes.Equal(actual, expected) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s",
maskSecrets(string(actual)),
maskSecrets(string(expected)),
desc)
}
func validateStringContains(actual string, expectedToContain string, descriptions ...string) string {
if strings.Contains(actual, expectedToContain) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s",
maskSecrets(actual),
maskSecrets(expectedToContain),
desc)
}
func validateEmptyString(actual string, descriptions ...string) string {
if actual == "" {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to be empty, but was not. %s", maskSecrets(actual), desc)
}
func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string {
if strings.HasPrefix(actual, expectedPrefix) {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s",
maskSecrets(actual),
maskSecrets(expectedPrefix),
desc)
}
func validateValueBetween(value float64, min float64, max float64, descriptions ...string) string {
if value > min && value < max {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" should be between \"%s\" and \"%s\" but did not. %s",
fmt.Sprintf("%f", value),
fmt.Sprintf("%f", min),
fmt.Sprintf("%f", max),
desc)
}
func validateValueBetweenInclusive(value float64, min float64, max float64, descriptions ...string) string {
if value >= min && value <= max {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%s\" should be between \"%s\" and \"%s\" inclusively but did not. %s",
fmt.Sprintf("%f", value),
fmt.Sprintf("%f", min),
fmt.Sprintf("%f", max),
desc)
}
func validateEmpty[T any](value []T, descriptions ...string) string {
if len(value) == 0 {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%v\" to be empty. %s", maskSecrets(fmt.Sprintf("%v", value)), desc)
}
func joinDescriptions(descriptions ...string) string {
return strings.Join(descriptions, " ")
}
func isNil(value any) bool {
if value == nil {
return true
}
val := reflect.ValueOf(value)
return slices.Contains([]reflect.Kind{reflect.Pointer, reflect.Slice, reflect.Map, reflect.Interface, reflect.Func}, val.Kind()) && val.IsNil()
}