-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
107 lines (96 loc) · 2.84 KB
/
errors.go
File metadata and controls
107 lines (96 loc) · 2.84 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
package attest
import (
"errors"
"fmt"
"strings"
"testing"
)
func noErrorAssert(t testing.TB, mode failMode, err error, msgAndArgs ...any) {
t.Helper()
if err != nil {
msg := formatMsg("expected no error", fmt.Sprintf("received: %s", err.Error()))
report(t, mode, msg, msgAndArgs...)
}
}
// NoError asserts that err is nil.
func NoError(t testing.TB, err error, msgAndArgs ...any) {
t.Helper()
noErrorAssert(t, modeError, err, msgAndArgs...)
}
func isErrorAssert(t testing.TB, mode failMode, err error, msgAndArgs ...any) {
t.Helper()
if err == nil {
report(t, mode, "expected an error, but got nil", msgAndArgs...)
}
}
// IsError asserts that err is not nil.
func IsError(t testing.TB, err error, msgAndArgs ...any) {
t.Helper()
isErrorAssert(t, modeError, err, msgAndArgs...)
}
func errorIsAssert(t testing.TB, mode failMode, err, target error, msgAndArgs ...any) {
t.Helper()
if !errors.Is(err, target) {
var actual string
if err != nil {
actual = err.Error()
} else {
actual = "<nil>"
}
var targetStr string
if target != nil {
targetStr = target.Error()
} else {
targetStr = "<nil>"
}
msg := formatMsg("error does not match target",
fmt.Sprintf("actual: %s\ntarget: %s", actual, targetStr))
report(t, mode, msg, msgAndArgs...)
}
}
// ErrorIs asserts that errors.Is(err, target) is true.
func ErrorIs(t testing.TB, err, target error, msgAndArgs ...any) {
t.Helper()
errorIsAssert(t, modeError, err, target, msgAndArgs...)
}
func errorAsAssert[T any](t testing.TB, mode failMode, err error, target *T, msgAndArgs ...any) {
t.Helper()
if target == nil {
report(t, mode, "ErrorAs: target must be a non-nil pointer", msgAndArgs...)
return
}
if !errors.As(err, target) {
var actual string
if err != nil {
actual = err.Error()
} else {
actual = "<nil>"
}
var zero T
msg := formatMsg("error does not match target type",
fmt.Sprintf("actual: %s\ntarget type: %T", actual, zero))
report(t, mode, msg, msgAndArgs...)
}
}
// ErrorAs asserts that errors.As(err, target) is true.
func ErrorAs[T any](t testing.TB, err error, target *T, msgAndArgs ...any) {
t.Helper()
errorAsAssert(t, modeError, err, target, msgAndArgs...)
}
func errorContainsAssert(t testing.TB, mode failMode, err error, substring string, msgAndArgs ...any) {
t.Helper()
if err == nil {
report(t, mode, "expected an error, but got nil", msgAndArgs...)
return
}
if !strings.Contains(err.Error(), substring) {
msg := formatMsg("error does not contain expected substring",
fmt.Sprintf("error: %q\nsubstring: %q", err.Error(), substring))
report(t, mode, msg, msgAndArgs...)
}
}
// ErrorContains asserts that err is non-nil and its message contains substring.
func ErrorContains(t testing.TB, err error, substring string, msgAndArgs ...any) {
t.Helper()
errorContainsAssert(t, modeError, err, substring, msgAndArgs...)
}