-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
141 lines (132 loc) · 2.79 KB
/
error.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
134
135
136
137
138
139
140
141
package assert
import (
"errors"
"fmt"
"strings"
"testing"
)
// ErrorStringer is a function that returns a string representation of an error.
//
// It can be customized to provide a better error message.
var ErrorStringer func(error) string = func(err error) string {
if err == nil {
return "<nil>"
}
return fmt.Sprintf("%q", err)
}
// Error asserts that err is not nil.
//
//nolint:thelper // It's called below.
func Error(tb testing.TB, err error, opts ...Option) bool {
ok := err != nil
if !ok {
tb.Helper()
Fail(
tb,
"error",
"no error",
opts...,
)
}
return ok
}
// NoError asserts that err is nil.
//
//nolint:thelper // It's called below.
func NoError(tb testing.TB, err error, opts ...Option) bool {
ok := err == nil
if !ok {
tb.Helper()
Fail(
tb,
"no_error",
"error: "+ErrorStringer(err),
opts...,
)
}
return ok
}
// ErrorIs asserts that [errors.Is] returns true.
//
//nolint:thelper // It's called below.
func ErrorIs(tb testing.TB, err, target error, opts ...Option) bool {
ok := errors.Is(err, target)
if !ok {
tb.Helper()
Fail(
tb,
"error_is",
fmt.Sprintf("no match:\nerr = %s\ntarget = %s", ErrorStringer(err), ErrorStringer(target)),
opts...,
)
}
return ok
}
// ErrorNotIs asserts that [errors.Is] returns false.
//
//nolint:thelper // It's called below.
func ErrorNotIs(tb testing.TB, err, target error, opts ...Option) bool {
ok := !errors.Is(err, target)
if !ok {
tb.Helper()
Fail(
tb,
"error_not_is",
fmt.Sprintf("match:\nerr = %s\ntarget = %s", ErrorStringer(err), ErrorStringer(target)),
opts...,
)
}
return ok
}
// ErrorAs asserts that [errors.As] returns true.
//
//nolint:thelper // It's called below.
func ErrorAs(tb testing.TB, err error, target any, opts ...Option) bool {
ok := errors.As(err, target)
if !ok {
tb.Helper()
Fail(
tb,
"error_as",
fmt.Sprintf("no match:\nerr = %s\ntarget = %T", ErrorStringer(err), target),
opts...,
)
}
return ok
}
// ErrorEqual asserts that the result of [error.Error] is equal to message.
func ErrorEqual(tb testing.TB, err error, message string, opts ...Option) bool {
tb.Helper()
ok := Error(tb, err, opts...)
if !ok {
return false
}
ok = err.Error() == message
if !ok {
Fail(
tb,
"error_equal",
fmt.Sprintf("not equal:\nerr = %s\nmessage = %q", ErrorStringer(err), message),
opts...,
)
}
return ok
}
// ErrorContains asserts that the result of [error.Error] contains substr.
func ErrorContains(tb testing.TB, err error, substr string, opts ...Option) bool {
tb.Helper()
ok := Error(tb, err, opts...)
if !ok {
return false
}
ok = strings.Contains(err.Error(), substr)
if !ok {
Fail(
tb,
"error_contains",
fmt.Sprintf("not contains:\nerr = %s\nsubstr = %q", ErrorStringer(err), substr),
opts...,
)
}
return ok
}