Skip to content

Commit 4bbcdb5

Browse files
committed
feat: add IsError.
Signed-off-by: ghosind <ghosind@gmail.com>
1 parent a5620a2 commit 4bbcdb5

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

builtin.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,33 @@ func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) err
494494
return tryNotHasSuffixString(t, true, str, suffix, message...)
495495
}
496496

497+
// IsError tests whether the error matches the target or not. It'll set the result to fail if the
498+
// error does not match to the target error, and it doesn't stop the execution.
499+
//
500+
// err1 := errors.New("error 1")
501+
// err2 := errors.New("error 2")
502+
// assert.IsError(t, err1, err1) // success
503+
// assert.IsError(t, err1, err2) // fail
504+
// assert.IsError(t, errors.Join(err1, err2), err1) // success
505+
// assert.IsError(t, errors.Join(err1, err2), err2) // success
506+
func IsError(t *testing.T, err, target error, message ...any) error {
507+
return isError(t, false, err, target, message...)
508+
}
509+
510+
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
511+
// stop the execution if the error does not match to the target error.
512+
//
513+
// err1 := errors.New("error 1")
514+
// err2 := errors.New("error 2")
515+
// assert.IsErrorNow(t, errors.Join(err1, err2), err1) // success
516+
// assert.IsErrorNow(t, errors.Join(err1, err2), err2) // success
517+
// assert.IsErrorNow(t, err1, err1) // success
518+
// assert.IsErrorNow(t, err1, err2) // fail
519+
// // never runs
520+
func IsErrorNow(t *testing.T, err, target error, message ...any) error {
521+
return isError(t, true, err, target, message...)
522+
}
523+
497524
// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does
498525
// not contain the key, or the type of the key cannot assign to the type of the key of the map.
499526
//

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
defaultErrMessageGte string = "%v must greater than or equal to %v"
3535
defaultErrMessageLt string = "%v must less then %v"
3636
defaultErrMessageLte string = "%v must less then or equal to %v"
37+
defaultErrMessageIsError string = "expect err matches %v, got %v"
3738
)
3839

3940
var (

errors.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package assert
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
// IsError tests whether the error matches the target or not. It'll set the result to fail if the
10+
// error does not match to the target error, and it doesn't stop the execution.
11+
//
12+
// err1 := errors.New("error 1")
13+
// err2 := errors.New("error 2")
14+
// a := assert.New(t)
15+
// a.IsError(err1, err1) // success
16+
// a.IsError(err1, err2) // fail
17+
// a.IsError(errors.Join(err1, err2), err1) // success
18+
// a.IsError(errors.Join(err1, err2), err2) // success
19+
func (a *Assertion) IsError(err, target error, message ...any) error {
20+
return isError(a.T, false, err, target, message...)
21+
}
22+
23+
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
24+
// stop the execution if the error does not match to the target error.
25+
//
26+
// err1 := errors.New("error 1")
27+
// err2 := errors.New("error 2")
28+
// a := assert.New(t)
29+
// a.IsErrorNow(errors.Join(err1, err2), err1) // success
30+
// a.IsErrorNow(errors.Join(err1, err2), err2) // success
31+
// a.IsErrorNow(err1, err1) // success
32+
// a.IsErrorNow(err1, err2) // fail
33+
// // never runs
34+
func (a *Assertion) IsErrorNow(err, target error, message ...any) error {
35+
return isError(a.T, true, err, target, message...)
36+
}
37+
38+
// isError tests whether the error matches the target or not.
39+
func isError(t *testing.T, failedNow bool, err, target error, message ...any) error {
40+
t.Helper()
41+
42+
return test(
43+
t,
44+
func() bool { return errors.Is(err, target) },
45+
failedNow,
46+
fmt.Sprintf(defaultErrMessageIsError, target, err),
47+
message...,
48+
)
49+
}

errors_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package assert
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestIsError(t *testing.T) {
9+
a := New(t)
10+
mockA := New(new(testing.T))
11+
12+
err1 := errors.New("error 1")
13+
err2 := errors.New("error 2")
14+
err3 := errors.New("error 3")
15+
16+
testIsError(a, mockA, errors.New("test"), err1, false)
17+
testIsError(a, mockA, err1, err1, true)
18+
testIsError(a, mockA, err1, err2, false)
19+
testIsError(a, mockA, err1, err3, false)
20+
testIsError(a, mockA, errors.Join(err1, err2), err1, true)
21+
testIsError(a, mockA, errors.Join(err1, err2), err2, true)
22+
testIsError(a, mockA, errors.Join(err1, err2), err3, false)
23+
}
24+
25+
func testIsError(a, mockA *Assertion, err, target error, isError bool) {
26+
a.T.Helper()
27+
28+
testAssertionFunction(a, "IsError", func() error {
29+
return IsError(mockA.T, err, target)
30+
}, isError)
31+
testAssertionFunction(a, "Assertion.IsError", func() error {
32+
return mockA.IsError(err, target)
33+
}, isError)
34+
35+
testAssertionNowFunction(a, "IsErrorNow", func() {
36+
IsErrorNow(mockA.T, err, target)
37+
}, !isError)
38+
testAssertionNowFunction(a, "Assertion.IsErrorNow", func() {
39+
mockA.IsErrorNow(err, target)
40+
}, !isError)
41+
}

0 commit comments

Comments
 (0)