-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
72 lines (54 loc) · 1.34 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
// Assertion helpers
package assert
import (
"encoding/json"
"regexp"
"testing"
)
func Equal[T comparable](t testing.TB, actual T, expected T) {
t.Helper()
if actual != expected {
t.Fatalf("exp=%v; got=%v", expected, actual)
}
}
func EqualJSON(t testing.TB, obj interface{}, expected string) {
t.Helper()
jsonBytes, err := json.MarshalIndent(obj, "", " ")
if err != nil {
panic(err)
}
Equal(t, string(jsonBytes), expected)
}
func Ok(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("error: %v", err)
}
}
func Matches(t testing.TB, actual string, pattern string) {
t.Helper()
if !regexp.MustCompile(pattern).MatchString(actual) {
t.Fatalf("expected %s to match %s", pattern, actual)
}
}
// Deprecated: use fn with JSON uppercased
func EqualJson(t testing.TB, obj interface{}, expected string) {
t.Helper()
EqualJSON(t, obj, expected)
}
// Deprecated: use `Equal(t, expr, true)`
// replacing `Assert(t, attempts == 1)` with `Equal(t, attempts, 1)` allows for better error messages.
func Assert(t testing.TB, expr bool) {
t.Helper()
Equal(t, expr, true)
}
// Deprecated: use Equal()
func EqualString(t testing.TB, actual string, expected string) {
t.Helper()
Equal(t, actual, expected)
}
// Deprecated: use Equal()
func EqualInt(t testing.TB, actual int, expected int) {
t.Helper()
Equal(t, actual, expected)
}