From e6048ab517a78b8ca043b6657453ffa787c1bbfe Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Mon, 11 Jul 2022 21:43:19 +0100 Subject: [PATCH] add NoError assertion --- assert/Asserter.go | 13 +++++++++++++ assert/Asserter_test.go | 21 +++++++++++++++++++++ assert/example_test.go | 13 +++++++++++++ assert/pkgfunc.go | 5 +++++ assert/pkgfunc_test.go | 15 +++++++++++++++ 5 files changed, 67 insertions(+) diff --git a/assert/Asserter.go b/assert/Asserter.go index 1e59357..f123af3 100644 --- a/assert/Asserter.go +++ b/assert/Asserter.go @@ -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, + }) +} diff --git a/assert/Asserter_test.go b/assert/Asserter_test.go index b39e028..b583ab3 100644 --- a/assert/Asserter_test.go +++ b/assert/Asserter_test.go @@ -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) + }) +} diff --git a/assert/example_test.go b/assert/example_test.go index 5461d3c..2da9cee 100644 --- a/assert/example_test.go +++ b/assert/example_test.go @@ -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 +} diff --git a/assert/pkgfunc.go b/assert/pkgfunc.go index 475755f..1cdd41f 100644 --- a/assert/pkgfunc.go +++ b/assert/pkgfunc.go @@ -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...) +} diff --git a/assert/pkgfunc_test.go b/assert/pkgfunc_test.go index 606a056..348ffd2 100644 --- a/assert/pkgfunc_test.go +++ b/assert/pkgfunc_test.go @@ -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{}