aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-27 22:32:18 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-27 22:32:18 -0500
commitf2713fa991c914bf6b2beeea9b2938d022b738b4 (patch)
tree1303326fd479668c56cdca15af56ffa6ec6b27d2
parentRun go mod tidy (diff)
downloadcheckmate-f2713fa991c914bf6b2beeea9b2938d022b738b4.zip
Add Check test helper
-rw-r--r--assert.go24
-rw-r--r--assert_test.go27
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
@@ -64,19 +64,37 @@ func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any
// 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")
+ }
})
}