-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
629 lines (586 loc) · 17.4 KB
/
assert.go
File metadata and controls
629 lines (586 loc) · 17.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// SPDX-License-Identifier: EUPL-1.2
// AX-shaped assertions for the Core test framework.
//
// The Assert* family records a non-fatal failure; the Require* family
// records and stops the test. Both wrap testing.TB so helpers compose
// across Test, Benchmark, and Fuzz contexts.
//
// # Output format
//
// Default is the AX one-line key=value shape — minimalist, grep-able,
// AI-readable:
//
// fs_test.go:144: AssertEqual want="hello" got="world"
// fs_test.go:147: AssertNoError got=open foo: no such file or directory
//
// Set core.AssertVerbose = true (e.g. in TestMain or a setup hook) to
// switch to the multi-line "standard output" format more familiar from
// testify and human-driven test triage:
//
// fs_test.go:144: AssertEqual failed
// want: "hello"
// got: "world"
//
// The flag is global to the test process. Pass = silent in either mode
// (Go test default).
//
// Usage
//
// core.AssertEqual(t, "expected", actual)
// core.AssertTrue(t, result.OK)
// core.AssertNoError(t, err)
// core.AssertContains(t, []string{"a", "b"}, "a")
// core.RequireNoError(t, c.Fs().Write(p, "data").Error())
//
// Optional trailing string args are joined into a context suffix on
// the failure line.
package core
// AssertVerbose toggles the failure-message format. When false (default)
// each Assert*/Require* emits the AX one-line shape; when true each
// emits a multi-line testify-style block. Flip it in TestMain when a
// human is reading failures and you want the readable format.
//
// func TestMain(m *testing.M) {
// core.AssertVerbose = true
// m.Run()
// }
var AssertVerbose = false
// assertFail centralises every Assert*/Require* failure path. The
// caller passes a kv-pair list of context (e.g. "want", expected,
// "got", actual). When AssertVerbose is false the helper renders one
// AX line; when true a multi-line block.
func assertFail(t TB, fatal bool, name string, msg []string, kvs ...any) {
t.Helper()
suffix := assertMsg(msg)
if AssertVerbose {
body := name + " failed\n"
for i := 0; i+1 < len(kvs); i += 2 {
body += Sprintf(" %s: %#v\n", kvs[i], kvs[i+1])
}
if suffix != "" {
body += " msg:" + suffix
}
if fatal {
t.Fatal(body)
} else {
t.Error(body)
}
return
}
parts := make([]string, 0, len(kvs)/2)
for i := 0; i+1 < len(kvs); i += 2 {
parts = append(parts, Sprintf("%s=%#v", kvs[i], kvs[i+1]))
}
line := name
if len(parts) > 0 {
line += " " + Join(" ", parts...)
}
line += suffix
if fatal {
t.Fatal(line)
} else {
t.Error(line)
}
}
// AssertEqual fails the test if want and got are not deeply equal.
//
// core.AssertEqual(t, "expected", result.Value)
// core.AssertEqual(t, 42, result.Count)
func AssertEqual(t TB, want, got any, msg ...string) {
t.Helper()
if !DeepEqual(want, got) {
assertFail(t, false, "AssertEqual", msg, "want", want, "got", got)
}
}
// AssertNotEqual fails the test if want and got are deeply equal.
//
// core.AssertNotEqual(t, oldValue, newValue)
func AssertNotEqual(t TB, want, got any, msg ...string) {
t.Helper()
if DeepEqual(want, got) {
assertFail(t, false, "AssertNotEqual", msg, "want!=", want, "got", got)
}
}
// AssertTrue fails the test if condition is false.
//
// core.AssertTrue(t, result.OK)
// core.AssertTrue(t, len(items) > 0, "items must not be empty")
func AssertTrue(t TB, condition bool, msg ...string) {
t.Helper()
if !condition {
assertFail(t, false, "AssertTrue", msg, "want", true, "got", false)
}
}
// AssertFalse fails the test if condition is true.
//
// core.AssertFalse(t, c.Fs().Exists(deletedPath))
func AssertFalse(t TB, condition bool, msg ...string) {
t.Helper()
if condition {
assertFail(t, false, "AssertFalse", msg, "want", false, "got", true)
}
}
// AssertNil fails the test if v is non-nil. Handles typed-nil interface
// values (e.g. a (*Foo)(nil) inside an any).
//
// core.AssertNil(t, returnedPointer)
func AssertNil(t TB, v any, msg ...string) {
t.Helper()
if !assertIsNil(v) {
assertFail(t, false, "AssertNil", msg, "want", nil, "got", v)
}
}
// AssertNotNil fails the test if v is nil.
//
// core.AssertNotNil(t, c.Fs())
func AssertNotNil(t TB, v any, msg ...string) {
t.Helper()
if assertIsNil(v) {
assertFail(t, false, "AssertNotNil", msg, "want", "non-nil", "got", nil)
}
}
// AssertNoError fails the test if err is non-nil.
//
// core.AssertNoError(t, c.Fs().Write(p, "data").Error())
func AssertNoError(t TB, err error, msg ...string) {
t.Helper()
if err != nil {
assertFail(t, false, "AssertNoError", msg, "got", err)
}
}
// AssertError fails the test if err is nil. Optional substring matches
// against err.Error() for tighter assertions.
//
// core.AssertError(t, parseFails())
// core.AssertError(t, parseFails(), "invalid syntax")
func AssertError(t TB, err error, msg ...string) {
t.Helper()
if err == nil {
assertFail(t, false, "AssertError", msg, "want", "non-nil", "got", nil)
return
}
for _, want := range msg {
if !Contains(err.Error(), want) {
assertFail(t, false, "AssertError", nil, "want-substring", want, "got", err.Error())
return
}
}
}
// AssertContains fails the test if needle is not present in haystack.
// Supports strings (substring match), slices/arrays (deep-equal element
// membership), and maps (key membership).
//
// core.AssertContains(t, "hello world", "world")
// core.AssertContains(t, []string{"a", "b"}, "a")
// core.AssertContains(t, map[string]int{"x": 1}, "x")
func AssertContains(t TB, haystack, needle any, msg ...string) {
t.Helper()
if !assertContains(haystack, needle) {
assertFail(t, false, "AssertContains", msg, "haystack", haystack, "needle", needle)
}
}
// AssertNotContains fails the test if needle IS present in haystack.
func AssertNotContains(t TB, haystack, needle any, msg ...string) {
t.Helper()
if assertContains(haystack, needle) {
assertFail(t, false, "AssertNotContains", msg, "haystack", haystack, "needle (found)", needle)
}
}
// AssertLen fails the test if the length of v does not equal want.
// Works on strings, slices, arrays, maps, and channels.
//
// core.AssertLen(t, items, 3)
func AssertLen(t TB, v any, want int, msg ...string) {
t.Helper()
rv := ValueOf(v)
switch rv.Kind() {
case KindString, KindSlice, KindArray, KindMap, KindChan:
if rv.Len() != want {
assertFail(t, false, "AssertLen", msg, "want", want, "got", rv.Len())
}
default:
assertFail(t, false, "AssertLen", msg, "unsupported kind", rv.Kind().String())
}
}
// AssertEmpty fails the test if v is not empty. Treats zero-length
// strings/slices/arrays/maps/channels and nil as empty.
//
// core.AssertEmpty(t, results)
func AssertEmpty(t TB, v any, msg ...string) {
t.Helper()
if !assertIsEmpty(v) {
assertFail(t, false, "AssertEmpty", msg, "want", "empty", "got", v)
}
}
// AssertNotEmpty fails the test if v is empty (see AssertEmpty).
//
// core.AssertNotEmpty(t, response.Body)
func AssertNotEmpty(t TB, v any, msg ...string) {
t.Helper()
if assertIsEmpty(v) {
assertFail(t, false, "AssertNotEmpty", msg, "want", "non-empty", "got", v)
}
}
// AssertGreater fails the test if got is not strictly greater than want.
// Works on numeric kinds (int, uint, float) and strings.
//
// core.AssertGreater(t, count, 0)
func AssertGreater(t TB, got, want any, msg ...string) {
t.Helper()
cmp, ok := assertCompare(got, want)
if !ok {
assertFail(t, false, "AssertGreater", msg, "incomparable got", got, "want", want)
return
}
if cmp <= 0 {
assertFail(t, false, "AssertGreater", msg, "got", got, "want>", want)
}
}
// AssertGreaterOrEqual fails the test if got is less than want.
//
// core.AssertGreaterOrEqual(t, elapsed, minDuration)
func AssertGreaterOrEqual(t TB, got, want any, msg ...string) {
t.Helper()
cmp, ok := assertCompare(got, want)
if !ok {
assertFail(t, false, "AssertGreaterOrEqual", msg, "incomparable got", got, "want", want)
return
}
if cmp < 0 {
assertFail(t, false, "AssertGreaterOrEqual", msg, "got", got, "want>=", want)
}
}
// AssertLess fails the test if got is not strictly less than want.
//
// core.AssertLess(t, errorCount, limit)
func AssertLess(t TB, got, want any, msg ...string) {
t.Helper()
cmp, ok := assertCompare(got, want)
if !ok {
assertFail(t, false, "AssertLess", msg, "incomparable got", got, "want", want)
return
}
if cmp >= 0 {
assertFail(t, false, "AssertLess", msg, "got", got, "want<", want)
}
}
// AssertLessOrEqual fails the test if got is greater than want.
func AssertLessOrEqual(t TB, got, want any, msg ...string) {
t.Helper()
cmp, ok := assertCompare(got, want)
if !ok {
assertFail(t, false, "AssertLessOrEqual", msg, "incomparable got", got, "want", want)
return
}
if cmp > 0 {
assertFail(t, false, "AssertLessOrEqual", msg, "got", got, "want<=", want)
}
}
// AssertPanics fails the test if calling fn does not panic.
//
// core.AssertPanics(t, func() { mustParse("garbage") })
func AssertPanics(t TB, fn func(), msg ...string) {
t.Helper()
defer func() {
if r := recover(); r == nil {
assertFail(t, false, "AssertPanics", msg, "want", "panic", "got", "normal-return")
}
}()
fn()
}
// AssertNotPanics fails the test if calling fn panics.
//
// core.AssertNotPanics(t, func() { safeDivide(10, 2) })
func AssertNotPanics(t TB, fn func(), msg ...string) {
t.Helper()
defer func() {
if r := recover(); r != nil {
assertFail(t, false, "AssertNotPanics", msg, "want", "normal-return", "got panic", r)
}
}()
fn()
}
// AssertPanicsWithError fails the test if fn does not panic, or panics
// with a value whose error string does not contain wantSubstr. Argument
// order matches testify's PanicsWithError(t, errString, fn).
//
// core.AssertPanicsWithError(t, "empty input", func() { mustParse("") })
func AssertPanicsWithError(t TB, wantSubstr string, fn func(), msg ...string) {
t.Helper()
defer func() {
r := recover()
if r == nil {
assertFail(t, false, "AssertPanicsWithError", msg, "want", "panic", "got", "normal-return")
return
}
var got string
if err, ok := r.(error); ok {
got = err.Error()
} else {
got = Sprintf("%v", r)
}
if !Contains(got, wantSubstr) {
assertFail(t, false, "AssertPanicsWithError", msg, "want-substring", wantSubstr, "got", got)
}
}()
fn()
}
// AssertErrorIs fails the test if err does not wrap target (Is).
//
// core.AssertErrorIs(t, err, fs.ErrNotExist)
func AssertErrorIs(t TB, err, target error, msg ...string) {
t.Helper()
if !Is(err, target) {
assertFail(t, false, "AssertErrorIs", msg, "want-target", target, "got", err)
}
}
// AssertInDelta fails the test if |got - want| > delta. Use for float
// comparisons where exact equality is not appropriate.
//
// core.AssertInDelta(t, expected, actual, 0.0001)
func AssertInDelta(t TB, want, got, delta float64, msg ...string) {
t.Helper()
if IsNaN(want) || IsNaN(got) {
assertFail(t, false, "AssertInDelta", msg, "NaN involved want", want, "got", got)
return
}
diff := Abs(want - got)
if diff > delta {
assertFail(t, false, "AssertInDelta", msg, "want", want, "got", got, "delta", delta, "actual-diff", diff)
}
}
// AssertSame fails the test if want and got are not the same pointer.
//
// core.AssertSame(t, c.Fs(), c.Fs()) // singleton check
func AssertSame(t TB, want, got any, msg ...string) {
t.Helper()
wv := ValueOf(want)
gv := ValueOf(got)
if wv.Kind() != KindPointer || gv.Kind() != KindPointer {
assertFail(t, false, "AssertSame", msg, "both args must be pointers, want", wv.Kind().String(), "got", gv.Kind().String())
return
}
if wv.Pointer() != gv.Pointer() {
assertFail(t, false, "AssertSame", msg, "want", Sprintf("%p", want), "got", Sprintf("%p", got))
}
}
// AssertElementsMatch fails the test if want and got are not slices/arrays
// containing the same elements regardless of order. Uses deep equality
// per element.
//
// core.AssertElementsMatch(t, []int{1, 2, 3}, []int{3, 1, 2})
func AssertElementsMatch(t TB, want, got any, msg ...string) {
t.Helper()
wv := ValueOf(want)
gv := ValueOf(got)
if (wv.Kind() != KindSlice && wv.Kind() != KindArray) ||
(gv.Kind() != KindSlice && gv.Kind() != KindArray) {
assertFail(t, false, "AssertElementsMatch", msg, "both args must be slices/arrays, want", wv.Kind().String(), "got", gv.Kind().String())
return
}
if wv.Len() != gv.Len() {
assertFail(t, false, "AssertElementsMatch", msg, "len-mismatch want", wv.Len(), "got", gv.Len())
return
}
matched := make([]bool, gv.Len())
for i := 0; i < wv.Len(); i++ {
w := wv.Index(i).Interface()
found := false
for j := 0; j < gv.Len(); j++ {
if matched[j] {
continue
}
if DeepEqual(w, gv.Index(j).Interface()) {
matched[j] = true
found = true
break
}
}
if !found {
assertFail(t, false, "AssertElementsMatch", msg, "missing element", w, "from got", got)
return
}
}
}
// RequireNoError fails the test AND stops it if err is non-nil. Use when
// the rest of the test depends on the operation succeeding.
//
// core.RequireNoError(t, c.Fs().Write(p, "data").Error())
func RequireNoError(t TB, err error, msg ...string) {
t.Helper()
if err != nil {
assertFail(t, true, "RequireNoError", msg, "got", err)
}
}
// RequireTrue fails the test AND stops it if condition is false. Use to
// guard test invariants where continuing past a false condition is
// meaningless.
//
// core.RequireTrue(t, scanner.Scan(), "scan precondition")
func RequireTrue(t TB, condition bool, msg ...string) {
t.Helper()
if !condition {
assertFail(t, true, "RequireTrue", msg, "want", true, "got", false)
}
}
// RequireNotEmpty fails the test AND stops it if v is empty. Use to
// guard test invariants where downstream assertions presume non-empty
// fixture data.
//
// core.RequireNotEmpty(t, fixturePath)
func RequireNotEmpty(t TB, v any, msg ...string) {
t.Helper()
if assertIsEmpty(v) {
assertFail(t, true, "RequireNotEmpty", msg, "want", "non-empty", "got", v)
}
}
// --- internal helpers ---
// assertMsg returns " — <joined msg>" or "" depending on optional context.
func assertMsg(msg []string) string {
if len(msg) == 0 {
return ""
}
return " — " + Join(" ", msg...)
}
// assertContains is the internal helper for AssertContains.
func assertContains(haystack, needle any) bool {
hv := ValueOf(haystack)
switch hv.Kind() {
case KindString:
nv := ValueOf(needle)
if nv.Kind() != KindString {
return false
}
return Contains(hv.String(), nv.String())
case KindSlice, KindArray:
for i := 0; i < hv.Len(); i++ {
if DeepEqual(hv.Index(i).Interface(), needle) {
return true
}
}
case KindMap:
for _, k := range hv.MapKeys() {
if DeepEqual(k.Interface(), needle) {
return true
}
}
}
return false
}
// assertIsNil reports whether v is a nil value, including typed-nil
// interface values like a (*Foo)(nil) inside an any.
func assertIsNil(v any) bool {
if v == nil {
return true
}
rv := ValueOf(v)
switch rv.Kind() {
case KindChan, KindFunc, KindInterface, KindMap, KindPointer, KindSlice:
return rv.IsNil()
}
return false
}
// assertIsEmpty reports whether v is empty for AssertEmpty/AssertNotEmpty.
// Strings/slices/arrays/maps/channels with len==0 are empty; nil is empty;
// other types use their zero value via reflect.
func assertIsEmpty(v any) bool {
if v == nil {
return true
}
rv := ValueOf(v)
switch rv.Kind() {
case KindString, KindSlice, KindArray, KindMap, KindChan:
return rv.Len() == 0
case KindPointer, KindInterface:
if rv.IsNil() {
return true
}
return assertIsEmpty(rv.Elem().Interface())
}
zero := Zero(rv.Type()).Interface()
return DeepEqual(v, zero)
}
// assertCompare returns -1, 0, +1 if a is less, equal, or greater than b.
// The boolean is false when the values are not comparable as a numeric or
// string pair.
func assertCompare(a, b any) (int, bool) {
av := ValueOf(a)
bv := ValueOf(b)
switch av.Kind() {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64:
switch bv.Kind() {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64:
return assertCmpInt64(av.Int(), bv.Int()), true
case KindUint, KindUint8, KindUint16, KindUint32, KindUint64:
ai := av.Int()
bi := bv.Uint()
if ai < 0 {
return -1, true
}
return assertCmpUint64(uint64(ai), bi), true
case KindFloat32, KindFloat64:
return assertCmpFloat64(float64(av.Int()), bv.Float()), true
}
case KindUint, KindUint8, KindUint16, KindUint32, KindUint64:
switch bv.Kind() {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64:
bi := bv.Int()
if bi < 0 {
return 1, true
}
return assertCmpUint64(av.Uint(), uint64(bi)), true
case KindUint, KindUint8, KindUint16, KindUint32, KindUint64:
return assertCmpUint64(av.Uint(), bv.Uint()), true
case KindFloat32, KindFloat64:
return assertCmpFloat64(float64(av.Uint()), bv.Float()), true
}
case KindFloat32, KindFloat64:
switch bv.Kind() {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64:
return assertCmpFloat64(av.Float(), float64(bv.Int())), true
case KindUint, KindUint8, KindUint16, KindUint32, KindUint64:
return assertCmpFloat64(av.Float(), float64(bv.Uint())), true
case KindFloat32, KindFloat64:
return assertCmpFloat64(av.Float(), bv.Float()), true
}
case KindString:
if bv.Kind() == KindString {
a, b := av.String(), bv.String()
if a < b {
return -1, true
}
if a > b {
return 1, true
}
return 0, true
}
}
return 0, false
}
func assertCmpInt64(a, b int64) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
func assertCmpUint64(a, b uint64) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
func assertCmpFloat64(a, b float64) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}