Skip to content

Commit 7f48972

Browse files
Merge pull request #1129 from palsivertsen/not-error-as
Add assertion for NotErrorAs
2 parents 95d1f9c + f844b26 commit 7f48972

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed

assert/assertion_format.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertion_forward.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertions.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,24 @@ func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{
21492149
), msgAndArgs...)
21502150
}
21512151

2152+
// NotErrorAs asserts that none of the errors in err's chain matches target,
2153+
// but if so, sets target to that error value.
2154+
func NotErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool {
2155+
if h, ok := t.(tHelper); ok {
2156+
h.Helper()
2157+
}
2158+
if !errors.As(err, target) {
2159+
return true
2160+
}
2161+
2162+
chain := buildErrorChainString(err)
2163+
2164+
return Fail(t, fmt.Sprintf("Target error should not be in err chain:\n"+
2165+
"found: %q\n"+
2166+
"in chain: %s", target, chain,
2167+
), msgAndArgs...)
2168+
}
2169+
21522170
func buildErrorChainString(err error) string {
21532171
if err == nil {
21542172
return ""

assert/assertions_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,7 +3269,6 @@ func TestNotErrorIs(t *testing.T) {
32693269
}
32703270

32713271
func TestErrorAs(t *testing.T) {
3272-
mockT := new(testing.T)
32733272
tests := []struct {
32743273
err error
32753274
result bool
@@ -3282,9 +3281,38 @@ func TestErrorAs(t *testing.T) {
32823281
tt := tt
32833282
var target *customError
32843283
t.Run(fmt.Sprintf("ErrorAs(%#v,%#v)", tt.err, target), func(t *testing.T) {
3284+
mockT := new(testing.T)
32853285
res := ErrorAs(mockT, tt.err, &target)
32863286
if res != tt.result {
3287-
t.Errorf("ErrorAs(%#v,%#v) should return %t)", tt.err, target, tt.result)
3287+
t.Errorf("ErrorAs(%#v,%#v) should return %t", tt.err, target, tt.result)
3288+
}
3289+
if res == mockT.Failed() {
3290+
t.Errorf("The test result (%t) should be reflected in the testing.T type (%t)", res, !mockT.Failed())
3291+
}
3292+
})
3293+
}
3294+
}
3295+
3296+
func TestNotErrorAs(t *testing.T) {
3297+
tests := []struct {
3298+
err error
3299+
result bool
3300+
}{
3301+
{fmt.Errorf("wrap: %w", &customError{}), false},
3302+
{io.EOF, true},
3303+
{nil, true},
3304+
}
3305+
for _, tt := range tests {
3306+
tt := tt
3307+
var target *customError
3308+
t.Run(fmt.Sprintf("NotErrorAs(%#v,%#v)", tt.err, target), func(t *testing.T) {
3309+
mockT := new(testing.T)
3310+
res := NotErrorAs(mockT, tt.err, &target)
3311+
if res != tt.result {
3312+
t.Errorf("NotErrorAs(%#v,%#v) should not return %t", tt.err, target, tt.result)
3313+
}
3314+
if res == mockT.Failed() {
3315+
t.Errorf("The test result (%t) should be reflected in the testing.T type (%t)", res, !mockT.Failed())
32883316
}
32893317
})
32903318
}

require/require.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

require/require_forward.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)