diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 09:44:23 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 09:44:23 -0500 |
| commit | 93e65834957f1da343cf43e1387b0411466af7d4 (patch) | |
| tree | 88337fba2241d31c5658483a692ba7fa8196080a /assert_test.go | |
| parent | Add Check test helper (diff) | |
| download | checkmate-93e65834957f1da343cf43e1387b0411466af7d4.zip | |
Add working changes
Diffstat (limited to 'assert_test.go')
| -rw-r--r-- | assert_test.go | 205 |
1 files changed, 193 insertions, 12 deletions
diff --git a/assert_test.go b/assert_test.go index 59645a6..277bfb5 100644 --- a/assert_test.go +++ b/assert_test.go @@ -6,19 +6,14 @@ import ( "testing" ) -// MockT is a mock testing.T structure for capturing test outputs. +// MockT is a mock structure for capturing test outputs. type MockT struct { TestingT - HelperCalled bool FailNowCalled bool Logs []string } -func (m *MockT) Helper() { - m.HelperCalled = true -} - func (m *MockT) FailNow() { m.FailNowCalled = true } @@ -27,10 +22,26 @@ 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 +} + func TestCheck(t *testing.T) { - t.Run("True", func(t *testing.T) { + t.Run("should not fail or emit any logs on true condition", func(t *testing.T) { mockT := &MockT{} - Check(mockT, true) + + 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") } @@ -39,9 +50,14 @@ func TestCheck(t *testing.T) { } }) - t.Run("False", func(t *testing.T) { + t.Run("should not fail but it should emit a log on false condition", func(t *testing.T) { mockT := &MockT{} - Check(mockT, false) + + 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") } @@ -49,20 +65,95 @@ func TestCheck(t *testing.T) { 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) { + mockT := &MockT{} + + 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 { + t.Fatalf( + "Check should emit warning message %s on wrong type for message, got %s", + expectedWarningLog, mockT.Logs[0], + ) + } + expectedFailureLog := "check failed" + if mockT.Logs[1] != expectedFailureLog { + t.Fatalf( + "Check should emit default failure message %s on wrong type for message, got %s", + expectedFailureLog, mockT.Logs[0], + ) + } + }) + + 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("True", func(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("False", func(t *testing.T) { + 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") } @@ -70,6 +161,56 @@ func TestAssert(t *testing.T) { t.Fatal("Assert should have emitted a failure log") } }) + + t.Run("should allow optional message with no format placeholders", func(t *testing.T) { + mockT := &MockT{} + failureLog := "my failure message" + + Assert(mockT, false, failureLog) + + 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], + ) + } + }) + + t.Run("should allow optional message with format placeholders filled in", 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) + + if !mockHelperT.HelperCalled { + t.Fatal("HelperT not called") + } + }) } func TestAssertEqual(t *testing.T) { @@ -88,6 +229,16 @@ func TestAssertEqual(t *testing.T) { t.Fatal("AssertEqual passed when it should have failed") } }) + + t.Run("should called Helper() when passed a helperT", func(t *testing.T) { + mockHelperT := &MockHelperT{} + + AssertEqual(mockHelperT, true, true) + + if !mockHelperT.HelperCalled { + t.Fatal("HelperT not called") + } + }) } func TestAssertNotEqual(t *testing.T) { @@ -106,6 +257,16 @@ func TestAssertNotEqual(t *testing.T) { 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) { @@ -124,6 +285,16 @@ func TestAssertLenEqual(t *testing.T) { 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) { @@ -150,6 +321,16 @@ 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 { |