diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-01 22:34:52 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-01 22:34:52 -0500 |
| commit | c4f88b3173e34387147499f6f3a66b5595c7bf47 (patch) | |
| tree | b12eea8e762b9ca62a52c25fdb928e2f821ac504 | |
| parent | Fix typo (diff) | |
| download | checkmate-c4f88b3173e34387147499f6f3a66b5595c7bf47.zip | |
Add check module, add some not functions, and remove a few test fns
| -rw-r--r-- | CHANGELOG.md | 14 | ||||
| -rw-r--r-- | README.md | 43 | ||||
| -rw-r--r-- | assert/assert.go | 190 | ||||
| -rw-r--r-- | assert/assert_test.go | 225 | ||||
| -rw-r--r-- | check/check.go | 201 | ||||
| -rw-r--r-- | check/check_test.go | 345 | ||||
| -rw-r--r-- | doc.go | 24 | ||||
| -rw-r--r-- | internal/cmtest/cmtest.go | 39 |
8 files changed, 755 insertions, 326 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bed6e..c43c7d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Added `check` module. This module contains the same functions as the + `assert` module, but they return a boolean and mark the test as failed + instead of failing the test immediately. + +- `NotErrorIs`, `NotErrorContains`, and `NotDeepEqual` functions. + +### Removed + +- `LenEqual`, `Assert`, and `Check` functions. + ## [0.2.0] - 2024-01-28 ### Changed @@ -15,8 +15,6 @@ Checkmate is a Go 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. - ## Installation To install Checkmate, use the following command: @@ -24,44 +22,3 @@ To install Checkmate, use the following command: ```bash $ go get github.com/eugenetriguba/checkmate ``` - -## Usage - -To get a feel for what Checkmate has to offer, here is a simple example with some commentary that shows all the supported assertion functions. For a reference on each function, see the [GoDoc](https://godoc.org/github.com/eugenetriguba/checkmate). - -```go - -import ( - "errors" - "os" - "testing" - - "github.com/eugenetriguba/checkmate/assert" -) - -func TestAssertions(t *testing.T) { - // Assert is the base general assertion function. It - // takes in any boolean expression and will fail the - // test if the expression is false. - assert.Assert(t, true) - - // Also note, all functions in this library take in an - // optional message argument which may have placeholders - // that are formatted with the remaining arguments. - assert.Assert(t, false, "expected %v to be true", false) - - // Check is like Assert, but it will not fail the test. - assert.Check(t, true) - - assert.True(t, true) - assert.False(t, false) - assert.Equal(t, 1, 1) - assert.DeepEqual(t, []int{1, 2, 3}, []int{1, 2, 3}) - assert.LenEqual(t, []int{1, 2, 3}, 3) - assert.NotEqual(t, 1, 2) - assert.Nil(t, nil) - assert.NotNil(t, "not nil") - assert.ErrorIs(t, os.ErrInvalid, os.ErrInvalid) - assert.ErrorContains(t, errors.New("error 1"), "error 1") -} -``` 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 { diff --git a/assert/assert_test.go b/assert/assert_test.go index a21f00c..97ff550 100644 --- a/assert/assert_test.go +++ b/assert/assert_test.go @@ -6,154 +6,107 @@ import ( "os" "strings" "testing" -) - -// MockT is a mock structure for capturing test outputs. -type MockT struct { - TestingT - - FailNowCalled bool - Logs []string -} - -func (m *MockT) FailNow() { - m.FailNowCalled = true -} -func (m *MockT) Log(args ...any) { - m.Logs = append(m.Logs, fmt.Sprint(args...)) -} + "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/internal/cmtest" +) -// MockHelperT has a MockT and is also a helperT. -type MockHelperT struct { - MockT +type assertFn func(t checkmate.TestingT, args []any) - HelperCalled bool -} - -func (m *MockHelperT) Helper() { - m.HelperCalled = true +func wrappedAssertNil(t checkmate.TestingT, args []any) { + if len(args) > 1 { + Nil(t, args[0], args[1:]...) + } else { + Nil(t, args[0]) + } } -// assertFn defines a common interface to be able to -// wrap all the assertion functions in the library to -// a common interface to make testing their common behavior -// with testing tables easier. -type assertFn func(t TestingT, args []any) - -func wrapCheck(t TestingT, args []any) { +func wrappedAssertNotNil(t checkmate.TestingT, args []any) { if len(args) > 1 { - Check(t, args[0].(bool), args[1:]...) + NotNil(t, args[0], args[1:]...) } else { - Check(t, args[0].(bool)) + NotNil(t, args[0]) } } -func wrapAssert(t TestingT, args []any) { +func wrappedAssertTrue(t checkmate.TestingT, args []any) { if len(args) > 1 { - Assert(t, args[0].(bool), args[1:]...) + True(t, args[0].(bool), args[1:]...) } else { - Assert(t, args[0].(bool)) + True(t, args[0].(bool)) } } -func wrapAssertEqual(t TestingT, args []any) { - if len(args) > 2 { - Equal(t, args[0], args[1], args[2:]...) - +func wrappedAssertFalse(t checkmate.TestingT, args []any) { + if len(args) > 1 { + False(t, args[0].(bool), args[1:]...) } else { - Equal(t, args[0], args[1]) + False(t, args[0].(bool)) } } -func wrapAssertDeepEqual(t TestingT, args []any) { +func wrappedAssertDeepEqual(t checkmate.TestingT, args []any) { if len(args) > 2 { DeepEqual(t, args[0], args[1], args[2:]...) - } else { DeepEqual(t, args[0], args[1]) } } -func wrapAssertNotEqual(t TestingT, args []any) { +func wrappedAssertNotDeepEqual(t checkmate.TestingT, args []any) { if len(args) > 2 { - NotEqual(t, args[0], args[1], args[2:]...) - + NotDeepEqual(t, args[0], args[1], args[2:]...) } else { - NotEqual(t, args[0], args[1]) + NotDeepEqual(t, args[0], args[1]) } } -func wrapAssertLenEqualInt(t TestingT, args []any) { - slice, ok := args[0].([]int) - if !ok { - t.Log("Invalid argument type for AssertLenEqualInt") - } - - expectedLen, ok := args[1].(int) - if !ok { - t.Log("Invalid length argument for AssertLenEqualInt") - } - +func wrappedAssertErrorIs(t checkmate.TestingT, args []any) { if len(args) > 2 { - LenEqual(t, slice, expectedLen, args[2:]...) + ErrorIs(t, args[0].(error), args[1].(error), args[2:]...) } else { - LenEqual(t, slice, expectedLen) + ErrorIs(t, args[0].(error), args[1].(error)) } } -func wrapAssertErrorIs(t TestingT, args []any) { +func wrappedAssertNotErrorIs(t checkmate.TestingT, args []any) { if len(args) > 2 { - ErrorIs(t, args[0].(error), args[1].(error), args[2:]...) - + NotErrorIs(t, args[0].(error), args[1].(error), args[2:]...) } else { - ErrorIs(t, args[0].(error), args[1].(error)) + NotErrorIs(t, args[0].(error), args[1].(error)) } } -func wrapAssertErrorContains(t TestingT, args []any) { +func wrappedAssertErrorContains(t checkmate.TestingT, args []any) { if len(args) > 2 { ErrorContains(t, args[0].(error), args[1].(string), args[2:]...) - } else { ErrorContains(t, args[0].(error), args[1].(string)) } } -func wrapAssertTrue(t TestingT, args []any) { - if len(args) > 1 { - True(t, args[0].(bool), args[1:]...) - - } else { - True(t, args[0].(bool)) - } -} - -func wrapAssertFalse(t TestingT, args []any) { - if len(args) > 1 { - False(t, args[0].(bool), args[1:]...) - +func wrappedAssertNotErrorContains(t checkmate.TestingT, args []any) { + if len(args) > 2 { + NotErrorContains(t, args[0].(error), args[1].(string), args[2:]...) } else { - False(t, args[0].(bool)) + NotErrorContains(t, args[0].(error), args[1].(string)) } } -func wrapAssertNil(t TestingT, args []any) { - if len(args) > 1 { - Nil(t, args[0], args[1:]...) - +func wrappedAssertEqual(t checkmate.TestingT, args []any) { + if len(args) > 2 { + Equal(t, args[0], args[1], args[2:]...) } else { - Nil(t, args[0]) + Equal(t, args[0], args[1]) } } -func wrapAssertNotNil(t TestingT, args []any) { - if len(args) > 1 { - NotNil(t, args[0], args[1:]...) - +func wrappedAssertNotEqual(t checkmate.TestingT, args []any) { + if len(args) > 2 { + NotEqual(t, args[0], args[1], args[2:]...) } else { - NotNil(t, args[0]) + NotEqual(t, args[0], args[1]) } } @@ -162,18 +115,18 @@ var passingTestFns = []struct { assertionFn assertFn args []any }{ - {"Check", wrapCheck, []any{true}}, - {"Assert", wrapAssert, []any{true}}, - {"AssertEqual", wrapAssertEqual, []any{5, 5}}, - {"AssertDeepEqual", wrapAssertDeepEqual, []any{5, 5}}, - {"AssertNotEqual", wrapAssertNotEqual, []any{5, 10}}, - {"AssertLenEqual", wrapAssertLenEqualInt, []any{[]int{1, 2}, 2}}, - {"AssertErrorIs", wrapAssertErrorIs, []any{os.ErrExist, os.ErrExist}}, - {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 1"}}, - {"AssertTrue", wrapAssertTrue, []any{true}}, - {"AssertFalse", wrapAssertFalse, []any{false}}, - {"AssertNil", wrapAssertNil, []any{nil}}, - {"AssertNotNil", wrapAssertNotNil, []any{1}}, + {"AssertNil", wrappedAssertNil, []any{nil}}, + {"AssertNotNil", wrappedAssertNotNil, []any{1}}, + {"AssertTrue", wrappedAssertTrue, []any{true}}, + {"AssertFalse", wrappedAssertFalse, []any{false}}, + {"AssertErrorIs", wrappedAssertErrorIs, []any{os.ErrExist, os.ErrExist}}, + {"AssertNotErrorIs", wrappedAssertNotErrorIs, []any{os.ErrExist, os.ErrClosed}}, + {"AssertErrorContains", wrappedAssertErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"AssertNotErrorContains", wrappedAssertNotErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"AssertDeepEqual", wrappedAssertDeepEqual, []any{5, 5}}, + {"AssertNotDeepEqual", wrappedAssertNotDeepEqual, []any{5, 6}}, + {"AssertEqual", wrappedAssertEqual, []any{5, 5}}, + {"AssertNotEqual", wrappedAssertNotEqual, []any{5, 10}}, } var failingTestFns = []struct { @@ -181,18 +134,18 @@ var failingTestFns = []struct { assertionFn assertFn args []any }{ - {"Check", wrapCheck, []any{false}}, - {"Assert", wrapAssert, []any{false}}, - {"AssertEqual", wrapAssertEqual, []any{5, 10}}, - {"AssertDeepEqual", wrapAssertDeepEqual, []any{5, 10}}, - {"AssertNotEqual", wrapAssertNotEqual, []any{5, 5}}, - {"AssertLenEqual", wrapAssertLenEqualInt, []any{[]int{1, 2}, 0}}, - {"AssertErrorIs", wrapAssertErrorIs, []any{errors.New("error 1"), errors.New("error 2")}}, - {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 2"}}, - {"AssertTrue", wrapAssertTrue, []any{false}}, - {"AssertFalse", wrapAssertFalse, []any{true}}, - {"AssertNil", wrapAssertNil, []any{1}}, - {"AssertNotNil", wrapAssertNotNil, []any{nil}}, + {"AssertNil", wrappedAssertNil, []any{1}}, + {"AssertNotNil", wrappedAssertNotNil, []any{nil}}, + {"AssertTrue", wrappedAssertTrue, []any{false}}, + {"AssertFalse", wrappedAssertFalse, []any{true}}, + {"AssertErrorIs", wrappedAssertErrorIs, []any{errors.New("error 1"), errors.New("error 2")}}, + {"AssertNotErrorIs", wrappedAssertNotErrorIs, []any{os.ErrClosed, os.ErrClosed}}, + {"AssertErrorContains", wrappedAssertErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"AssertNotErrorContains", wrappedAssertNotErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"AssertDeepEqual", wrappedAssertDeepEqual, []any{5, 10}}, + {"AssertNotDeepEqual", wrappedAssertNotDeepEqual, []any{5, 5}}, + {"AssertEqual", wrappedAssertEqual, []any{5, 10}}, + {"AssertNotEqual", wrappedAssertNotEqual, []any{5, 5}}, } func TestOptionalMessageAndArgs(t *testing.T) { @@ -207,7 +160,7 @@ func TestOptionalMessageAndArgs(t *testing.T) { "Invalid message type", []any{5}, []string{ - "checkmate: assertion called with a non-string message, using default message", + "checkmate: check called with a non-string message, using default message", "check failed", }, }, @@ -217,15 +170,15 @@ func TestOptionalMessageAndArgs(t *testing.T) { for _, testCase := range testCases { testName := fmt.Sprintf("%s: %s", testFn.name, testCase.name) t.Run(testName, func(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} testFn.assertionFn(mockT, append(testFn.args, testCase.args...)) if len(mockT.Logs) != len(testCase.expectedLogs) { t.Errorf( "%s: expected %d (%v) log messages but got %d (%v)", - testName, len(mockT.Logs), mockT.Logs, len(testCase.expectedLogs), - testCase.expectedLogs, + testName, len(testCase.expectedLogs), + testCase.expectedLogs, len(mockT.Logs), mockT.Logs, ) } if len(mockT.Logs) == len(testCase.expectedLogs) { @@ -244,9 +197,9 @@ func TestOptionalMessageAndArgs(t *testing.T) { } func TestAssertFnsCallHelper(t *testing.T) { - for _, testFn := range failingTestFns { + for _, testFn := range passingTestFns { t.Run(testFn.name, func(t *testing.T) { - mockHelperT := &MockHelperT{} + mockHelperT := &cmtest.MockHelperT{} testFn.assertionFn(mockHelperT, testFn.args) @@ -260,7 +213,7 @@ func TestAssertFnsCallHelper(t *testing.T) { func TestAssertFnsShouldNotFailOrEmitLogsOnSuccess(t *testing.T) { for _, testFn := range passingTestFns { t.Run(testFn.name, func(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} testFn.assertionFn(mockT, testFn.args) if mockT.FailNowCalled { @@ -279,28 +232,6 @@ func TestAssertFnsShouldNotFailOrEmitLogsOnSuccess(t *testing.T) { } } -func TestCheckReturnValues(t *testing.T) { - t.Run("should return true on true condition", func(t *testing.T) { - mockT := &MockT{} - - passed := Check(mockT, true) - - if !passed { - t.Fatal("Check should have returned true on true condition") - } - }) - - t.Run("should return false on false condition", func(t *testing.T) { - mockT := &MockT{} - - passed := Check(mockT, false) - - if passed { - t.Fatal("Check should have returned false on false condition") - } - }) -} - func TestAssertEqual(t *testing.T) { testCases := []struct { name string @@ -321,7 +252,7 @@ func TestAssertEqual(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} Equal(mockT, tc.actual, tc.expected) if mockT.FailNowCalled != tc.shouldFail { @@ -357,7 +288,7 @@ func TestAssertDeepEqual(t *testing.T) { } t.Run("Equal structs", func(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30}) if mockT.FailNowCalled { t.Error("AssertDeepEqual failed when it should have passed") @@ -365,7 +296,7 @@ func TestAssertDeepEqual(t *testing.T) { }) t.Run("Not equal structs", func(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30}) if !mockT.FailNowCalled { t.Error("AssertDeepEqual passed when it should have failed") @@ -381,7 +312,7 @@ func containsDiffMessage(log string) bool { } func TestAssertErrorIsChecksErrTree(t *testing.T) { - mockT := &MockT{} + mockT := &cmtest.MockT{} actual := fmt.Errorf("wrapped: %w", os.ErrInvalid) expected := os.ErrInvalid diff --git a/check/check.go b/check/check.go new file mode 100644 index 0000000..ddb9d5a --- /dev/null +++ b/check/check.go @@ -0,0 +1,201 @@ +package check + +import ( + "errors" + "fmt" + "strings" + + "github.com/eugenetriguba/checkmate" + "github.com/google/go-cmp/cmp" +) + +type helperT interface { + Helper() +} + +// Nil checks whether the value equals nil. +func Nil(t checkmate.TestingT, value any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to be nil, got %v", value} + } + + return check(t, value == nil, msgAndArgs...) +} + +// NotNil checks whether the value does not equal nil. +func NotNil(t checkmate.TestingT, value any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to not be nil, got nil", value} + } + + return check(t, value != nil, msgAndArgs...) +} + +// True checks whether the condition is true. +func True(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be true, got false"} + } + + return check(t, condition, msgAndArgs...) +} + +// False checks whether the condition is false. +func False(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be false, got true"} + } + + return check(t, !condition, msgAndArgs...) +} + +// ErrorIs checks whether the target error occurs within err's error tree. +func ErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected error %v to have error %v in its tree", err, target} + } + + return check(t, errors.Is(err, target), msgAndArgs...) +} + +// NotErrorIs checks whether the target error does not occur within +// the err's error tree. +func NotErrorIs(t checkmate.TestingT, err error, target error, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected error %v to not have error %v in its tree", err, target} + } + + return check(t, !errors.Is(err, target), msgAndArgs...) +} + +// ErrorContains checks whether the given err contains the errText +// in the err.Error() output. +func ErrorContains(t checkmate.TestingT, err error, errText string, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected err to contain %s, got %s", errText, err.Error()} + } + + return check(t, strings.Contains(err.Error(), errText), msgAndArgs...) +} + +// NotErrorContains checks 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) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected err to contain not %s, got that it does", errText} + } + + return check(t, !strings.Contains(err.Error(), errText), msgAndArgs...) +} + +// DeepEqual checks if two values are deeply equal. If they are not equal, +// it logs the differences. +func DeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + diff := cmp.Diff(expected, actual) + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"mismatch (-expected +actual):\n%s", diff} + } + + return check(t, diff == "", msgAndArgs...) +} + +// NotDeepEqual checks if two values are not deeply equal. +func NotDeepEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + diff := cmp.Diff(expected, actual) + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to not equal %v, got that they're equal", actual, expected} + } + return check(t, diff != "", msgAndArgs...) +} + +// Equal checks if two primitive values are equal. +func Equal(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to equal %v", expected, actual} + } + + return check(t, actual == expected, msgAndArgs...) +} + +// NotEqual checks if two values are not equal. It fails the test if +// the values are equal. +func NotEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) bool { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to not equal %v", actual, expected} + } + + return check(t, actual != expected, msgAndArgs...) +} + +// Check evaluates a boolean condition and if the condition is false, +// it will log out a message and mark the test as failed. However, it does +// not immediately stop execution, unlike the assert functions. +func check(t checkmate.TestingT, condition bool, msgAndArgs ...any) bool { + 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: check called with a non-string message, using default message") + } + } + t.Log(message) + t.Fail() + } + + return condition +} diff --git a/check/check_test.go b/check/check_test.go new file mode 100644 index 0000000..9c73d52 --- /dev/null +++ b/check/check_test.go @@ -0,0 +1,345 @@ +package check + +import ( + "errors" + "fmt" + "os" + "strings" + "testing" + + "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/internal/cmtest" +) + +type checkFn func(t checkmate.TestingT, args []any) bool + +func wrappedCheckNil(t checkmate.TestingT, args []any) bool { + if len(args) > 1 { + return Nil(t, args[0], args[1:]...) + } else { + return Nil(t, args[0]) + } +} + +func wrappedCheckNotNil(t checkmate.TestingT, args []any) bool { + if len(args) > 1 { + return NotNil(t, args[0], args[1:]...) + } else { + return NotNil(t, args[0]) + } +} + +func wrappedCheckTrue(t checkmate.TestingT, args []any) bool { + if len(args) > 1 { + return True(t, args[0].(bool), args[1:]...) + } else { + return True(t, args[0].(bool)) + } +} + +func wrappedCheckFalse(t checkmate.TestingT, args []any) bool { + if len(args) > 1 { + return False(t, args[0].(bool), args[1:]...) + } else { + return False(t, args[0].(bool)) + } +} + +func wrappedCheckDeepEqual(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return DeepEqual(t, args[0], args[1], args[2:]...) + } else { + return DeepEqual(t, args[0], args[1]) + } +} + +func wrappedCheckNotDeepEqual(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return NotDeepEqual(t, args[0], args[1], args[2:]...) + } else { + return NotDeepEqual(t, args[0], args[1]) + } +} + +func wrappedCheckErrorIs(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return ErrorIs(t, args[0].(error), args[1].(error), args[2:]...) + } else { + return ErrorIs(t, args[0].(error), args[1].(error)) + } +} + +func wrappedCheckNotErrorIs(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return NotErrorIs(t, args[0].(error), args[1].(error), args[2:]...) + } else { + return NotErrorIs(t, args[0].(error), args[1].(error)) + } +} + +func wrappedCheckErrorContains(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return ErrorContains(t, args[0].(error), args[1].(string), args[2:]...) + } else { + return ErrorContains(t, args[0].(error), args[1].(string)) + } +} + +func wrappedCheckNotErrorContains(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return NotErrorContains(t, args[0].(error), args[1].(string), args[2:]...) + } else { + return NotErrorContains(t, args[0].(error), args[1].(string)) + } +} + +func wrappedCheckEqual(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return Equal(t, args[0], args[1], args[2:]...) + } else { + return Equal(t, args[0], args[1]) + } +} + +func wrappedCheckNotEqual(t checkmate.TestingT, args []any) bool { + if len(args) > 2 { + return NotEqual(t, args[0], args[1], args[2:]...) + } else { + return NotEqual(t, args[0], args[1]) + } +} + +var passingTestFns = []struct { + name string + fn checkFn + args []any +}{ + {"CheckNil", wrappedCheckNil, []any{nil}}, + {"CheckNotNil", wrappedCheckNotNil, []any{1}}, + {"CheckTrue", wrappedCheckTrue, []any{true}}, + {"CheckFalse", wrappedCheckFalse, []any{false}}, + {"CheckErrorIs", wrappedCheckErrorIs, []any{os.ErrExist, os.ErrExist}}, + {"CheckNotErrorIs", wrappedCheckNotErrorIs, []any{os.ErrExist, os.ErrClosed}}, + {"CheckErrorContains", wrappedCheckErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"CheckNotErrorContains", wrappedCheckNotErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"CheckDeepEqual", wrappedCheckDeepEqual, []any{5, 5}}, + {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 6}}, + {"CheckEqual", wrappedCheckEqual, []any{5, 5}}, + {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 10}}, +} + +var failingTestFns = []struct { + name string + fn checkFn + args []any +}{ + {"CheckNil", wrappedCheckNil, []any{1}}, + {"CheckNotNil", wrappedCheckNotNil, []any{nil}}, + {"CheckTrue", wrappedCheckTrue, []any{false}}, + {"CheckFalse", wrappedCheckFalse, []any{true}}, + {"CheckErrorIs", wrappedCheckErrorIs, []any{errors.New("error 1"), errors.New("error 2")}}, + {"CheckNotErrorIs", wrappedCheckNotErrorIs, []any{os.ErrClosed, os.ErrClosed}}, + {"CheckErrorContains", wrappedCheckErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"CheckNotErrorContains", wrappedCheckNotErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"CheckDeepEqual", wrappedCheckDeepEqual, []any{5, 10}}, + {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 5}}, + {"CheckEqual", wrappedCheckEqual, []any{5, 10}}, + {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 5}}, +} + +func TestOptionalMessageAndArgs(t *testing.T) { + testCases := []struct { + name string + args []any + expectedLogs []string + }{ + {"Plain message", []any{"my message"}, []string{"my message"}}, + {"Message with format placeholders", []any{"my message: %d", 5}, []string{"my message: 5"}}, + { + "Invalid message type", + []any{5}, + []string{ + "checkmate: check called with a non-string message, using default message", + "check failed", + }, + }, + } + + for _, testFn := range failingTestFns { + for _, testCase := range testCases { + testName := fmt.Sprintf("%s: %s", testFn.name, testCase.name) + t.Run(testName, func(t *testing.T) { + mockT := &cmtest.MockT{} + + testFn.fn(mockT, append(testFn.args, testCase.args...)) + + if len(mockT.Logs) != len(testCase.expectedLogs) { + t.Errorf( + "%s: expected %d (%v) log messages but got %d (%v)", + testName, len(testCase.expectedLogs), + testCase.expectedLogs, len(mockT.Logs), mockT.Logs, + ) + } + if len(mockT.Logs) == len(testCase.expectedLogs) { + for i, expectedLogMsg := range testCase.expectedLogs { + if mockT.Logs[i] != expectedLogMsg { + t.Errorf( + "%s: expected log message '%s', got '%s'", + testName, expectedLogMsg, mockT.Logs[i], + ) + } + } + } + }) + } + } +} + +func TestCheckFnsCallHelper(t *testing.T) { + for _, testFn := range passingTestFns { + t.Run(testFn.name, func(t *testing.T) { + mockHelperT := &cmtest.MockHelperT{} + + testFn.fn(mockHelperT, testFn.args) + + if !mockHelperT.HelperCalled { + t.Errorf("%s: expected HelperT to be called", testFn.name) + } + }) + } +} + +func TestCheckFnsShouldNotFailOrEmitLogsOnSuccess(t *testing.T) { + for _, testFn := range passingTestFns { + t.Run(testFn.name, func(t *testing.T) { + mockT := &cmtest.MockT{} + testFn.fn(mockT, testFn.args) + + if mockT.FailNowCalled || mockT.FailCalled { + t.Fatalf( + "%s: fail called, expected the test to not be failed on true condition", + testFn.name, + ) + } + if len(mockT.Logs) > 0 { + t.Fatalf( + "%s: %v logs emitted, expected the test to not emit any logs on true condition", + testFn.name, mockT.Logs, + ) + } + }) + } +} + +func TestAssertEqual(t *testing.T) { + testCases := []struct { + name string + actual any + expected any + shouldFail bool + logMessages []string + }{ + {"EqualIntegers", 5, 5, false, []string{}}, + {"UnequalIntegers", 5, 10, true, []string{"expected 10 to equal 5"}}, + {"EqualFloats", 5.123, 5.123, false, []string{}}, + {"UnequalFloats", 5.123, 5.1234, true, []string{"expected 5.1234 to equal 5.123"}}, + {"EqualStrings", "test", "test", false, []string{}}, + {"UnequalStrings", "test", "fail", true, []string{"expected fail to equal test"}}, + {"EqualBooleans", true, true, false, []string{}}, + {"UnequalBooleans", false, true, true, []string{"expected true to equal false"}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + mockT := &cmtest.MockT{} + Equal(mockT, tc.actual, tc.expected) + + if mockT.FailCalled != tc.shouldFail { + t.Errorf( + "%s: FailCalled = %v, want %v", + tc.name, mockT.FailCalled, tc.shouldFail, + ) + } + if len(mockT.Logs) != len(tc.logMessages) { + t.Errorf( + "%s: expected %d (%v) log messages but got %d (%v)", + tc.name, len(mockT.Logs), mockT.Logs, len(tc.logMessages), tc.logMessages, + ) + } + if len(mockT.Logs) == len(tc.logMessages) { + for i, expectedLogMsg := range tc.logMessages { + if mockT.Logs[i] != expectedLogMsg { + t.Errorf( + "%s: expected log message '%s', got '%s'", + tc.name, expectedLogMsg, mockT.Logs[0], + ) + } + } + } + }) + } +} + +func TestAssertDeepEqual(t *testing.T) { + type TestStruct struct { + Name string + Age int + } + + t.Run("Equal structs", func(t *testing.T) { + mockT := &cmtest.MockT{} + DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30}) + if mockT.FailCalled { + t.Error("AssertDeepEqual failed when it should have passed") + } + }) + + t.Run("Not equal structs", func(t *testing.T) { + mockT := &cmtest.MockT{} + DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30}) + if !mockT.FailCalled { + t.Error("AssertDeepEqual passed when it should have failed") + } + if len(mockT.Logs) == 0 || !containsDiffMessage(mockT.Logs[0]) { + t.Error("AssertDeepEqual did not log the correct diff message") + } + }) +} + +func containsDiffMessage(log string) bool { + return strings.Contains(log, "(-expected +actual)") +} + +func TestAssertErrorIsChecksErrTree(t *testing.T) { + mockT := &cmtest.MockT{} + actual := fmt.Errorf("wrapped: %w", os.ErrInvalid) + expected := os.ErrInvalid + + ErrorIs(mockT, actual, expected) + + if mockT.FailNowCalled { + t.Fatal("AssertErrorIs failed when it should have passed") + } +} + +func TestCheckReturnValues(t *testing.T) { + t.Run("should return true on true condition", func(t *testing.T) { + mockT := &cmtest.MockT{} + + passed := check(mockT, true) + + if !passed { + t.Fatal("Check should have returned true on true condition") + } + }) + + t.Run("should return false on false condition", func(t *testing.T) { + mockT := &cmtest.MockT{} + + passed := check(mockT, false) + + if passed { + t.Fatal("Check should have returned false on false condition") + } + }) +} @@ -0,0 +1,24 @@ +// 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 library is divided into two different packages. It provides a `check` package and an +// `assert` package. The difference between them is that the functions inside of the `assert` +// package will immediately fail the test on an assertion failure. However, the functions inside +// of the `check` package will mark the test as failed and return a boolean result, however, they +// will continue execution until the test has finished. Outside of those two differences, the +// API and behavior is the same between them. +// +// Furthermore, any of the assertion of check package will accept a variadic argument at the +// end called `msgAndArgs`. This allows the caller to pass in their own custom message on test +// failure along with any arguments for the message if any format placeholders were used. +package checkmate + +// The subset of testing.T which is used by the +// checkmate package. +type TestingT interface { + Log(args ...any) + Fail() + FailNow() +} diff --git a/internal/cmtest/cmtest.go b/internal/cmtest/cmtest.go new file mode 100644 index 0000000..a3454c6 --- /dev/null +++ b/internal/cmtest/cmtest.go @@ -0,0 +1,39 @@ +package cmtest + +import ( + "fmt" + + "github.com/eugenetriguba/checkmate" +) + +// MockT is a mock structure for capturing test outputs. +type MockT struct { + checkmate.TestingT + + FailNowCalled bool + FailCalled bool + Logs []string +} + +func (m *MockT) FailNow() { + m.FailNowCalled = true +} + +func (m *MockT) Fail() { + m.FailCalled = true +} + +func (m *MockT) Log(args ...any) { + m.Logs = append(m.Logs, fmt.Sprint(args...)) +} + +// MockHelperT has a MockT and is also a helperT. +type MockHelperT struct { + MockT + + HelperCalled bool +} + +func (m *MockHelperT) Helper() { + m.HelperCalled = true +} |