-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.go
432 lines (393 loc) · 10.5 KB
/
checks.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Package testutils contains convenient testing checkers that compare a produced
// value against an expected value (or condition).
// There are value checks like `CheckEqual(expected, produced, t)“, and
// checks that should run deferred like `defer ShouldPanic(t)`.
package testutils
import (
"bytes"
"io"
"math"
"os"
"reflect"
"regexp"
"testing"
)
func unequalValues(e, g interface{}, t *testing.T) {
t.Helper()
t.Fatalf("Expected equal: %T %v, got %T %v", e, e, g, g)
}
func equalValues(e, g interface{}, t *testing.T) {
t.Helper()
t.Fatalf("Expected not equal: %T %v, got %T %v", e, e, g, g)
}
// CheckEqual checks if two values are deeply equal and calls t.Fatalf if not
func CheckEqual(expected interface{}, got interface{}, t *testing.T) {
if !valuesEqual(expected, got) {
t.Helper()
unequalValues(expected, got, t)
}
}
// CheckNotEqual checks if two values are deeply equal and calls t.Fatalf if not
func CheckNotEqual(expected interface{}, got interface{}, t *testing.T) {
if valuesEqual(expected, got) {
t.Helper()
equalValues(expected, got, t)
}
}
// CheckMatches checks expected regular expression is matched by the given string and calls t.Fatalf if not
//
// The expected regular expression can be either a *regexp.Regexp or a string that represents a valid regexp
func CheckMatches(expected interface{}, got string, t *testing.T) {
var rx *regexp.Regexp
switch expected := expected.(type) {
case *regexp.Regexp:
rx = expected
case string:
var err error
rx, err = regexp.Compile(expected)
if err != nil {
t.Fatalf("CheckMatches: illegal regexp %q", expected)
}
default:
t.Fatalf("CheckMatches: first argument must be a regexp or a string, got %T %v", expected, expected)
}
if !rx.MatchString(got) {
t.Helper()
t.Fatalf("Expected match for %q, got %s", rx.String(), got)
}
}
// func valuesEqual(a interface{}, b interface{}) bool {
// nc := numericCompare(a, b)
// return nc == 0 || nc == -2 && reflect.DeepEqual(a, b)
// }
func valuesEqual(a interface{}, b interface{}) bool {
nc := numericCompare(a, b)
var ok bool
if nc == -2 {
// Not numerically equal
// Since DeepEqual does not return true for some cases of reflected values. (Not sure why).
sa, ok1 := AsInterface(a)
sb, ok2 := AsInterface(b)
if ok1 && ok2 && sa == sb {
return true
}
ok = reflect.DeepEqual(a, b)
}
return nc == 0 || nc == -2 && ok
}
// CheckEqualAndNoError checks there is no error, and that two values are deeply equal and calls t.Fatalf if not
func CheckEqualAndNoError(expected interface{}, got interface{}, gotError error, t *testing.T) {
t.Helper()
CheckNotError(gotError, t)
if !reflect.DeepEqual(expected, got) {
unequalValues(expected, got, t)
}
}
// CheckContainsElements checks if one slice contains all elements of another slice irrespective of order and uniqueness.
func CheckContainsElements(expected interface{}, got interface{}, t *testing.T) {
if sliceContains(got, expected, false) {
t.Helper()
t.Fatalf("Slice %v does not contain all elements in %v", got, expected)
}
}
// CheckEqualElements checks if two slices contains the exact same set of elements irrespective of order and uniqueness.
func CheckEqualElements(expected interface{}, got interface{}, t *testing.T) {
if !sliceContains(got, expected, true) {
t.Helper()
t.Fatalf("Elements of slice %v and %v differ", expected, got)
}
}
// sliceContainsAll returns true if a contains all elements in b, irrespective of order. Each element is
// matched exactly once. If checkSize is given a and b must have the same size.
func sliceContains(a, b interface{}, checkSize bool) bool {
va := reflect.ValueOf(a)
if va.Kind() != reflect.Slice {
return false
}
vb := reflect.ValueOf(b)
if vb.Kind() != reflect.Slice {
return false
}
tb := vb.Len()
ta := va.Len()
if checkSize && ta != tb {
return false
}
// any set contains an empty set
if tb == 0 {
return true
}
// an empty set cannot contain a non empty set
if ta == 0 {
return false
}
// a smaller set cannot contain all from a larger set
if ta < tb {
return false
}
ma := make([]bool, ta)
nextB:
for ib := 0; ib < tb; ib++ {
eb := vb.Index(ib)
for ia := 0; ia < ta; ia++ {
if ma[ia] {
continue
}
if valuesEqual(eb, va.Index(ia)) {
ma[ia] = true
continue nextB
}
}
return false
}
return true
}
// CheckNil checks if value is nil
func CheckNil(got interface{}, t *testing.T) {
rf := reflect.ValueOf(got)
if rf.IsValid() && !rf.IsNil() {
t.Helper()
t.Fatalf("Expected: nil, got %v", got)
}
}
// CheckNotNil checks if value is not nil
func CheckNotNil(got interface{}, t *testing.T) {
rf := reflect.ValueOf(got)
if !rf.IsValid() || rf.IsNil() {
t.Helper()
t.Fatalf("Expected: not nil, got nil")
}
}
// CheckError checks if there is an error
func CheckError(got interface{}, t *testing.T) {
_, ok := got.(error)
if !ok {
t.Helper()
t.Fatalf("Expected: error, got %v", got)
}
}
// CheckNotError checks if value is not an error
func CheckNotError(got interface{}, t *testing.T) {
err, ok := got.(error)
if ok {
t.Helper()
t.Fatalf("Expected: no error, got %q", err.Error())
}
}
// CheckNumericGreater checks if second value is greater than first. Comparisons are made regardless of
// bit size and an integer is equal to a float if casting it to a float makes it equal.
func CheckNumericGreater(expected interface{}, got interface{}, t *testing.T) {
if numericCompare(expected, got) != 1 {
t.Helper()
t.Fatalf("Expected: %T %v greater than %T %v", expected, expected, got, got)
}
}
// CheckNumericLess checks if second value is less than first. Comparisons are made regardless of
// bit size and an integer is equal to a float if casting it to a float makes it equal.
func CheckNumericLess(expected interface{}, got interface{}, t *testing.T) {
if numericCompare(expected, got) != -1 {
t.Helper()
t.Fatalf("Expected: %T %v less than %T %v", expected, expected, got, got)
}
}
// numericCompare checks if two numeric values are equal. Comparisons are made regardless of
// bit size and an integer is equal to a float if casting it to a float makes it equal.
// Return 0 if numerically equal, 1 if got is greater than expected, and -1 if less.
// In case they are not numeric values a value of -2 is returned.
func numericCompare(expected interface{}, got interface{}) int {
if ei, ok := AsInteger(expected); ok {
if gi, ok := AsInteger(got); ok {
if ei == gi {
return 0
} else if gi > ei {
return 1
}
return -1
} else if gf, ok := AsFloat(got); ok {
if float64(ei) == gf {
return 0
} else if gf > float64(ei) {
return 1
}
return -1
}
} else if ef, ok := AsFloat(expected); ok {
if gf, ok := AsFloat(got); ok {
if ef == gf {
return 0
} else if gf > ef {
return 1
}
return -1
}
// No need to test if "got" is an integer since we know that "expected" isn't
} else if eu, ok := expected.(uint64); ok {
if gu, ok := expected.(uint64); ok && eu == gu {
return 0
} else if gu > eu {
return 1
}
return -1
// No need to test if "got" is an integer since we know that "expected" isn't
}
return -2
}
// AsInteger returns the argument as a signed 64 bit integer and true if the argument
// is an integer that fits into that type. Otherwise it returns 0, false
func AsInteger(v interface{}) (int64, bool) {
ok := true
var rv int64
switch et := v.(type) {
case int8:
rv = int64(et)
case int16:
rv = int64(et)
case int32:
rv = int64(et)
case int:
rv = int64(et)
case int64:
rv = et
case uint8:
rv = int64(et)
case uint16:
rv = int64(et)
case uint32:
rv = int64(et)
case uint:
if et <= math.MaxInt64 {
rv = int64(et)
} else {
ok = false
}
case uint64:
if et <= math.MaxInt64 {
rv = int64(et)
} else {
ok = false
}
default:
ok = false
}
return rv, ok
}
// AsFloat returns the argument as a 64 bit float and true if the argument
// is a float that fits into that type. Otherwise it returns 0, false
func AsFloat(v interface{}) (rv float64, ok bool) {
ok = true
switch et := v.(type) {
case int16:
rv = float64(et)
case int32:
rv = float64(et)
case int64:
rv = float64(et)
case uint16:
rv = float64(et)
case uint32:
rv = float64(et)
case uint64:
rv = float64(et)
case float32:
rv = float64(et)
case float64:
rv = et
default:
ok = false
}
return
}
// AsInterface returns the argument as an interface{}. This is useful when a value
// may be a reflect.Value and an operation does not work on such, but on the underlying real
// value albeit behind an interface{}.
func AsInterface(v interface{}) (rv interface{}, ok bool) {
ok = true
switch et := v.(type) {
case reflect.Value:
rv = et.Interface()
// case string:
// rv = string(et)
// case bool:
// rv = bool(et)
default:
ok = false
}
return
}
// CheckTrue checks if value is true
func CheckTrue(got bool, t *testing.T) {
if !got {
t.Helper()
t.Fatalf("Expected: true, got %v", got)
}
}
// CheckFalse checks if value is false
func CheckFalse(got bool, t *testing.T) {
if got {
t.Helper()
t.Fatalf("Expected: false, got %v", got)
}
}
const chunkSize = 0x10000
// CheckFilesEqual equals checks if the two files have the exact same contents.
func CheckFilesEqual(file1, file2 string, t *testing.T) {
t.Helper()
var fi1, fi2 os.FileInfo
var err error
if fi1, err = os.Stat(file1); err != nil {
t.Fatal(err)
}
if fi2, err = os.Stat(file2); err != nil {
t.Fatal(err)
}
if fi1.IsDir() {
t.Fatalf("%q is a directory", file1)
}
if fi2.IsDir() {
t.Fatalf("%q is a directory", file2)
}
sz := fi1.Size()
if sz != fi2.Size() {
t.Fatalf("size of file %q (%d), does not match size of %q (%d)", file1, fi1.Size(), file2, fi2.Size())
}
var f1, f2 *os.File
if f1, err = os.Open(file1); err != nil {
t.Fatal(err)
}
defer f1.Close()
if f2, err = os.Open(file1); err != nil {
t.Fatal(err)
}
defer f2.Close()
if sz > chunkSize {
sz = chunkSize
}
b1 := make([]byte, sz)
b2 := make([]byte, sz)
for {
n1, err1 := f1.Read(b1)
n2, err2 := f2.Read(b2)
if err1 == io.EOF && err2 == io.EOF {
return
}
if err1 != nil {
t.Fatal(err1)
}
if err2 != nil {
t.Fatal(err2)
}
if !bytes.Equal(b1[0:n1], b2[0:n2]) {
t.Fatalf("content of file %q and %q differ", file1, file2)
}
}
}
// CheckFileExists checks that given file name is for an existing regular file
func CheckFileExists(filename string, t *testing.T) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
t.Fatalf("file %s does not exist", filename)
}
if info.IsDir() {
t.Fatalf("file %s is a directory, not a file", filename)
}
}