aboutsummaryrefslogtreecommitdiffstats
path: root/assert.go
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 /assert.go
parentRun go mod tidy (diff)
downloadcheckmate-f2713fa991c914bf6b2beeea9b2938d022b738b4.zip
Add Check test helper
Diffstat (limited to 'assert.go')
-rw-r--r--assert.go24
1 files changed, 21 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