diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 16:24:54 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 16:24:54 -0500 |
| commit | a74e84419015c14631913e854df6acd0c60a6f08 (patch) | |
| tree | 6b740aa633714bdd31a8eb38e614d4fa3f101bd2 /assert | |
| parent | Update version badge (diff) | |
| download | checkmate-a74e84419015c14631913e854df6acd0c60a6f08.zip | |
Move assert functions to assert packagev0.2.0
Diffstat (limited to 'assert')
| -rw-r--r-- | assert/assert.go | 227 | ||||
| -rw-r--r-- | assert/assert_test.go | 393 |
2 files changed, 620 insertions, 0 deletions
diff --git a/assert/assert.go b/assert/assert.go new file mode 100644 index 0000000..b120ec5 --- /dev/null +++ b/assert/assert.go @@ -0,0 +1,227 @@ +// Checkmate is a library designed to enhance the testing experience by providing a +// set of assertion functions. With Go, there is no built-in assertion library which +// can make writing tests feel more verbose than it needs to be, so Checkmate aims to +// fill that gap. +// +// This is a simple and small library. If you need something more advanced and full-featured, +// [Testify](https://github.com/stretchr/testify), [Gomega](https://github.com/onsi/gomega), or +// [gotest.tools](https://github.com/gotestyourself/gotest.tools) are all great options. +package assert + +import ( + "errors" + "fmt" + "strings" + + "github.com/google/go-cmp/cmp" +) + +// Nil checks whether the value equals nil. An optional +// message and arguments for any format placeholders in that +// message can be provided for if a failure occurs. +func Nil(t TestingT, value any, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to be nil, got %v", value} + } + + Assert(t, value == nil, msgAndArgs...) +} + +// NotNil checks whether the value does not equal nil. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func NotNil(t TestingT, value any, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to not be nil, got nil", value} + } + + Assert(t, value != nil, msgAndArgs...) +} + +// True checks whether the condition is true. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func True(t TestingT, condition bool, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be true, got false"} + } + + Assert(t, condition, msgAndArgs...) +} + +// False checks whether the condition is false. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func False(t TestingT, condition bool, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be false, got true"} + } + + Assert(t, !condition, msgAndArgs...) +} + +// ErrorIs checks whether any error in the actual error's tree +// matches the expected error. An optional message and arguments for +// any format placeholders in that message can be provided for if a +// failure occurs. +func ErrorIs(t TestingT, actual error, expected error, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected error %v to have error %v in its tree", actual, expected} + } + + Assert(t, errors.Is(actual, expected), msgAndArgs...) +} + +// ErrorContains checks whether the given err contains the expectedErrText +// in the err.Error() output. An optional message and arguments for any format +// placeholders in that message can be provided for if a failure occurs. +func ErrorContains(t TestingT, err error, expectedErrText string, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected err to contain %s, got %s", expectedErrText, err.Error()} + } + + Assert(t, strings.Contains(err.Error(), expectedErrText), msgAndArgs...) +} + +// DeepEqual 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 DeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { + 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} + } + Assert(t, false, msgAndArgs...) + } +} + +// Equal 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 Equal(t TestingT, actual, expected any, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if actual != expected { + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected %v to equal %v", expected, actual} + } + Assert(t, false, msgAndArgs...) + } +} + +// NotEqual 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 NotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { + 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} + } + Assert(t, false, msgAndArgs...) + } +} + +// LenEqual 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 LenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) { + 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} + } + 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) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"assertion failed"} + } + passed := Check(t, condition, msgAndArgs...) + if !passed { + t.FailNow() + } +} + +// Check evaluates a boolean condition and logs a message if the condition is false. +// Unlike Assert, it does not stop the test when the condition is false but returns +// a boolean indicating the result. This function is useful for cases where you want +// to perform multiple checks in a single test function without stopping the test +// 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 { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if !condition { + message := "check failed" + if len(msgAndArgs) > 0 { + switch msgAndArgs[0].(type) { + case string: + message = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) + default: + t.Log("checkmate: assertion called with a non-string message, using default message") + } + } + t.Log(message) + return false + } + return true +} + +// The subset of testing.T which is used by the +// checkmate package. +type TestingT interface { + Log(args ...any) + FailNow() +} + +type helperT interface { + Helper() +} diff --git a/assert/assert_test.go b/assert/assert_test.go new file mode 100644 index 0000000..a21f00c --- /dev/null +++ b/assert/assert_test.go @@ -0,0 +1,393 @@ +package assert + +import ( + "errors" + "fmt" + "os" + "strings" + "testing" +) + +// MockT is a mock structure for capturing test outputs. +type MockT struct { + TestingT + + FailNowCalled bool + Logs []string +} + +func (m *MockT) FailNow() { + m.FailNowCalled = true +} + +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 +} + +// 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 { + Equal(t, args[0], args[1], args[2:]...) + + } else { + Equal(t, args[0], args[1]) + } +} + +func wrapAssertDeepEqual(t TestingT, args []any) { + if len(args) > 2 { + DeepEqual(t, args[0], args[1], args[2:]...) + + } else { + DeepEqual(t, args[0], args[1]) + } +} + +func wrapAssertNotEqual(t TestingT, args []any) { + if len(args) > 2 { + NotEqual(t, args[0], args[1], args[2:]...) + + } else { + NotEqual(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 { + LenEqual(t, slice, expectedLen, args[2:]...) + + } else { + LenEqual(t, slice, expectedLen) + } +} + +func wrapAssertErrorIs(t TestingT, args []any) { + if len(args) > 2 { + ErrorIs(t, args[0].(error), args[1].(error), args[2:]...) + + } else { + ErrorIs(t, args[0].(error), args[1].(error)) + } +} + +func wrapAssertErrorContains(t TestingT, args []any) { + if len(args) > 2 { + ErrorContains(t, args[0].(error), args[1].(string), args[2:]...) + + } else { + ErrorContains(t, args[0].(error), args[1].(string)) + } +} + +func wrapAssertTrue(t TestingT, args []any) { + if len(args) > 1 { + True(t, args[0].(bool), args[1:]...) + + } else { + True(t, args[0].(bool)) + } +} + +func wrapAssertFalse(t TestingT, args []any) { + if len(args) > 1 { + False(t, args[0].(bool), args[1:]...) + + } else { + False(t, args[0].(bool)) + } +} + +func wrapAssertNil(t TestingT, args []any) { + if len(args) > 1 { + Nil(t, args[0], args[1:]...) + + } else { + Nil(t, args[0]) + } +} + +func wrapAssertNotNil(t TestingT, args []any) { + if len(args) > 1 { + NotNil(t, args[0], args[1:]...) + + } else { + NotNil(t, args[0]) + } +} + +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}}, + {"AssertErrorIs", wrapAssertErrorIs, []any{os.ErrExist, os.ErrExist}}, + {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"AssertTrue", wrapAssertTrue, []any{true}}, + {"AssertFalse", wrapAssertFalse, []any{false}}, + {"AssertNil", wrapAssertNil, []any{nil}}, + {"AssertNotNil", wrapAssertNotNil, []any{1}}, +} + +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}}, + {"AssertErrorIs", wrapAssertErrorIs, []any{errors.New("error 1"), errors.New("error 2")}}, + {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"AssertTrue", wrapAssertTrue, []any{false}}, + {"AssertFalse", wrapAssertFalse, []any{true}}, + {"AssertNil", wrapAssertNil, []any{1}}, + {"AssertNotNil", wrapAssertNotNil, []any{nil}}, +} + +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 TestAssertFnsShouldNotFailOrEmitLogsOnSuccess(t *testing.T) { + for _, testFn := range passingTestFns { + t.Run(testFn.name, func(t *testing.T) { + mockT := &MockT{} + testFn.assertionFn(mockT, testFn.args) + + if mockT.FailNowCalled { + t.Fatalf( + "%s: fail now called, expected the test to not be failed on true condition", + testFn.name, + ) + } + if len(mockT.Logs) > 0 { + t.Fatalf( + "%s: %v logs emitted, expected the test to not emit any logs on true condition", + testFn.name, mockT.Logs, + ) + } + }) + } +} + +func TestCheckReturnValues(t *testing.T) { + t.Run("should return true 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") + } + }) + + t.Run("should return false 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") + } + }) +} + +func TestAssertEqual(t *testing.T) { + 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"}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + mockT := &MockT{} + Equal(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], + ) + } + } + } + }) + } +} + +func TestAssertDeepEqual(t *testing.T) { + type TestStruct struct { + Name string + Age int + } + + t.Run("Equal structs", func(t *testing.T) { + mockT := &MockT{} + DeepEqual(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{} + DeepEqual(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)") +} + +func TestAssertErrorIsChecksErrTree(t *testing.T) { + mockT := &MockT{} + actual := fmt.Errorf("wrapped: %w", os.ErrInvalid) + expected := os.ErrInvalid + + ErrorIs(mockT, actual, expected) + + if mockT.FailNowCalled { + t.Fatal("AssertErrorIs failed when it should have passed") + } +} |