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 +++++++++++++++++++++--- assert_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) 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 diff --git a/assert_test.go b/assert_test.go index 27f73eb..59645a6 100644 --- a/assert_test.go +++ b/assert_test.go @@ -27,6 +27,30 @@ func (m *MockT) Log(args ...any) { m.Logs = append(m.Logs, fmt.Sprint(args...)) } +func TestCheck(t *testing.T) { + t.Run("True", func(t *testing.T) { + mockT := &MockT{} + Check(mockT, true) + if mockT.FailNowCalled { + t.Fatal("Check should not be failing any tests") + } + if len(mockT.Logs) > 0 { + t.Fatal("Check should not emit any logs on success") + } + }) + + t.Run("False", func(t *testing.T) { + mockT := &MockT{} + Check(mockT, false) + if mockT.FailNowCalled { + t.Fatal("Check should not be failing any tests") + } + if len(mockT.Logs) == 0 { + t.Fatal("Check should be emiting a logs on failure") + } + }) +} + func TestAssert(t *testing.T) { t.Run("True", func(t *testing.T) { mockT := &MockT{} @@ -42,6 +66,9 @@ func TestAssert(t *testing.T) { if !mockT.FailNowCalled { t.Fatal("Assert passed when it should have failed") } + if len(mockT.Logs) == 0 { + t.Fatal("Assert should have emitted a failure log") + } }) } -- cgit v1.2.3