-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
99 lines (88 loc) · 1.85 KB
/
assert.go
File metadata and controls
99 lines (88 loc) · 1.85 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
// Package assert provides some basic test helpers.
//
// These are adapted from: https://github.com/benbjohnson/testing
package assert
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
// NoError fails the test if the err is not nil.
func NoError(tb testing.TB, err error) {
if err == nil {
return
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf(
"\033[31m%s:%d: unexpected error: %s\033[39m\n\n",
filepath.Base(file),
line,
err.Error(),
)
tb.FailNow()
}
// Equals fails the test if want is not equal to got.
func Equals(tb testing.TB, want, got any) {
if reflect.DeepEqual(want, got) {
return
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf(
"\033[31m%s:%d:\n\n\twant: %#v\n\n\tgot: %#v\033[39m\n\n",
filepath.Base(file),
line,
want,
got,
)
tb.FailNow()
}
func DiffEquals(tb testing.TB, want, got any, opts ...cmp.Option) {
diff := cmp.Diff(want, got, opts...)
if diff == "" {
return
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf(
"\033[31m%s:%d:\n\n%s\033[39m\n\n",
filepath.Base(file),
line,
diff,
)
tb.FailNow()
}
func IgnorePath(paths ...string) cmp.Option {
return cmp.FilterPath(
func(p cmp.Path) bool {
s1 := p.String()
s2 := p.GoString()
for _, path := range paths {
if path == s1 || path == s2 {
return true
}
}
return false
},
cmp.Ignore(),
)
}
// TimesEqual fails the test if the difference between want and got is not within maxDelta.
// For exact matches, supply a maxDelta of zero.
func TimesEqual(tb testing.TB, want, got time.Time, maxDelta time.Duration) {
d := want.Sub(got).Abs()
if d <= maxDelta {
return
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf(
"\033[31m%s:%d:\n\n\twant: %s\n\n\tgot: %s\033[39m\n\n",
filepath.Base(file),
line,
want.String(),
got.String(),
)
tb.FailNow()
}