From efd0607baf4621aabc4cbf4b1fd12c703634275b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 27 Jan 2024 21:58:54 -0500 Subject: Initial Commit --- assert/assert.go | 88 ++++++++++++++++++++++++++++++++++ assert/assert_test.go | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 assert/assert.go create mode 100644 assert/assert_test.go (limited to 'assert') diff --git a/assert/assert.go b/assert/assert.go new file mode 100644 index 0000000..e2dd404 --- /dev/null +++ b/assert/assert.go @@ -0,0 +1,88 @@ +package assert + +import ( + "fmt" + + "github.com/google/go-cmp/cmp" +) + +// 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. +func AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { + t.Helper() + if diff := cmp.Diff(expected, actual); diff != "" { + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"mismatch (-expected +actual):\n%s", diff} + } + Assert(t, false, msgAndArgs...) + } +} + +// AssertEqual checks if two primitive values are equal. An optional +// 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 actual != expected { + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to equal %v", expected, actual} + } + Assert(t, false, msgAndArgs...) + } +} + +// AssertNotEqual checks if two values are not equal. It fails the test if +// 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 actual == expected { + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to not equal %v", expected, actual} + } + Assert(t, false, msgAndArgs...) + } +} + +// AssertLenEqual checks if the length of a given slice or array equals the +// expected length. It fails the test if the lengths are not equal. An optional +// 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 len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to have len %d, got len %d", l, len(l), expectedLen} + } + Assert(t, len(l) == expectedLen, msgAndArgs...) +} + +// Assert checks a boolean condition and fails the test if the condition is false. +// This is a general-purpose assertion function used to validate if a condition holds true. +// 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 !condition { + message := "assertion failed" + if len(msgAndArgs) > 0 { + switch msgAndArgs[0].(type) { + case string: + message = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) + default: + t.Log("checkmate: Assert called with a non-string message, using default message") + } + } + t.Log(message) + t.FailNow() + } +} + +// The subset of testing.T which is used by the +// checkmate package. +type TestingT interface { + Helper() + Log(args ...any) + FailNow() +} diff --git a/assert/assert_test.go b/assert/assert_test.go new file mode 100644 index 0000000..be3a9ab --- /dev/null +++ b/assert/assert_test.go @@ -0,0 +1,130 @@ +package assert + +import ( + "fmt" + "strings" + "testing" +) + +// MockT is a mock testing.T 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 +} + +func (m *MockT) Log(args ...any) { + m.Logs = append(m.Logs, fmt.Sprint(args...)) +} + +func TestAssert(t *testing.T) { + t.Run("True", func(t *testing.T) { + mockT := &MockT{} + Assert(mockT, true) + if mockT.FailNowCalled { + t.Fatal("Assert failed when it should have passed") + } + }) + + t.Run("False", func(t *testing.T) { + mockT := &MockT{} + Assert(mockT, false) + if !mockT.FailNowCalled { + t.Fatal("Assert passed when it should have failed") + } + }) +} + +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") + } + }) + + 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") + } + }) +} + +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") + } + }) +} + +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") + } + }) +} + +func TestAssertDeepEqual(t *testing.T) { + type TestStruct struct { + Name string + Age int + } + + t.Run("Equal structs", func(t *testing.T) { + mockT := &MockT{} + AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30}) + if mockT.FailNowCalled { + t.Error("AssertDeepEqual failed when it should have passed") + } + }) + + t.Run("Not equal structs", func(t *testing.T) { + mockT := &MockT{} + AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30}) + if !mockT.FailNowCalled { + t.Error("AssertDeepEqual passed when it should have failed") + } + if len(mockT.Logs) == 0 || !containsDiffMessage(mockT.Logs[0]) { + t.Error("AssertDeepEqual did not log the correct diff message") + } + }) +} + +func containsDiffMessage(log string) bool { + return strings.Contains(log, "(-expected +actual)") +} -- cgit v1.2.3