generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis.go
84 lines (70 loc) · 1.41 KB
/
is.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
package is
import (
"errors"
)
type t interface {
Helper()
Log(args ...any)
Logf(format string, args ...any)
FailNow()
}
func Nil[T any](t t, v *T, messages ...any) {
t.Helper()
if v != nil {
t.Logf(`Expected nil, but got "%v" (type %T)`, *v, v)
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}
func NotNil[T any](t t, v *T, messages ...any) {
t.Helper()
if v == nil {
t.Logf(`Expected not nil, but got nil (type %T)`, v)
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}
func Error(t t, expected, actual error, messages ...any) {
t.Helper()
if !errors.Is(actual, expected) {
t.Logf(`Expected "%v" (type %T), but got "%v" (type %T)`, expected, expected, actual, actual)
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}
func NotError(t t, err error, messages ...any) {
t.Helper()
if err != nil {
t.Logf(`Expected nil error, but got "%v" (type %T)`, err, err)
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}
func Equal[T comparable](t t, expected, actual T, messages ...any) {
t.Helper()
if expected != actual {
t.Logf(`Expected "%v", but got "%v" (type %T)`, expected, actual, actual)
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}
func True(t t, expression bool, messages ...any) {
t.Helper()
if !expression {
t.Log("Not true")
if len(messages) > 0 {
t.Log(messages...)
}
t.FailNow()
}
}