diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 13:14:18 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 13:14:18 -0500 |
| commit | 1897da4993a494c7ac904e5969d26da85d566000 (patch) | |
| tree | 84fd6194aca8a7ddb12b7cf90fcc4458343dd390 | |
| parent | Limit lines to 100 chars (diff) | |
| download | checkmate-1897da4993a494c7ac904e5969d26da85d566000.zip | |
Use assertFn wrapper and table tests ot cut down on redundant tests
| -rw-r--r-- | assert_test.go | 258 |
1 files changed, 21 insertions, 237 deletions
diff --git a/assert_test.go b/assert_test.go index 803f0e5..7a1a1c5 100644 --- a/assert_test.go +++ b/assert_test.go @@ -189,186 +189,46 @@ func TestAssertFnsCallHelper(t *testing.T) { } } -func TestCheck(t *testing.T) { - t.Run("should not fail or emit any logs 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") - } - if mockT.FailNowCalled { - t.Fatal("Check should not be failing any tests") - } - if len(mockT.Logs) > 0 { - t.Fatal("Check should not emit any logs on success") - } - }) - - t.Run("should not fail but it should emit a log 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") - } - if mockT.FailNowCalled { - t.Fatal("Check should not be failing any tests") - } - if len(mockT.Logs) == 0 { - t.Fatal("Check should be emiting a logs on failure") - } - }) - - t.Run("should allow optional message with no format placeholders", func(t *testing.T) { - mockT := &MockT{} - failureLog := "my failure message" - - Check(mockT, false, failureLog) - - if len(mockT.Logs) != 1 { - t.Fatal("Check should emit one failure log") - } - if mockT.Logs[0] != failureLog { - t.Fatalf( - "Check should emit custom message %s on failure, got %s", - failureLog, mockT.Logs[0], - ) - } - }) - - t.Run("should allow optional message with format placeholders filled in", func(t *testing.T) { - mockT := &MockT{} - - Check(mockT, false, "my failure message: %d", 5) - - if len(mockT.Logs) != 1 { - t.Fatal("Check should emit one failure log") - } - expectedFailureLog := "my failure message: 5" - if mockT.Logs[0] != expectedFailureLog { - t.Fatalf( - "Check should emit custom message %s on failure, got %s", - expectedFailureLog, mockT.Logs[0], - ) - } - }) - - t.Run( - "should default to standard message if incorrect type for message passed", - func(t *testing.T) { +func TestAssertFnsShouldNotFailOrEmitLogsOnSuccess(t *testing.T) { + for _, testFn := range passingTestFns { + t.Run(testFn.name, func(t *testing.T) { mockT := &MockT{} + testFn.assertionFn(mockT, testFn.args) - Check(mockT, false, 5) - - if len(mockT.Logs) != 2 { - t.Fatal("Check should emit one failure log and one warning log") - } - expectedWarningLog := "checkmate: assertion called with a non-string message, using default message" - if mockT.Logs[0] != expectedWarningLog { + if mockT.FailNowCalled { t.Fatalf( - "Check should emit warning message %s on wrong type for message, got %s", - expectedWarningLog, mockT.Logs[0], + "%s: fail now called, expected the test to not be failed on true condition", + testFn.name, ) } - expectedFailureLog := "check failed" - if mockT.Logs[1] != expectedFailureLog { + if len(mockT.Logs) > 0 { t.Fatalf( - "Check should emit default failure message %s on wrong type for message, got %s", - expectedFailureLog, - mockT.Logs[0], + "%s: %v logs emitted, expected the test to not emit any logs on true condition", + testFn.name, mockT.Logs, ) } - }, - ) - - t.Run("should called Helper() when passed a helperT", func(t *testing.T) { - mockHelperT := &MockHelperT{} - - Check(mockHelperT, true) - - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") - } - }) + }) + } } -func TestAssert(t *testing.T) { - t.Run("should not fail test or emit log on true assertion", func(t *testing.T) { - mockT := &MockT{} - - Assert(mockT, true) - - if mockT.FailNowCalled { - t.Fatal("Assert failed when it should have passed") - } - if len(mockT.Logs) > 0 { - t.Fatal("Check should not emit any logs on success") - } - }) - - t.Run("should call fail now and emit a log on false assertion", func(t *testing.T) { - mockT := &MockT{} - - Assert(mockT, false) - - if !mockT.FailNowCalled { - t.Fatal("Assert passed when it should have failed") - } - if len(mockT.Logs) == 0 { - t.Fatal("Assert should have emitted a failure log") - } - }) - - t.Run("should allow optional message with no format placeholders", func(t *testing.T) { +func TestCheckReturnValues(t *testing.T) { + t.Run("should return true on true condition", func(t *testing.T) { mockT := &MockT{} - failureLog := "my failure message" - Assert(mockT, false, failureLog) + passed := Check(mockT, true) - if !mockT.FailNowCalled { - t.Fatal("Assert passed when it should have failed") - } - if len(mockT.Logs) != 1 { - t.Fatalf("Assert should emit one failure log, got %d logs", len(mockT.Logs)) - } - if mockT.Logs[0] != failureLog { - t.Fatalf( - "Assert should emit custom message %s on failure, got %s", - failureLog, mockT.Logs[0], - ) + if !passed { + t.Fatal("Check should have returned true on true condition") } }) - t.Run("should allow optional message with format placeholders filled in", func(t *testing.T) { + t.Run("should return false on false condition", func(t *testing.T) { mockT := &MockT{} - Assert(mockT, false, "my failure message: %d", 5) - - if !mockT.FailNowCalled { - t.Fatal("Assert passed when it should have failed") - } - if len(mockT.Logs) != 1 { - t.Fatalf("Assert should emit one failure log, got %d logs", len(mockT.Logs)) - } - expectedFailureLog := "my failure message: 5" - if mockT.Logs[0] != expectedFailureLog { - t.Fatalf( - "Assert should emit custom message %s on failure, got %s", - expectedFailureLog, mockT.Logs[0], - ) - } - }) - - t.Run("should called Helper() when passed a helperT", func(t *testing.T) { - mockHelperT := &MockHelperT{} - - Assert(mockHelperT, true) + passed := Check(mockT, false) - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") + if passed { + t.Fatal("Check should have returned false on false condition") } }) } @@ -422,72 +282,6 @@ func TestAssertEqual(t *testing.T) { } } -func TestAssertEqualCallsHelper(t *testing.T) { - mockHelperT := &MockHelperT{} - - AssertEqual(mockHelperT, true, true) - - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") - } -} - -func TestAssertNotEqual(t *testing.T) { - t.Run("Equal", func(t *testing.T) { - mockT := &MockT{} - AssertNotEqual(mockT, 5, 5) - if !mockT.FailNowCalled { - t.Fatal("AssertNotEqual failed when it should have passed") - } - }) - - t.Run("Not equal", func(t *testing.T) { - mockT := &MockT{} - AssertNotEqual(mockT, 5, 10) - if mockT.FailNowCalled { - t.Fatal("AssertNotEqual passed when it should have failed") - } - }) - - t.Run("should called Helper() when passed a helperT", func(t *testing.T) { - mockHelperT := &MockHelperT{} - - AssertNotEqual(mockHelperT, true, false) - - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") - } - }) -} - -func TestAssertLenEqual(t *testing.T) { - t.Run("Equal lengths", func(t *testing.T) { - mockT := &MockT{} - AssertLenEqual(mockT, []int{1, 2, 3}, 3) - if mockT.FailNowCalled { - t.Error("AssertLenEqual failed when it should have passed") - } - }) - - t.Run("Lengths aren't equal", func(t *testing.T) { - mockT := &MockT{} - AssertLenEqual(mockT, []int{1, 2, 3}, 2) - if !mockT.FailNowCalled { - t.Error("AssertLenEqual passed when it should have failed") - } - }) - - t.Run("should called Helper() when passed a helperT", func(t *testing.T) { - mockHelperT := &MockHelperT{} - - AssertLenEqual(mockHelperT, []string{""}, 1) - - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") - } - }) -} - func TestAssertDeepEqual(t *testing.T) { type TestStruct struct { Name string @@ -512,16 +306,6 @@ func TestAssertDeepEqual(t *testing.T) { t.Error("AssertDeepEqual did not log the correct diff message") } }) - - t.Run("should called Helper() when passed a helperT", func(t *testing.T) { - mockHelperT := &MockHelperT{} - - AssertDeepEqual(mockHelperT, []string{""}, []string{""}) - - if !mockHelperT.HelperCalled { - t.Fatal("HelperT not called") - } - }) } func containsDiffMessage(log string) bool { |