Skip to content

Commit 3cce381

Browse files
committed
feat: add notIsError.
Signed-off-by: ghosind <ghosind@gmail.com>
1 parent 297f879 commit 3cce381

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

builtin.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,14 @@ func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) err
499499
//
500500
// err1 := errors.New("error 1")
501501
// err2 := errors.New("error 2")
502+
// err3 := errors.New("error 3")
502503
// assert.IsError(t, err1, err1) // success
503504
// assert.IsError(t, err1, err2) // fail
504505
// assert.IsError(t, errors.Join(err1, err2), err1) // success
505506
// 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...)
507+
// assert.IsError(t, errors.Join(err1, err2), err3) // fail
508+
func IsError(t *testing.T, err, expected error, message ...any) error {
509+
return isError(t, false, err, expected, message...)
508510
}
509511

510512
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
@@ -517,8 +519,37 @@ func IsError(t *testing.T, err, target error, message ...any) error {
517519
// assert.IsErrorNow(t, err1, err1) // success
518520
// assert.IsErrorNow(t, err1, err2) // fail
519521
// // never runs
520-
func IsErrorNow(t *testing.T, err, target error, message ...any) error {
521-
return isError(t, true, err, target, message...)
522+
func IsErrorNow(t *testing.T, err, expected error, message ...any) error {
523+
return isError(t, true, err, expected, message...)
524+
}
525+
526+
// NotIsError tests whether the error matches the target or not. It'll set the result to fail if
527+
// the error matches to the target error, and it doesn't stop the execution.
528+
//
529+
// err1 := errors.New("error 1")
530+
// err2 := errors.New("error 2")
531+
// err3 := errors.New("error 3")
532+
// assert.NotIsError(t, err1, err2) // success
533+
// assert.NotIsError(t, err1, err1) // fail
534+
// assert.NotIsError(t, errors.Join(err1, err2), err3) // success
535+
// assert.NotIsError(t, errors.Join(err1, err2), err1) // fail
536+
// assert.NotIsError(t, errors.Join(err1, err2), err2) // fail
537+
func NotIsError(t *testing.T, err, unexpected error, message ...any) error {
538+
return notIsError(t, false, err, unexpected, message...)
539+
}
540+
541+
// NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail
542+
// and stop the execution if the error matches to the target error.
543+
//
544+
// err1 := errors.New("error 1")
545+
// err2 := errors.New("error 2")
546+
// err3 := errors.new("error 3")
547+
// assert.NotIsErrorNow(t, errors.Join(err1, err2), err3) // success
548+
// assert.NotIsErrorNow(t, err1, err2) // fail
549+
// assert.NotIsErrorNow(t, err1, err1) // fail and terminate
550+
// // never runs
551+
func NotIsErrorNow(t *testing.T, err, unexpected error, message ...any) error {
552+
return notIsError(t, true, err, unexpected, message...)
522553
}
523554

524555
// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
defaultErrMessageLt string = "%v must less then %v"
3636
defaultErrMessageLte string = "%v must less then or equal to %v"
3737
defaultErrMessageIsError string = "expect err matches %v, got %v"
38+
defaultErrMessageNotIsError string = "expect err does not matches %v"
3839
)
3940

4041
var (

errors.go

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
// a.IsError(err1, err2) // fail
1717
// a.IsError(errors.Join(err1, err2), err1) // success
1818
// 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...)
19+
func (a *Assertion) IsError(err, expected error, message ...any) error {
20+
return isError(a.T, false, err, expected, message...)
2121
}
2222

2323
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
@@ -31,19 +31,61 @@ func (a *Assertion) IsError(err, target error, message ...any) error {
3131
// a.IsErrorNow(err1, err1) // success
3232
// a.IsErrorNow(err1, err2) // fail
3333
// // never runs
34-
func (a *Assertion) IsErrorNow(err, target error, message ...any) error {
35-
return isError(a.T, true, err, target, message...)
34+
func (a *Assertion) IsErrorNow(err, expected error, message ...any) error {
35+
return isError(a.T, true, err, expected, message...)
36+
}
37+
38+
// NotIsError tests whether the error matches the target or not. It'll set the result to fail if
39+
// the error matches to the target error, and it doesn't stop the execution.
40+
//
41+
// a := assert.New(t)
42+
// err1 := errors.New("error 1")
43+
// err2 := errors.New("error 2")
44+
// err3 := errors.New("error 3")
45+
// a.NotIsError(err1, err2) // success
46+
// a.NotIsError(err1, err1) // fail
47+
// a.NotIsError(errors.Join(err1, err2), err3) // success
48+
// a.NotIsError(errors.Join(err1, err2), err1) // fail
49+
// a.NotIsError(errors.Join(err1, err2), err2) // fail
50+
func (a *Assertion) NotIsError(err, unexpected error, message ...any) error {
51+
return notIsError(a.T, false, err, unexpected, message...)
52+
}
53+
54+
// NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail
55+
// and stop the execution if the error matches to the target error.
56+
//
57+
// a := assert.New(t)
58+
// err1 := errors.New("error 1")
59+
// err2 := errors.New("error 2")
60+
// err3 := errors.new("error 3")
61+
// a.NotIsErrorNow(errors.Join(err1, err2), err3) // success
62+
// a.NotIsErrorNow(err1, err2) // fail
63+
// a.NotIsErrorNow(err1, err1) // fail and terminate
64+
// // never runs
65+
func (a *Assertion) NotIsErrorNow(err, unexpected error, message ...any) error {
66+
return notIsError(a.T, true, err, unexpected, message...)
3667
}
3768

3869
// isError tests whether the error matches the target or not.
39-
func isError(t *testing.T, failedNow bool, err, target error, message ...any) error {
70+
func isError(t *testing.T, failedNow bool, err, expected error, message ...any) error {
4071
t.Helper()
4172

4273
return test(
4374
t,
44-
func() bool { return errors.Is(err, target) },
75+
func() bool { return errors.Is(err, expected) },
76+
failedNow,
77+
fmt.Sprintf(defaultErrMessageIsError, expected, err),
78+
message...,
79+
)
80+
}
81+
82+
// isError tests whether the error does not match the target error or not.
83+
func notIsError(t *testing.T, failedNow bool, err, unexpected error, message ...any) error {
84+
return test(
85+
t,
86+
func() bool { return !errors.Is(err, unexpected) },
4587
failedNow,
46-
fmt.Sprintf(defaultErrMessageIsError, target, err),
88+
fmt.Sprintf(defaultErrMessageNotIsError, unexpected),
4789
message...,
4890
)
4991
}

errors_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,24 @@ func testIsError(a, mockA *Assertion, err, target error, isError bool) {
2929
return mockA.IsError(err, target)
3030
}, isError)
3131

32+
testAssertionFunction(a, "NotIsError", func() error {
33+
return NotIsError(mockA.T, err, target)
34+
}, !isError)
35+
testAssertionFunction(a, "Assertion.NotIsError", func() error {
36+
return mockA.NotIsError(err, target)
37+
}, !isError)
38+
3239
testAssertionNowFunction(a, "IsErrorNow", func() {
3340
IsErrorNow(mockA.T, err, target)
3441
}, !isError)
3542
testAssertionNowFunction(a, "Assertion.IsErrorNow", func() {
3643
mockA.IsErrorNow(err, target)
3744
}, !isError)
45+
46+
testAssertionNowFunction(a, "NotIsErrorNow", func() {
47+
NotIsErrorNow(mockA.T, err, target)
48+
}, isError)
49+
testAssertionNowFunction(a, "Assertion.NotIsErrorNow", func() {
50+
mockA.NotIsErrorNow(err, target)
51+
}, isError)
3852
}

0 commit comments

Comments
 (0)