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
|
package check
import (
"errors"
"fmt"
"reflect"
"strings"
"git.sr.ht/~eugenetriguba/checkmate"
"github.com/google/go-cmp/cmp"
)
type helperT interface {
Helper()
}
// Nil checks whether the value equals nil.
func Nil(t checkmate.TestingT, value any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected value to be nil, got %v", value}
}
isNil := false
val := reflect.ValueOf(value)
if val.Kind() == reflect.Ptr {
if val.IsNil() {
isNil = true
}
} else if value == nil {
isNil = true
}
return check(t, isNil, msgAndArgs...)
}
// NotNil checks whether the value does not equal nil.
func NotNil(t checkmate.TestingT, value any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected value to not be nil, got nil"}
}
return check(t, value != nil, msgAndArgs...)
}
// True checks whether the condition is true.
func True(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected condition to be true, got false"}
}
return check(t, condition, msgAndArgs...)
}
// False checks whether the condition is false.
func False(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected condition to be false, got true"}
}
return check(t, !condition, msgAndArgs...)
}
// ErrorIs checks whether the target error occurs within err's error tree.
func ErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected error %v to have error %v in its tree", err, target}
}
return check(t, errors.Is(err, target), msgAndArgs...)
}
// NotErrorIs checks whether the target error does not occur within
// the err's error tree.
func NotErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected error %v to not have error %v in its tree", err, target}
}
return check(t, !errors.Is(err, target), msgAndArgs...)
}
// ErrorContains checks whether the given err contains the errText
// in the err.Error() output.
func ErrorContains(t checkmate.TestingT, err error, errText string, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected err to contain %s, got %s", errText, err.Error()}
}
return check(t, strings.Contains(err.Error(), errText), msgAndArgs...)
}
// NotErrorContains checks whether the given err does not contain the errText
// in the err.Error() output.
func NotErrorContains(t checkmate.TestingT, err error, errText string, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected err to contain not %s, got that it does", errText}
}
return check(t, !strings.Contains(err.Error(), errText), msgAndArgs...)
}
// DeepEqual checks if two values are deeply equal. If they are not equal,
// it logs the differences.
func DeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
diff := cmp.Diff(expected, actual)
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"mismatch (-expected +actual):\n%s", diff}
}
return check(t, diff == "", msgAndArgs...)
}
// NotDeepEqual checks if two values are not deeply equal.
func NotDeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
diff := cmp.Diff(expected, actual)
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected %v to not equal %v, got that they're equal", actual, expected}
}
return check(t, diff != "", msgAndArgs...)
}
// Equal checks if two primitive values are equal.
func Equal(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected %v to equal %v", actual, expected}
}
return check(t, actual == expected, msgAndArgs...)
}
// NotEqual checks if two values are not equal. It fails the test if
// the values are equal.
func NotEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected %v to not equal %v", actual, expected}
}
return check(t, actual != expected, msgAndArgs...)
}
// Contains checks whether a container includes a specified item.
//
// Supported container types:
// - string: checks if the string contains the given substring (item must be a string).
// - slice or array: checks if the container contains an element equal to the item.
// - map: checks if the map has the given key (item must be the key type).
// - pointer to any of the above: dereferences and checks the underlying value.
func Contains(t checkmate.TestingT, container, item any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
contains, errMsg := containsCheck(container, item)
if errMsg != "" {
return check(t, false, "Contains: %s", errMsg)
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected %v to contain %v", container, item}
}
return check(t, contains, msgAndArgs...)
}
// NotContains checks whether a container does not include a specified item.
//
// Supported container types:
// - string: checks if the string does not contain the given substring (item must be a string).
// - slice or array: checks if the container does not contain an element equal to the item.
// - map: checks if the map does not have the given key (item must be the key type).
// - pointer to any of the above: dereferences and checks the underlying value.
func NotContains(t checkmate.TestingT, container, item any, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
contains, errMsg := containsCheck(container, item)
if errMsg != "" {
return check(t, false, "NotContains: %s", errMsg)
}
if len(msgAndArgs) == 0 {
msgAndArgs = []any{"expected %v to not contain %v", container, item}
}
return check(t, !contains, msgAndArgs...)
}
// containsCheck is a helper that checks whether a container includes an item.
// Returns (contains bool, errMsg string) where errMsg is non-empty on error.
func containsCheck(container, item any) (bool, string) {
val := reflect.ValueOf(container)
if val.Kind() == reflect.Pointer {
val = val.Elem()
}
kind := val.Kind()
switch kind {
case reflect.String:
substr, ok := item.(string)
if !ok {
return false, "item must be string when container is a string"
}
return strings.Contains(val.String(), substr), ""
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
if reflect.DeepEqual(val.Index(i).Interface(), item) {
return true, ""
}
}
return false, ""
case reflect.Map:
keyVal := reflect.ValueOf(item)
return val.MapIndex(keyVal).IsValid(), ""
default:
return false, fmt.Sprintf("unsupported container type %T", container)
}
}
// Check evaluates a boolean condition and if the condition is false,
// it will log out a message and mark the test as failed. However, it does
// not immediately stop execution, unlike the assert functions.
func check(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if !condition {
message := "check failed"
if len(msgAndArgs) > 0 {
switch msgAndArgs[0].(type) {
case string:
message = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
default:
t.Log("checkmate: check called with a non-string message, using default message")
}
}
t.Log(message)
t.Fail()
}
return condition
}
|