-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
133 lines (113 loc) · 3.84 KB
/
assert.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
package assert
var (
// The failure format string for values not being equal. Formatted with `expected` then `got`.
EqualMessage = "value did not equal expectation.\nexpected: %v\n got: %v"
// The error format string for one or both pointers being nil. Formatted with `got` then `expected`.
DereferenceEqualErrMsg = "go-assert: could not dereference nil pointer\ngot %v, expected %v"
// The failure format string if the err is not nil. Formatted with `err`.
NilErrMessage = "expected no error, got error:\n%v"
// The failure format string for slices being different sizes. Formatted with `expected` then `got`.
SliceSizeMessage = "slices were different sizes.\nexpected len:%d\n got len:%d\n"
// The failure format string for slices not matching at some index. Formatted with the mismatched
// index, then `expected`, then `got`.
SliceMismatchMessage = "slices differed at index %d.\nexpected: %v\n got: %v"
)
// The interface that represents the subset of `testing.T` that this package
// requires. Passing in a `testing.T` satisfies this interface.
type TestingT interface {
Helper()
Fatal(...any)
Fatalf(string, ...any)
Errorf(string, ...any)
}
// Assert that the passed condition is true. If not, fatally fail with
// `message` and format `args` into it.
func Assert(t TestingT, condition bool, message string, args ...any) {
t.Helper()
if !condition {
t.Fatalf(message, args...)
}
}
// Assert that `got` equals `expected`. The types between compared
// arguments must be the same. Uses `assert.EqualMessage`.
func Equal[T comparable](t TestingT, expected T, got T) {
t.Helper()
EqualMsg(t, expected, got, EqualMessage)
}
// Assert that the value at `got` equals the value at `expected`. Will
// error if either pointer is nil. Uses `assert.DereferenceEqualErrMsg`
// and `assert.EqualMessage`.
func DereferenceEqual[T comparable](t TestingT, expected *T, got *T) {
t.Helper()
DereferenceEqualMsg(t, expected, got, DereferenceEqualErrMsg, EqualMessage)
}
// Assert that that `err` is nil. Uses `assert.NilErrMessage`.
func NilErr(t TestingT, err error) {
t.Helper()
NilErrMsg(t, err, NilErrMessage)
}
// Assert that slices `got` and `expected` are equal. Will produce a
// different message if the lengths are different or if any element
// mismatches. Uses `assert.SliceSizeMessage` and
// `assert.SliceMismatchMessage`.
func SliceEqual[T comparable](t TestingT, expected []T, got []T) {
t.Helper()
SliceEqualMsg(
t,
expected,
got,
SliceSizeMessage,
SliceMismatchMessage,
)
}
// Assert that `got` equals `expected`. The types between compared
// arguments must be the same. Uses `message`.
func EqualMsg[T comparable](t TestingT, expected T, got T, message string) {
t.Helper()
if got != expected {
t.Fatalf(message, expected, got)
}
}
// Assert that the value at `got` equals the value at `expected`. Will
// error if either pointer is nil. Uses `errMessage` and `mismatchMessage`.
func DereferenceEqualMsg[T comparable](
t TestingT,
expected *T,
got *T,
errMessage,
mismatchMessage string,
) {
t.Helper()
if got == nil || expected == nil {
t.Errorf(errMessage, expected, got)
} else {
EqualMsg(t, *expected, *got, mismatchMessage)
}
}
// Assert that that `err` is nil. Uses `message`.
func NilErrMsg(t TestingT, err error, message string) {
t.Helper()
if err != nil {
t.Fatalf(message, err)
}
}
// Assert that slices `got` and `expected` are equal. Will produce a
// different message if the lengths are different or if any element
// mismatches. Uses `sizeMessage` and `mismatchMessage`.
func SliceEqualMsg[T comparable](
t TestingT,
expected []T,
got []T,
sizeMessage, mismatchMessage string,
) {
t.Helper()
if len(got) != len(expected) {
t.Fatalf(sizeMessage, len(expected), len(got))
} else {
for i := range got {
if got[i] != expected[i] {
t.Fatalf(mismatchMessage, i, expected[i], got[i])
}
}
}
}