aboutsummaryrefslogtreecommitdiffstats
path: root/assert/assert.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-02-01 22:34:52 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-02-01 22:34:52 -0500
commitc4f88b3173e34387147499f6f3a66b5595c7bf47 (patch)
treeb12eea8e762b9ca62a52c25fdb928e2f821ac504 /assert/assert.go
parentFix typo (diff)
downloadcheckmate-c4f88b3173e34387147499f6f3a66b5595c7bf47.zip
Add check module, add some not functions, and remove a few test fns
Diffstat (limited to 'assert/assert.go')
-rw-r--r--assert/assert.go190
1 files changed, 54 insertions, 136 deletions
diff --git a/assert/assert.go b/assert/assert.go
index b120ec5..aae8d26 100644
--- a/assert/assert.go
+++ b/assert/assert.go
@@ -1,225 +1,143 @@
-// Checkmate is a library designed to enhance the testing experience by providing a
-// set of assertion functions. With Go, there is no built-in assertion library which
-// can make writing tests feel more verbose than it needs to be, so Checkmate aims to
-// fill that gap.
-//
-// This is a simple and small library. If you need something more advanced and full-featured,
-// [Testify](https://github.com/stretchr/testify), [Gomega](https://github.com/onsi/gomega), or
-// [gotest.tools](https://github.com/gotestyourself/gotest.tools) are all great options.
package assert
import (
- "errors"
- "fmt"
- "strings"
-
- "github.com/google/go-cmp/cmp"
+ "github.com/eugenetriguba/checkmate"
+ "github.com/eugenetriguba/checkmate/check"
)
-// Nil checks whether the value equals nil. An optional
-// message and arguments for any format placeholders in that
-// message can be provided for if a failure occurs.
-func Nil(t TestingT, value any, msgAndArgs ...any) {
+// Nil asserts whether the value equals nil.
+func Nil(t checkmate.TestingT, value any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected value to be nil, got %v", value}
+ if passed := check.Nil(t, value, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, value == nil, msgAndArgs...)
}
-// NotNil checks whether the value does not equal nil.
-// An optional message and arguments for any format placeholders
-// in that message can be provided for if a failure occurs.
-func NotNil(t TestingT, value any, msgAndArgs ...any) {
+// NotNil asserts whether the value does not equal nil.
+func NotNil(t checkmate.TestingT, value any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected value to not be nil, got nil", value}
+ if passed := check.NotNil(t, value, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, value != nil, msgAndArgs...)
}
-// True checks whether the condition is true.
-// An optional message and arguments for any format placeholders
-// in that message can be provided for if a failure occurs.
-func True(t TestingT, condition bool, msgAndArgs ...any) {
+// True asserts whether the condition is true.
+func True(t checkmate.TestingT, condition bool, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected condition to be true, got false"}
+ if passed := check.True(t, condition, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, condition, msgAndArgs...)
}
-// False checks whether the condition is false.
-// An optional message and arguments for any format placeholders
-// in that message can be provided for if a failure occurs.
-func False(t TestingT, condition bool, msgAndArgs ...any) {
+// False asserts whether the condition is false.
+func False(t checkmate.TestingT, condition bool, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected condition to be false, got true"}
+ if passed := check.False(t, condition, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, !condition, msgAndArgs...)
}
-// ErrorIs checks whether any error in the actual error's tree
-// matches the expected error. An optional message and arguments for
-// any format placeholders in that message can be provided for if a
-// failure occurs.
-func ErrorIs(t TestingT, actual error, expected error, msgAndArgs ...any) {
+// ErrorIs asserts whether the target error occurs within err's error tree.
+func ErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected error %v to have error %v in its tree", actual, expected}
+ if passed := check.ErrorIs(t, err, target, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, errors.Is(actual, expected), msgAndArgs...)
}
-// ErrorContains checks whether the given err contains the expectedErrText
-// in the err.Error() output. An optional message and arguments for any format
-// placeholders in that message can be provided for if a failure occurs.
-func ErrorContains(t TestingT, err error, expectedErrText string, msgAndArgs ...any) {
+// NotErrorIs asserts whether the target error does not occur within
+// the err's error tree.
+func NotErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected err to contain %s, got %s", expectedErrText, err.Error()}
+ if passed := check.NotErrorIs(t, err, target, msgAndArgs...); !passed {
+ t.FailNow()
}
-
- Assert(t, strings.Contains(err.Error(), expectedErrText), msgAndArgs...)
}
-// DeepEqual checks if two values are deeply equal using Google's
-// go-cmp cmp.Equal. If they are not equal, it logs the differences
-// using cmp.Diff. An optional message and arguments for any format
-// placeholders in that message can be provided for if a failure occurs.
-func DeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) {
+// ErrorContains asserts whether the given err contains the errText in the err.Error() output.
+func ErrorContains(t checkmate.TestingT, err error, errText string, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if diff := cmp.Diff(expected, actual); diff != "" {
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"mismatch (-expected +actual):\n%s", diff}
- }
- Assert(t, false, msgAndArgs...)
+ if passed := check.ErrorContains(t, err, errText, msgAndArgs...); !passed {
+ t.FailNow()
}
}
-// Equal checks if two primitive values are equal. An optional
-// message and arguments for any format placeholders in that message
-// can be provided for if a failure occurs.
-func Equal(t TestingT, actual, expected any, msgAndArgs ...any) {
+// NotErrorContains asserts whether the given err does not contain the errText
+// in the err.Error() output.
+func NotErrorContains(t checkmate.TestingT, err error, errText string, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if actual != expected {
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected %v to equal %v", expected, actual}
- }
- Assert(t, false, msgAndArgs...)
+ if passed := check.NotErrorContains(t, err, errText, msgAndArgs...); !passed {
+ t.FailNow()
}
}
-// NotEqual checks if two values are not equal. It fails the test if
-// the values are equal. An optional message and arguments for any format
-// placeholders in that message can be provided if a failure occurs.
-func NotEqual(t TestingT, actual, expected any, msgAndArgs ...any) {
+// DeepEqual asserts that two values are deeply equal. If they are not equal,
+// it logs the differences.
+func DeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if actual == expected {
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected %v to not equal %v", expected, actual}
- }
- Assert(t, false, msgAndArgs...)
+ if passed := check.DeepEqual(t, actual, expected, msgAndArgs...); !passed {
+ t.FailNow()
}
}
-// LenEqual checks if the length of a given slice or array equals the
-// expected length. It fails the test if the lengths are not equal. An optional
-// message and arguments for any format placeholders in that message can be provided
-// if a failure occurs.
-func LenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) {
+// NotDeepEqual asserts if two values are not deeply equal.
+func NotDeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"expected %v to have len %d, got len %d", l, len(l), expectedLen}
+ if passed := check.NotDeepEqual(t, actual, expected, msgAndArgs...); !passed {
+ t.FailNow()
}
- Assert(t, len(l) == expectedLen, msgAndArgs...)
}
-// Assert checks a boolean condition and fails the test if the condition is false.
-// This is a general-purpose assertion function used to validate if a condition holds true.
-// 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) {
+// Equal asserts if two primitive values are equal.
+func Equal(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if len(msgAndArgs) == 0 {
- msgAndArgs = []any{"assertion failed"}
- }
- passed := Check(t, condition, msgAndArgs...)
- if !passed {
+
+ if passed := check.Equal(t, actual, expected, msgAndArgs...); !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 {
+// NotEqual asserts if two values are not equal.
+func NotEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
- if !condition {
- 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: assertion called with a non-string message, using default message")
- }
- }
- t.Log(message)
- return false
- }
- return true
-}
-
-// The subset of testing.T which is used by the
-// checkmate package.
-type TestingT interface {
- Log(args ...any)
- FailNow()
+ if passed := check.NotEqual(t, actual, expected, msgAndArgs...); !passed {
+ t.FailNow()
+ }
}
type helperT interface {