-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
472 lines (378 loc) · 11 KB
/
assert.go
File metadata and controls
472 lines (378 loc) · 11 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
471
472
package testastic
import (
"cmp"
"errors"
"fmt"
"reflect"
"regexp"
"strings"
"testing"
)
const nilTypeName = "nil"
// fail reports an assertion failure with expected and actual values.
func fail(tb testing.TB, name, expected, actual string) {
tb.Helper()
tb.Errorf(
"testastic: assertion failed\n\n %s\n expected: %s\n actual: %s",
name, red(expected), green(actual),
)
}
// Equal asserts that expected and actual are equal using the == operator.
// Reports a non-fatal error on failure, allowing the test to continue.
func Equal[T comparable](tb testing.TB, expected, actual T) {
tb.Helper()
if expected != actual {
fail(tb, "Equal", formatVal(expected), formatVal(actual))
}
}
// NotEqual asserts that expected and actual are not equal using the == operator.
// Reports a non-fatal error on failure, allowing the test to continue.
func NotEqual[T comparable](tb testing.TB, unexpected, actual T) {
tb.Helper()
if unexpected == actual {
tb.Errorf(
"testastic: assertion failed\n\n NotEqual\n unexpected: %s\n actual: %s",
red(formatVal(unexpected)), green(formatVal(actual)),
)
}
}
// DeepEqual asserts that expected and actual are deeply equal using reflect.DeepEqual.
func DeepEqual[T any](tb testing.TB, expected, actual T) {
tb.Helper()
if !reflect.DeepEqual(expected, actual) {
fail(tb, "DeepEqual", formatVal(expected), formatVal(actual))
}
}
// Nil asserts that value is nil. Handles interface nil correctly by
// checking the underlying pointer for pointer, map, slice, channel, and func types.
func Nil(tb testing.TB, value any) {
tb.Helper()
if !isNil(value) {
fail(tb, "Nil", "nil", formatVal(value))
}
}
// NotNil asserts that value is not nil. Handles interface nil correctly by
// checking the underlying pointer for pointer, map, slice, channel, and func types.
func NotNil(tb testing.TB, value any) {
tb.Helper()
if isNil(value) {
fail(tb, "NotNil", "not nil", "nil")
}
}
// True asserts that value is true.
// Reports a non-fatal error on failure, allowing the test to continue.
func True(tb testing.TB, value bool) {
tb.Helper()
if !value {
fail(tb, "True", "true", "false")
}
}
// False asserts that value is false.
// Reports a non-fatal error on failure, allowing the test to continue.
func False(tb testing.TB, value bool) {
tb.Helper()
if value {
fail(tb, "False", "false", "true")
}
}
// NoError asserts that err is nil.
// Reports a non-fatal error on failure, displaying the error message.
func NoError(tb testing.TB, err error) {
tb.Helper()
if err != nil {
fail(tb, "NoError", "no error", err.Error())
}
}
// Error asserts that err is not nil.
// Reports a non-fatal error on failure, allowing the test to continue.
func Error(tb testing.TB, err error) {
tb.Helper()
if err == nil {
fail(tb, "Error", "an error", "nil")
}
}
// ErrorIs asserts that err matches target using errors.Is.
func ErrorIs(tb testing.TB, err, target error) {
tb.Helper()
if !errors.Is(err, target) {
errStr := nilTypeName
if err != nil {
errStr = err.Error()
}
targetStr := nilTypeName
if target != nil {
targetStr = target.Error()
}
fail(tb, "ErrorIs", targetStr, errStr)
}
}
// ErrorContains asserts that err is non-nil and its message contains the given substring.
func ErrorContains(tb testing.TB, err error, substring string) {
tb.Helper()
wantMsg := "error containing " + fmt.Sprintf("%q", substring)
if err == nil {
fail(tb, "ErrorContains", wantMsg, "nil")
return
}
if !strings.Contains(err.Error(), substring) {
fail(tb, "ErrorContains", wantMsg, err.Error())
}
}
// ErrorAs asserts that err matches the target type using errors.As.
// The target must be a non-nil pointer to either an error interface or a concrete type
// that implements error.
//
// var pathErr *os.PathError
// testastic.ErrorAs(t, err, &pathErr)
func ErrorAs(tb testing.TB, err error, target any) {
tb.Helper()
if !errors.As(err, target) {
errStr := nilTypeName
if err != nil {
errStr = err.Error()
}
fail(tb, "ErrorAs", fmt.Sprintf("error assignable to %T", target), errStr)
}
}
// failCmp reports a comparison assertion failure.
func failCmp(tb testing.TB, name, expectOp, actualOp, a, b string) {
tb.Helper()
tb.Errorf(
"testastic: assertion failed\n\n %s\n expected: %s %s %s\n actual: %s %s %s",
name, red(a), expectOp, red(b), green(a), actualOp, green(b),
)
}
// Greater asserts that a > b using [cmp.Ordered] comparison.
func Greater[T cmp.Ordered](tb testing.TB, a, b T) {
tb.Helper()
if a <= b {
failCmp(tb, "Greater", ">", "<=", formatVal(a), formatVal(b))
}
}
// GreaterOrEqual asserts that a >= b using [cmp.Ordered] comparison.
func GreaterOrEqual[T cmp.Ordered](tb testing.TB, a, b T) {
tb.Helper()
if a < b {
failCmp(tb, "GreaterOrEqual", ">=", "<", formatVal(a), formatVal(b))
}
}
// Less asserts that a < b using [cmp.Ordered] comparison.
func Less[T cmp.Ordered](tb testing.TB, a, b T) {
tb.Helper()
if a >= b {
failCmp(tb, "Less", "<", ">=", formatVal(a), formatVal(b))
}
}
// LessOrEqual asserts that a <= b using [cmp.Ordered] comparison.
func LessOrEqual[T cmp.Ordered](tb testing.TB, a, b T) {
tb.Helper()
if a > b {
failCmp(tb, "LessOrEqual", "<=", ">", formatVal(a), formatVal(b))
}
}
// Between asserts that minVal <= value <= maxVal.
func Between[T cmp.Ordered](tb testing.TB, value, minVal, maxVal T) {
tb.Helper()
if value < minVal || value > maxVal {
expected := formatVal(minVal) + " <= value <= " + formatVal(maxVal)
fail(tb, "Between", expected, formatVal(value))
}
}
// stringInputValue converts supported string assertion inputs to a string.
func stringInputValue(value any) (string, bool) {
switch v := value.(type) {
case string:
return v, true
case []byte:
return string(v), true
case fmt.Stringer:
return v.String(), true
default:
return "", false
}
}
// failStrType reports an unsupported input type for string assertions.
func failStrType(tb testing.TB, name string, value any) {
tb.Helper()
tb.Errorf(
"testastic: assertion failed\n\n %s\n error: unsupported type %T (want string, []byte, or fmt.Stringer)",
name, value,
)
}
// failStr reports a string assertion failure.
func failStr(tb testing.TB, name, label, s, search, status string) {
tb.Helper()
tb.Errorf(
"testastic: assertion failed\n\n %s\n string: %s\n %s: %s (%s)",
name, green(formatVal(s)), label, red(formatVal(search)), status,
)
}
// Contains asserts that a string, byte slice, or Stringer contains the given substring.
// Reports a non-fatal error on failure, displaying the string and missing substring.
func Contains(tb testing.TB, value any, substring string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "Contains", value)
return
}
if !strings.Contains(s, substring) {
failStr(tb, "Contains", "substring", s, substring, "not found")
}
}
// NotContains asserts that a string, byte slice, or Stringer does not contain the given substring.
func NotContains(tb testing.TB, value any, substring string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "NotContains", value)
return
}
if strings.Contains(s, substring) {
failStr(tb, "NotContains", "substring", s, substring, "found")
}
}
// HasPrefix asserts that a string, byte slice, or Stringer starts with the given prefix.
func HasPrefix(tb testing.TB, value any, prefix string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "HasPrefix", value)
return
}
if !strings.HasPrefix(s, prefix) {
failStr(tb, "HasPrefix", "prefix", s, prefix, "not found")
}
}
// NotHasPrefix asserts that a string, byte slice, or Stringer does not start with the given prefix.
func NotHasPrefix(tb testing.TB, value any, prefix string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "NotHasPrefix", value)
return
}
if strings.HasPrefix(s, prefix) {
failStr(tb, "NotHasPrefix", "prefix", s, prefix, "found")
}
}
// HasSuffix asserts that a string, byte slice, or Stringer ends with the given suffix.
func HasSuffix(tb testing.TB, value any, suffix string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "HasSuffix", value)
return
}
if !strings.HasSuffix(s, suffix) {
failStr(tb, "HasSuffix", "suffix", s, suffix, "not found")
}
}
// NotHasSuffix asserts that a string, byte slice, or Stringer does not end with the given suffix.
func NotHasSuffix(tb testing.TB, value any, suffix string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "NotHasSuffix", value)
return
}
if strings.HasSuffix(s, suffix) {
failStr(tb, "NotHasSuffix", "suffix", s, suffix, "found")
}
}
// Matches asserts that a string, byte slice, or Stringer matches the given regular expression pattern.
func Matches(tb testing.TB, value any, pattern string) {
tb.Helper()
s, ok := stringInputValue(value)
if !ok {
failStrType(tb, "Matches", value)
return
}
re, err := regexp.Compile(pattern)
if err != nil {
tb.Errorf(
"testastic: assertion failed\n\n Matches\n error: invalid pattern %q: %v",
pattern, err,
)
return
}
if !re.MatchString(s) {
failStr(tb, "Matches", "pattern", s, pattern, "no match")
}
}
// StringEmpty asserts that s is an empty string ("").
func StringEmpty(tb testing.TB, s string) {
tb.Helper()
if s != "" {
fail(tb, "StringEmpty", `""`, formatVal(s))
}
}
// StringNotEmpty asserts that s is not an empty string ("").
func StringNotEmpty(tb testing.TB, s string) {
tb.Helper()
if s == "" {
fail(tb, "StringNotEmpty", "non-empty string", `""`)
}
}
// Panics asserts that the function panics when called.
//
// testastic.Panics(t, func() {
// panic("something went wrong")
// })
func Panics(tb testing.TB, fn func()) {
tb.Helper()
if !didPanic(fn) {
tb.Errorf("testastic: assertion failed\n\n Panics\n expected: %s\n actual: %s",
red("function to panic"), green("no panic"))
}
}
// NotPanics asserts that the function does not panic when called.
//
// testastic.NotPanics(t, func() {
// doSomethingSafe()
// })
func NotPanics(tb testing.TB, fn func()) {
tb.Helper()
if didPanic(fn) {
tb.Errorf("testastic: assertion failed\n\n NotPanics\n expected: %s\n actual: %s",
red("no panic"), green("function panicked"))
}
}
// didPanic returns true if the function panics when called.
func didPanic(fn func()) bool {
panicked := true
func() {
defer func() {
_ = recover()
}()
fn()
panicked = false
}()
return panicked
}
// isNil checks if a value is nil, handling interface nil correctly.
func isNil(value any) bool {
if value == nil {
return true
}
v := reflect.ValueOf(value)
//nolint:exhaustive // Only nil-able types need checking.
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
default:
return false
}
}
// formatVal formats a value for display in error messages.
func formatVal(v any) string {
if v == nil {
return "nil"
}
switch val := v.(type) {
case string:
return fmt.Sprintf("%q", val)
default:
return fmt.Sprintf("%v", val)
}
}