From f2713fa991c914bf6b2beeea9b2938d022b738b4 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 27 Jan 2024 22:32:18 -0500 Subject: Add Check test helper --- assert.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'assert.go') diff --git a/assert.go b/assert.go index 565522c..4eeb7d2 100644 --- a/assert.go +++ b/assert.go @@ -63,20 +63,38 @@ func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any // An optional message and arguments for any format placeholders in that message // can be provided if a failure occurs. func Assert(t TestingT, condition bool, msgAndArgs ...any) { + t.Helper() + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"assertion failed"} + } + passed := Check(t, condition, msgAndArgs...) + if !passed { + t.FailNow() + } +} + +// Check evaluates a boolean condition and logs a message if the condition is false. +// Unlike Assert, it does not stop the test when the condition is false but returns +// a boolean indicating the result. This function is useful for cases where you want +// to perform multiple checks in a single test function without stopping the test +// execution after the first failure. An optional message and arguments for any format +// placeholders in that message can be provided if the check fails. +func Check(t TestingT, condition bool, msgAndArgs ...any) bool { t.Helper() if !condition { - message := "assertion failed" + message := "check failed" if len(msgAndArgs) > 0 { switch msgAndArgs[0].(type) { case string: message = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) default: - t.Log("checkmate: Assert called with a non-string message, using default message") + t.Log("checkmate: assertion called with a non-string message, using default message") } } t.Log(message) - t.FailNow() + return false } + return true } // The subset of testing.T which is used by the -- cgit v1.2.3