Skip to content

Commit e6048ab

Browse files
committed
add NoError assertion
1 parent e162b21 commit e6048ab

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

assert/Asserter.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,16 @@ func (a Asserter) ErrorIs(expected, actual error, msg ...any) {
723723
UserMessage: msg,
724724
})
725725
}
726+
727+
func (a Asserter) NoError(err error, msg ...any) {
728+
a.TB.Helper()
729+
if err == nil {
730+
return
731+
}
732+
a.Fn(fmterror.Message{
733+
Method: "NoError",
734+
Cause: "Non-nil error value is received.",
735+
Values: []fmterror.Value{{Label: "error", Value: err}},
736+
UserMessage: msg,
737+
})
738+
}

assert/Asserter_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,3 +1347,24 @@ func TestAsserter_ErrorIs(t *testing.T) {
13471347
})
13481348
}
13491349
}
1350+
1351+
func TestAsserter_NoError(t *testing.T) {
1352+
t.Run(`when nil passed, then it is accepted`, func(t *testing.T) {
1353+
var failed bool
1354+
subject := asserter(func(args ...interface{}) { failed = true })
1355+
subject.NoError(nil)
1356+
Equal(t, failed, false)
1357+
})
1358+
t.Run(`when non-nil error value is passed`, func(t *testing.T) {
1359+
var failed bool
1360+
var actualMsg []interface{}
1361+
subject := asserter(func(args ...interface{}) {
1362+
failed = true
1363+
actualMsg = args
1364+
})
1365+
expectedMsg := []interface{}{"foo", "bar", "baz"}
1366+
subject.NoError(errors.New("boom"), expectedMsg...)
1367+
Equal(t, failed, true)
1368+
AssertFailFnArgs(t, expectedMsg, actualMsg)
1369+
})
1370+
}

assert/example_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,16 @@ func ExampleEventually_byCustomRetryStrategy() {
500500
}
501501
})
502502
}
503+
504+
func ExampleAsserter_NoError() {
505+
var tb testing.TB
506+
asserter := assert.Should(tb)
507+
asserter.NoError(nil) // passes
508+
asserter.NoError(errors.New("boom")) // fails
509+
}
510+
511+
func ExampleNoError() {
512+
var tb testing.TB
513+
assert.NoError(tb, nil) // passes
514+
assert.NoError(tb, errors.New("boom")) // fails
515+
}

assert/pkgfunc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ func ErrorIs(tb testing.TB, expected, actual error, msg ...any) {
7373
tb.Helper()
7474
Must(tb).ErrorIs(expected, actual, msg...)
7575
}
76+
77+
func NoError(tb testing.TB, err error, msg ...any) {
78+
tb.Helper()
79+
Must(tb).NoError(err, msg...)
80+
}

assert/pkgfunc_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ func TestPublicFunctions(t *testing.T) {
245245
assert.ErrorIs(tb, expected, actual)
246246
},
247247
},
248+
// .ErrorIs
249+
{
250+
Desc: ".NoError - happy",
251+
Failed: false,
252+
Assert: func(tb testing.TB) {
253+
assert.NoError(tb, nil)
254+
},
255+
},
256+
{
257+
Desc: ".NoError - rainy",
258+
Failed: true,
259+
Assert: func(tb testing.TB) {
260+
assert.NoError(tb, errors.New("boom"))
261+
},
262+
},
248263
} {
249264
t.Run(tc.Desc, func(t *testing.T) {
250265
stub := &testcase.StubTB{}

0 commit comments

Comments
 (0)