-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
395 lines (298 loc) · 11.2 KB
/
assert.go
File metadata and controls
395 lines (298 loc) · 11.2 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
package assert
import (
"encoding/json"
"errors"
"regexp"
)
// Testing is an interface wrapper around *testing.T
type Testing interface {
Helper()
Errorf(format string, args ...any)
}
// Fail reports a failure message via t.Errorf.
func Fail(t Testing, message string) bool {
t.Helper()
t.Errorf("%s", message)
return false
}
// Failf reports a formatted failure message via t.Errorf.
func Failf(t Testing, format string, args ...any) bool {
t.Helper()
t.Errorf(format, args...)
return false
}
// True asserts that the specified value is true.
func True(t Testing, condition bool, messages ...string) bool {
t.Helper()
if !condition {
return Failf(t, "%sShould be true", message(messages))
}
return true
}
// False asserts that the specified value is false.
func False(t Testing, condition bool, messages ...string) bool {
t.Helper()
if condition {
return Failf(t, "%sShould be false", message(messages))
}
return true
}
// Same asserts that two pointers reference the same object.
//
// Both arguments must be pointer variables. Pointer variable equality is
// determined based on the equality of both type and value.
func Same[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if valid, ok := same[T](expected, actual); !ok {
return Failf(t, "%sShould be pointers\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
} else if !valid {
return Failf(t, "%sShould be same\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// NotSame asserts that two pointers do NOT reference the same object.
//
// Both arguments must be pointer variables. Pointer variable equality is
// determined based on the equality of both type and value.
func NotSame[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if valid, ok := same(expected, actual); !ok {
Failf(t, "%sShould be pointers\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
} else if valid {
return Failf(t, "%sShould not be same\n actual: %s", message(messages), print(actual))
}
return true
}
// Equal asserts that two objects are equal.
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
func Equal[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if !equal(actual, expected) {
return Failf(t, "%sShould be equal:\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// NotEqual asserts that the specified values are NOT equal.
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
func NotEqual[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if equal(actual, expected) {
return Failf(t, "%sShould not be equal\n actual: %s", message(messages), print(actual))
}
return true
}
// EqualDelta asserts that two numeric values difference is less than delta.
//
// Panics if delta is negative.
func EqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
t.Helper()
if !equalDelta(actual, expected, delta) {
return Failf(t, "%sShould be equal in delta:\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// NotEqualDelta asserts that two numeric values difference is greater than delta.
//
// Panics if delta is negative.
func NotEqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
t.Helper()
if equalDelta(actual, expected, delta) {
return Failf(t, "%sShould not be equal in delta:\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// Greater asserts that actual is greater than expected.
func Greater[T Numeric](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if actual <= expected {
return Failf(t, "%sShould be greater\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// GreaterOrEqual asserts that actual is greater than or equal to expected.
func GreaterOrEqual[T Numeric](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if actual < expected {
return Failf(t, "%sShould be greater or equal\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// Less asserts that actual is less than expected.
func Less[T Numeric](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if actual >= expected {
return Failf(t, "%sShould be less\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// LessOrEqual asserts that actual is less than or equal to expected.
func LessOrEqual[T Numeric](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if actual > expected {
return Failf(t, "%sShould be less or equal\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// Length asserts that object has given length.
func Length[S Iterable[any]](t Testing, object S, expected int, messages ...string) bool {
t.Helper()
if actual := length(object); actual != expected {
return Failf(t, "%sShould have element length\n object: %#v\n actual: %d\nexpected: %d", message(messages), object, actual, expected)
}
return true
}
// Empty asserts that object has zero length.
func Empty[S Iterable[any]](t Testing, object S, messages ...string) bool {
t.Helper()
if length(object) != 0 {
return Failf(t, "%sShould be empty\n object: %#v", message(messages), object)
}
return true
}
// NotEmpty asserts that object has non-zero length.
func NotEmpty[S Iterable[any]](t Testing, object S, messages ...string) bool {
t.Helper()
if length(object) == 0 {
return Failf(t, "%sShould not be empty\n object: %#v", message(messages), object)
}
return true
}
// Contains asserts that object contains given element.
//
// Works with strings, arrays, slices, maps values and channels.
func Contains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, ok := contains(object, element); !ok {
return Failf(t, "%sShould be iterable\n object: %#v", message(messages), object)
} else if !found {
return Failf(t, "%sShould contain element\n object: %#v\n element: %#v", message(messages), object, element)
}
return true
}
// NotContains asserts that object does NOT contain given element.
//
// Works with strings, arrays, slices, maps values and channels.
func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, ok := contains(object, element); !ok {
Failf(t, "%sShould be iterable\n object: %#v", message(messages), object)
} else if found {
return Failf(t, "%sShould not contain element\n object: %#v\n element: %#v", message(messages), object, element)
}
return true
}
// Error asserts that error is NOT nil.
//
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
func Error(t Testing, err error, messages ...string) bool {
t.Helper()
if isNilError(err) {
return Failf(t, "%sShould be error", message(messages))
}
return true
}
// NoError asserts that error is nil.
//
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
func NoError(t Testing, err error, messages ...string) bool {
t.Helper()
if !isNilError(err) {
return Failf(t, "%sShould not be error\n msg: %[2]v\n error: %#[2]v", message(messages), err)
}
return true
}
// ErrorIs asserts that error is unwrappable to given target.
func ErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if !errors.Is(err, target) {
return Failf(t, "%sShould be same error\n msg: %[2]v\n error: %#[2]v\n target: %#v", message(messages), err, target)
}
return true
}
// NotErrorIs asserts that error is NOT unwrappable to given target.
func NotErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if errors.Is(err, target) {
return Failf(t, "%sShould not be same error\n msg: %[2]v\n error: %#[2]v\n target: %#v", message(messages), err, target)
}
return true
}
// Matches asserts that a string matches the given regular expression.
func Matches(t Testing, actual, pattern string, messages ...string) bool {
t.Helper()
re, err := regexp.Compile(pattern)
if err != nil {
return Failf(t, "%sShould be valid regexp\n pattern: %s\n err: %v", message(messages), pattern, err)
}
if !re.MatchString(actual) {
return Failf(t, "%sShould match regexp\n actual: %s\n pattern: %s", message(messages), actual, pattern)
}
return true
}
// NotMatches asserts that a string does NOT match the given regular expression.
func NotMatches(t Testing, actual, pattern string, messages ...string) bool {
t.Helper()
re, err := regexp.Compile(pattern)
if err != nil {
return Failf(t, "%sShould be valid regexp\n pattern: %s\n err: %v", message(messages), pattern, err)
}
if re.MatchString(actual) {
return Failf(t, "%sShould not match regexp\n actual: %s\n pattern: %s", message(messages), actual, pattern)
}
return true
}
// EqualJSON asserts that JSON strings are equal.
func EqualJSON(t Testing, actual, expected string, messages ...string) bool {
t.Helper()
var actualJSON, expectedJSON any
if err := json.Unmarshal([]byte(actual), &actualJSON); err != nil {
return Failf(t, "%sShould be valid JSON\n actual: %s\n err: %v", message(messages), actual, err)
}
if err := json.Unmarshal([]byte(expected), &expectedJSON); err != nil {
return Failf(t, "%sShould be valid JSON\nexpected: %s\n err: %v", message(messages), expected, err)
}
return Equal(t, actualJSON, expectedJSON)
}
// JSON asserts that object can be marshaled to expected JSON string.
func JSON(t Testing, actual any, expected string, messages ...string) bool {
t.Helper()
s, err := json.Marshal(actual)
return NoError(t, err, messages...) && EqualJSON(t, string(s), expected, messages...)
}
// Panics asserts that fn panics.
//
// When expected is nil, only the presence of a panic is checked.
// When expected is an error, the panic value is validated with errors.Is.
// Otherwise, the panic value is validated with deep equality.
func Panics(t Testing, fn func(), expected any, messages ...string) bool {
t.Helper()
panicked, value := panics(fn)
if !panicked {
return Failf(t, "%sShould panic", message(messages))
}
if expected == nil {
return true
}
if target, ok := expected.(error); ok {
if err, ok := value.(error); ok {
return ErrorIs(t, err, target, messages...)
}
return Failf(t, "%sShould panic with error\n actual: %s\nexpected: %s", message(messages), print(value), print(expected))
}
if !equal(value, expected) {
return Failf(t, "%sShould panic with value\n actual: %s\nexpected: %s", message(messages), print(value), print(expected))
}
return true
}
// NotPanics asserts that fn does NOT panic.
func NotPanics(t Testing, fn func(), messages ...string) bool {
t.Helper()
if panicked, value := panics(fn); panicked {
return Failf(t, "%sShould not panic\n value: %s", message(messages), print(value))
}
return true
}