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 | |
| parent | Add Check test helper (diff) | |
| download | checkmate-93e65834957f1da343cf43e1387b0411466af7d4.zip | |
Add working changes
| -rw-r--r-- | assert.go | 40 | ||||
| -rw-r--r-- | assert_test.go | 205 |
2 files changed, 223 insertions, 22 deletions
@@ -7,11 +7,14 @@ import ( ) // AssertDeepEqual 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. +// 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 AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { - t.Helper() + 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} @@ -24,7 +27,10 @@ func AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { // message and arguments for any format placeholders in that message // can be provided for if a failure occurs. func AssertEqual(t TestingT, actual, expected any, msgAndArgs ...any) { - t.Helper() + if ht, ok := t.(helperT); ok { + ht.Helper() + } + if actual != expected { if len(msgAndArgs) == 0 { msgAndArgs = []any{"expected %v to equal %v", expected, actual} @@ -37,7 +43,10 @@ func AssertEqual(t TestingT, actual, expected any, msgAndArgs ...any) { // the values are equal. An optional message and arguments for any format // placeholders in that message can be provided if a failure occurs. func AssertNotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { - t.Helper() + 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} @@ -51,7 +60,10 @@ func AssertNotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { // message and arguments for any format placeholders in that message can be provided // if a failure occurs. func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) { - t.Helper() + 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} } @@ -63,7 +75,9 @@ func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any // 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) { - t.Helper() + if ht, ok := t.(helperT); ok { + ht.Helper() + } if len(msgAndArgs) == 0 { msgAndArgs = []any{"assertion failed"} } @@ -80,7 +94,10 @@ func Assert(t TestingT, condition bool, msgAndArgs ...any) { // 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 { - t.Helper() + if ht, ok := t.(helperT); ok { + ht.Helper() + } + if !condition { message := "check failed" if len(msgAndArgs) > 0 { @@ -100,7 +117,10 @@ func Check(t TestingT, condition bool, msgAndArgs ...any) bool { // The subset of testing.T which is used by the // checkmate package. type TestingT interface { - Helper() Log(args ...any) FailNow() } + +type helperT interface { + Helper() +} 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 { |