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 /assert/assert_test.go | |
| parent | Fix typo (diff) | |
| download | checkmate-c4f88b3173e34387147499f6f3a66b5595c7bf47.zip | |
Add check module, add some not functions, and remove a few test fns
Diffstat (limited to 'assert/assert_test.go')
| -rw-r--r-- | assert/assert_test.go | 225 |
1 files changed, 78 insertions, 147 deletions
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 |