-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
91 lines (83 loc) · 3.1 KB
/
errors.go
File metadata and controls
91 lines (83 loc) · 3.1 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
package assert
import (
"errors"
"fmt"
"testing"
)
// IsError tests whether the error matches the target or not. It'll set the result to fail if the
// error does not match to the target error, and it doesn't stop the execution.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// a := assert.New(t)
// a.IsError(err1, err1) // success
// a.IsError(err1, err2) // fail
// a.IsError(errors.Join(err1, err2), err1) // success
// a.IsError(errors.Join(err1, err2), err2) // success
func (a *Assertion) IsError(err, expected error, message ...any) error {
return isError(a.T, false, err, expected, message...)
}
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
// stop the execution if the error does not match to the target error.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// a := assert.New(t)
// a.IsErrorNow(errors.Join(err1, err2), err1) // success
// a.IsErrorNow(errors.Join(err1, err2), err2) // success
// a.IsErrorNow(err1, err1) // success
// a.IsErrorNow(err1, err2) // fail
// // never runs
func (a *Assertion) IsErrorNow(err, expected error, message ...any) error {
return isError(a.T, true, err, expected, message...)
}
// NotIsError tests whether the error matches the target or not. It'll set the result to fail if
// the error matches to the target error, and it doesn't stop the execution.
//
// a := assert.New(t)
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// err3 := errors.New("error 3")
// a.NotIsError(err1, err2) // success
// a.NotIsError(err1, err1) // fail
// a.NotIsError(errors.Join(err1, err2), err3) // success
// a.NotIsError(errors.Join(err1, err2), err1) // fail
// a.NotIsError(errors.Join(err1, err2), err2) // fail
func (a *Assertion) NotIsError(err, unexpected error, message ...any) error {
return notIsError(a.T, false, err, unexpected, message...)
}
// NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail
// and stop the execution if the error matches to the target error.
//
// a := assert.New(t)
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// err3 := errors.new("error 3")
// a.NotIsErrorNow(errors.Join(err1, err2), err3) // success
// a.NotIsErrorNow(err1, err2) // fail
// a.NotIsErrorNow(err1, err1) // fail and terminate
// // never runs
func (a *Assertion) NotIsErrorNow(err, unexpected error, message ...any) error {
return notIsError(a.T, true, err, unexpected, message...)
}
// isError tests whether the error matches the target or not.
func isError(t *testing.T, failedNow bool, err, expected error, message ...any) error {
t.Helper()
return test(
t,
func() bool { return errors.Is(err, expected) },
failedNow,
fmt.Sprintf(defaultErrMessageIsError, expected, err),
message...,
)
}
// isError tests whether the error does not match the target error or not.
func notIsError(t *testing.T, failedNow bool, err, unexpected error, message ...any) error {
return test(
t,
func() bool { return !errors.Is(err, unexpected) },
failedNow,
fmt.Sprintf(defaultErrMessageNotIsError, unexpected),
message...,
)
}