-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
470 lines (366 loc) · 14.4 KB
/
Copy pathassert.go
File metadata and controls
470 lines (366 loc) · 14.4 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package assert
import (
"encoding/json"
"errors"
)
// Testing is an interface wrapper around *testing.T
type Testing interface {
Helper()
Errorf(format string, args ...any)
}
// Fail reports a failure message via t.Errorf.
func Fail(t Testing, message string) bool {
t.Helper()
t.Errorf("%s", message)
return false
}
// Failf reports a formatted failure message via t.Errorf.
func Failf(t Testing, format string, args ...any) bool {
t.Helper()
t.Errorf(format, args...)
return false
}
// True asserts that the specified value is true.
func True(t Testing, condition bool, messages ...string) bool {
t.Helper()
if !condition {
return Failf(t, "%sShould be true", join(messages))
}
return true
}
// False asserts that the specified value is false.
func False(t Testing, condition bool, messages ...string) bool {
t.Helper()
if condition {
return Failf(t, "%sShould be false", join(messages))
}
return true
}
// Nil asserts that the specified object is nil.
//
// A typed nil pointer, slice, map, channel, function or unsafe.Pointer is treated as nil.
func Nil(t Testing, object any, messages ...string) bool {
t.Helper()
if !isNil(object) {
return Failf(t, "%sShould be nil\n actual: %s", join(messages), format(object))
}
return true
}
// NotNil asserts that the specified object is NOT nil.
//
// A typed nil pointer, slice, map, channel, function or unsafe.Pointer is treated as nil.
func NotNil(t Testing, object any, messages ...string) bool {
t.Helper()
if isNil(object) {
return Failf(t, "%sShould not be nil\n actual: %s", join(messages), format(object))
}
return true
}
// Same asserts that two references point to the same object.
//
// Both arguments must be references: pointers, slices, maps or channels.
// Two references are the same when they have the same type and address;
// slices must also have the same length and capacity.
//
// Pointers to zero-size values and slices with zero capacity may share an
// address even when allocated separately, and are then reported as same.
func Same[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if identical, why := same(actual, expected); why != valid {
return Failf(t, "%s%s\n actual: %s\nexpected: %s", join(messages), why, format(actual), format(expected))
} else if !identical {
return Failf(t, "%sShould be same\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// NotSame asserts that two references do NOT point to the same object.
//
// Both arguments must be references: pointers, slices, maps or channels.
// Two references are the same when they have the same type and address;
// slices must also have the same length and capacity.
func NotSame[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if identical, why := same(actual, expected); why != valid {
return Failf(t, "%s%s\n actual: %s\nexpected: %s", join(messages), why, format(actual), format(expected))
} else if identical {
return Failf(t, "%sShould not be same\n actual: %s", join(messages), format(actual))
}
return true
}
// Equal asserts that two objects are equal.
//
// Equality is determined with reflect.DeepEqual: pointers are compared by the
// values they reference, and two non-nil functions are never equal. A typed nil
// stored in an interface is not equal to an untyped nil; use [Nil] for that.
func Equal[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if !equal(actual, expected) {
return Failf(t, "%sShould be equal\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// NotEqual asserts that the specified values are NOT equal.
//
// Equality is determined with reflect.DeepEqual: pointers are compared by the
// values they reference, and two non-nil functions are never equal.
func NotEqual[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if equal(actual, expected) {
return Failf(t, "%sShould not be equal\n actual: %s", join(messages), format(actual))
}
return true
}
// EqualDelta asserts that two numeric values differ by at most delta.
//
// Fails when delta is negative or NaN. NaN is only equal to NaN.
func EqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
t.Helper()
if within, why := equalDelta(actual, expected, delta); why != valid {
return Failf(t, "%s%s\n delta: %s", join(messages), why, format(delta))
} else if !within {
return Failf(t, "%sShould be equal in delta\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// NotEqualDelta asserts that two numeric values differ by more than delta.
//
// Fails when delta is negative or NaN. NaN is only equal to NaN.
func NotEqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
t.Helper()
if within, why := equalDelta(actual, expected, delta); why != valid {
return Failf(t, "%s%s\n delta: %s", join(messages), why, format(delta))
} else if within {
return Failf(t, "%sShould not be equal in delta\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// Greater asserts that actual is greater than expected.
//
// Fails when either value is NaN.
func Greater[T Ordered](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if order, ok := compare(actual, expected); !ok || order <= 0 {
return Failf(t, "%sShould be greater\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// GreaterOrEqual asserts that actual is greater than or equal to expected.
//
// Fails when either value is NaN.
func GreaterOrEqual[T Ordered](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if order, ok := compare(actual, expected); !ok || order < 0 {
return Failf(t, "%sShould be greater or equal\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// Less asserts that actual is less than expected.
//
// Fails when either value is NaN.
func Less[T Ordered](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if order, ok := compare(actual, expected); !ok || order >= 0 {
return Failf(t, "%sShould be less\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// LessOrEqual asserts that actual is less than or equal to expected.
//
// Fails when either value is NaN.
func LessOrEqual[T Ordered](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if order, ok := compare(actual, expected); !ok || order > 0 {
return Failf(t, "%sShould be less or equal\n actual: %s\nexpected: %s", join(messages), format(actual), format(expected))
}
return true
}
// Length asserts that object has given length.
//
// Works with strings, arrays, slices, maps and channels. String length is measured in bytes, not runes.
func Length[S Iterable](t Testing, object S, expected int, messages ...string) bool {
t.Helper()
if actual, why := length(object); why != valid {
return Failf(t, "%s%s\n object: %s", join(messages), why, format(object))
} else if actual != expected {
return Failf(t, "%sShould have length\n object: %s\n actual: %d\nexpected: %d", join(messages), format(object), actual, expected)
}
return true
}
// Empty asserts that object has zero length.
//
// Works with strings, arrays, slices, maps and channels. String length is measured in bytes, not runes.
func Empty[S Iterable](t Testing, object S, messages ...string) bool {
t.Helper()
if actual, why := length(object); why != valid {
return Failf(t, "%s%s\n object: %s", join(messages), why, format(object))
} else if actual != 0 {
return Failf(t, "%sShould be empty\n object: %s", join(messages), format(object))
}
return true
}
// NotEmpty asserts that object has non-zero length.
//
// Works with strings, arrays, slices, maps and channels. String length is measured in bytes, not runes.
func NotEmpty[S Iterable](t Testing, object S, messages ...string) bool {
t.Helper()
if actual, why := length(object); why != valid {
return Failf(t, "%s%s\n object: %s", join(messages), why, format(object))
} else if actual == 0 {
return Failf(t, "%sShould not be empty\n object: %s", join(messages), format(object))
}
return true
}
// Contains asserts that object contains given element.
//
// Works with strings, arrays, slices and map values.
func Contains[S Iterable, E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, why := contains(object, element); why != valid {
return Failf(t, "%s%s\n object: %s\n element: %s", join(messages), why, format(object), format(element))
} else if !found {
return Failf(t, "%sShould contain element\n object: %s\n element: %s", join(messages), format(object), format(element))
}
return true
}
// NotContains asserts that object does NOT contain given element.
//
// Works with strings, arrays, slices and map values.
func NotContains[S Iterable, E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, why := contains(object, element); why != valid {
return Failf(t, "%s%s\n object: %s\n element: %s", join(messages), why, format(object), format(element))
} else if found {
return Failf(t, "%sShould not contain element\n object: %s\n element: %s", join(messages), format(object), format(element))
}
return true
}
// Error asserts that error is NOT nil.
//
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
func Error(t Testing, err error, messages ...string) bool {
t.Helper()
if isNil(err) {
return Failf(t, "%sShould be error", join(messages))
}
return true
}
// NoError asserts that error is nil.
//
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
func NoError(t Testing, err error, messages ...string) bool {
t.Helper()
if !isNil(err) {
return Failf(t, "%sShould not be error\n msg: %[2]v\n error: %#[2]v", join(messages), err)
}
return true
}
// ErrorIs asserts that error matches given target according to errors.Is.
func ErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if !errors.Is(err, target) {
return Failf(t, "%sShould match error\n msg: %[2]v\n error: %#[2]v\n target: %#v", join(messages), err, target)
}
return true
}
// NotErrorIs asserts that error does NOT match given target according to errors.Is.
func NotErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if errors.Is(err, target) {
return Failf(t, "%sShould not match error\n msg: %[2]v\n error: %#[2]v\n target: %#v", join(messages), err, target)
}
return true
}
// ErrorAs asserts that error can be assigned to target according to errors.As.
//
// As with errors.As, target must be a non-nil pointer to a type implementing
// error or to any interface type; otherwise ErrorAs panics.
func ErrorAs(t Testing, err error, target any, messages ...string) bool {
t.Helper()
if !errors.As(err, target) {
return Failf(t, "%sShould be assignable to target\n msg: %[2]v\n error: %#[2]v\n target: %T", join(messages), err, target)
}
return true
}
// Matches asserts that a string matches the given regular expression.
func Matches(t Testing, actual, pattern string, messages ...string) bool {
t.Helper()
if matched, err := matches(actual, pattern); err != nil {
return Failf(t, "%sShould be valid regexp\n pattern: %s\n err: %v", join(messages), pattern, err)
} else if !matched {
return Failf(t, "%sShould match regexp\n actual: %s\n pattern: %s", join(messages), actual, pattern)
}
return true
}
// NotMatches asserts that a string does NOT match the given regular expression.
func NotMatches(t Testing, actual, pattern string, messages ...string) bool {
t.Helper()
if matched, err := matches(actual, pattern); err != nil {
return Failf(t, "%sShould be valid regexp\n pattern: %s\n err: %v", join(messages), pattern, err)
} else if matched {
return Failf(t, "%sShould not match regexp\n actual: %s\n pattern: %s", join(messages), actual, pattern)
}
return true
}
// EqualJSON asserts that JSON strings are semantically equal.
//
// Numbers are compared exactly, so 1.0 equals 1 and large integers keep their precision.
func EqualJSON(t Testing, actual, expected string, messages ...string) bool {
t.Helper()
actualJSON, err := decodeJSON(actual)
if err != nil {
return Failf(t, "%sShould be valid JSON\n actual: %s\n err: %v", join(messages), actual, err)
}
expectedJSON, err := decodeJSON(expected)
if err != nil {
return Failf(t, "%sShould be valid JSON\nexpected: %s\n err: %v", join(messages), expected, err)
}
if !equal(actualJSON, expectedJSON) {
return Failf(t, "%sShould be equal JSON\n actual: %s\nexpected: %s", join(messages), actual, expected)
}
return true
}
// JSON asserts that object can be marshaled to expected JSON string.
func JSON(t Testing, actual any, expected string, messages ...string) bool {
t.Helper()
s, err := json.Marshal(actual)
if err != nil {
return Failf(t, "%sShould be marshalable\n actual: %s\n err: %v", join(messages), format(actual), err)
}
return EqualJSON(t, string(s), expected, messages...)
}
// Panics asserts that fn panics.
//
// A panic(nil) is recognised regardless of the GODEBUG panicnil setting.
func Panics(t Testing, fn func(), messages ...string) bool {
t.Helper()
if panicked, _ := panics(fn); !panicked {
return Failf(t, "%sShould panic", join(messages))
}
return true
}
// PanicsWith asserts that fn panics with the expected value.
//
// The panic value must be deeply equal to expected. When expected is an error,
// a panic value matching it according to errors.Is is accepted as well.
// A panic(nil) is reported as a nil value regardless of the GODEBUG panicnil setting.
func PanicsWith(t Testing, fn func(), expected any, messages ...string) bool {
t.Helper()
panicked, value := panics(fn)
if !panicked {
return Failf(t, "%sShould panic\nexpected: %s", join(messages), format(expected))
}
if !panicsWith(value, expected) {
return Failf(t, "%sShould panic with value\n actual: %s\nexpected: %s", join(messages), format(value), format(expected))
}
return true
}
// NotPanics asserts that fn does NOT panic.
//
// A panic(nil) is recognised regardless of the GODEBUG panicnil setting.
func NotPanics(t Testing, fn func(), messages ...string) bool {
t.Helper()
if panicked, value := panics(fn); panicked {
return Failf(t, "%sShould not panic\n value: %s", join(messages), format(value))
}
return true
}