aboutsummaryrefslogtreecommitdiffstats
path: root/assert_test.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-28 13:04:47 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-28 13:04:47 -0500
commit677218c1ccf9914984f1b45dff23a20b66942f29 (patch)
treee84692dbf86f4637bfd02534f38caaddca873be6 /assert_test.go
parentAdd working changes (diff)
downloadcheckmate-677218c1ccf9914984f1b45dff23a20b66942f29.zip
Add assertFn and wrappers to tests
Diffstat (limited to 'assert_test.go')
-rw-r--r--assert_test.go222
1 files changed, 201 insertions, 21 deletions
diff --git a/assert_test.go b/assert_test.go
index 277bfb5..be1da76 100644
--- a/assert_test.go
+++ b/assert_test.go
@@ -33,6 +33,155 @@ func (m *MockHelperT) Helper() {
m.HelperCalled = true
}
+// 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) {
+ if len(args) > 1 {
+ Check(t, args[0].(bool), args[1:]...)
+ } else {
+ Check(t, args[0].(bool))
+ }
+}
+
+func wrapAssert(t TestingT, args []any) {
+ if len(args) > 1 {
+ Assert(t, args[0].(bool), args[1:]...)
+ } else {
+ Assert(t, args[0].(bool))
+ }
+}
+
+func wrapAssertEqual(t TestingT, args []any) {
+ if len(args) > 2 {
+ AssertEqual(t, args[0], args[1], args[2:]...)
+
+ } else {
+ AssertEqual(t, args[0], args[1])
+ }
+}
+
+func wrapAssertDeepEqual(t TestingT, args []any) {
+ if len(args) > 2 {
+ AssertDeepEqual(t, args[0], args[1], args[2:]...)
+
+ } else {
+ AssertDeepEqual(t, args[0], args[1])
+ }
+}
+
+func wrapAssertNotEqual(t TestingT, args []any) {
+ if len(args) > 2 {
+ AssertNotEqual(t, args[0], args[1], args[2:]...)
+
+ } else {
+ AssertNotEqual(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")
+ }
+
+ if len(args) > 2 {
+ AssertLenEqual(t, slice, expectedLen, args[2:]...)
+
+ } else {
+ AssertLenEqual(t, slice, expectedLen)
+ }
+}
+
+var passingTestFns = []struct {
+ name string
+ 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}},
+}
+
+var failingTestFns = []struct {
+ name string
+ 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}},
+}
+
+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: assertion 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 := &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,
+ )
+ }
+ 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 TestAssertFnsCallHelper(t *testing.T) {
+ for _, testFn := range failingTestFns {
+ t.Run(testFn.name, func(t *testing.T) {
+ mockHelperT := &MockHelperT{}
+
+ testFn.assertionFn(mockHelperT, testFn.args)
+
+ if !mockHelperT.HelperCalled {
+ t.Errorf("%s: expected HelperT to be called", testFn.name)
+ }
+ })
+ }
+}
+
func TestCheck(t *testing.T) {
t.Run("should not fail or emit any logs on true condition", func(t *testing.T) {
mockT := &MockT{}
@@ -214,31 +363,62 @@ func TestAssert(t *testing.T) {
}
func TestAssertEqual(t *testing.T) {
- t.Run("Equal", func(t *testing.T) {
- mockT := &MockT{}
- AssertEqual(mockT, 5, 5)
- if mockT.FailNowCalled {
- t.Fatal("AssertEqual failed when it should have passed")
- }
- })
+ 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"}},
+ }
- t.Run("Not equal", func(t *testing.T) {
- mockT := &MockT{}
- AssertEqual(mockT, 5, 10)
- if !mockT.FailNowCalled {
- t.Fatal("AssertEqual passed when it should have failed")
- }
- })
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ mockT := &MockT{}
+ AssertEqual(mockT, tc.actual, tc.expected)
+
+ if mockT.FailNowCalled != tc.shouldFail {
+ t.Errorf(
+ "%s: FailNowCalled = %v, want %v",
+ tc.name, mockT.FailNowCalled, 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],
+ )
+ }
+ }
+ }
+ })
+ }
+}
- t.Run("should called Helper() when passed a helperT", func(t *testing.T) {
- mockHelperT := &MockHelperT{}
+func TestAssertEqualCallsHelper(t *testing.T) {
+ mockHelperT := &MockHelperT{}
- AssertEqual(mockHelperT, true, true)
+ AssertEqual(mockHelperT, true, true)
- if !mockHelperT.HelperCalled {
- t.Fatal("HelperT not called")
- }
- })
+ if !mockHelperT.HelperCalled {
+ t.Fatal("HelperT not called")
+ }
}
func TestAssertNotEqual(t *testing.T) {