Skip to content

Commit

Permalink
add NoError assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Jul 11, 2022
1 parent e162b21 commit e6048ab
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions assert/Asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,16 @@ func (a Asserter) ErrorIs(expected, actual error, msg ...any) {
UserMessage: msg,
})
}

func (a Asserter) NoError(err error, msg ...any) {
a.TB.Helper()
if err == nil {
return
}
a.Fn(fmterror.Message{
Method: "NoError",
Cause: "Non-nil error value is received.",
Values: []fmterror.Value{{Label: "error", Value: err}},
UserMessage: msg,
})
}
21 changes: 21 additions & 0 deletions assert/Asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,24 @@ func TestAsserter_ErrorIs(t *testing.T) {
})
}
}

func TestAsserter_NoError(t *testing.T) {
t.Run(`when nil passed, then it is accepted`, func(t *testing.T) {
var failed bool
subject := asserter(func(args ...interface{}) { failed = true })
subject.NoError(nil)
Equal(t, failed, false)
})
t.Run(`when non-nil error value is passed`, func(t *testing.T) {
var failed bool
var actualMsg []interface{}
subject := asserter(func(args ...interface{}) {
failed = true
actualMsg = args
})
expectedMsg := []interface{}{"foo", "bar", "baz"}
subject.NoError(errors.New("boom"), expectedMsg...)
Equal(t, failed, true)
AssertFailFnArgs(t, expectedMsg, actualMsg)
})
}
13 changes: 13 additions & 0 deletions assert/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,16 @@ func ExampleEventually_byCustomRetryStrategy() {
}
})
}

func ExampleAsserter_NoError() {
var tb testing.TB
asserter := assert.Should(tb)
asserter.NoError(nil) // passes
asserter.NoError(errors.New("boom")) // fails
}

func ExampleNoError() {
var tb testing.TB
assert.NoError(tb, nil) // passes
assert.NoError(tb, errors.New("boom")) // fails
}
5 changes: 5 additions & 0 deletions assert/pkgfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ func ErrorIs(tb testing.TB, expected, actual error, msg ...any) {
tb.Helper()
Must(tb).ErrorIs(expected, actual, msg...)
}

func NoError(tb testing.TB, err error, msg ...any) {
tb.Helper()
Must(tb).NoError(err, msg...)
}
15 changes: 15 additions & 0 deletions assert/pkgfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ func TestPublicFunctions(t *testing.T) {
assert.ErrorIs(tb, expected, actual)
},
},
// .ErrorIs
{
Desc: ".NoError - happy",
Failed: false,
Assert: func(tb testing.TB) {
assert.NoError(tb, nil)
},
},
{
Desc: ".NoError - rainy",
Failed: true,
Assert: func(tb testing.TB) {
assert.NoError(tb, errors.New("boom"))
},
},
} {
t.Run(tc.Desc, func(t *testing.T) {
stub := &testcase.StubTB{}
Expand Down

0 comments on commit e6048ab

Please sign in to comment.