diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 15:32:46 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 15:32:46 -0500 |
| commit | 43fe7364aa190e5d4f12eaeacc12aac4083ac00c (patch) | |
| tree | 48fae540f2bfb7d1ca834faa898d4f39d3361828 | |
| parent | Fix badge image link (diff) | |
| download | checkmate-43fe7364aa190e5d4f12eaeacc12aac4083ac00c.zip | |
Add AssertErrorIs and AssertErrorContains
| -rw-r--r-- | assert.go | 41 | ||||
| -rw-r--r-- | assert_test.go | 36 |
2 files changed, 77 insertions, 0 deletions
@@ -1,11 +1,52 @@ +// 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 checkmate import ( + "errors" "fmt" + "strings" "github.com/google/go-cmp/cmp" ) +// AssertErrorIs 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 AssertErrorIs(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...) +} + +// AssertErrorContains 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 AssertErrorContains(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...) +} + // 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 diff --git a/assert_test.go b/assert_test.go index 7a1a1c5..84a17da 100644 --- a/assert_test.go +++ b/assert_test.go @@ -1,7 +1,9 @@ package checkmate import ( + "errors" "fmt" + "os" "strings" "testing" ) @@ -101,6 +103,24 @@ func wrapAssertLenEqualInt(t TestingT, args []any) { } } +func wrapAssertErrorIs(t TestingT, args []any) { + if len(args) > 2 { + AssertErrorIs(t, args[0].(error), args[1].(error), args[2:]...) + + } else { + AssertErrorIs(t, args[0].(error), args[1].(error)) + } +} + +func wrapAssertErrorContains(t TestingT, args []any) { + if len(args) > 2 { + AssertErrorContains(t, args[0].(error), args[1].(string), args[2:]...) + + } else { + AssertErrorContains(t, args[0].(error), args[1].(string)) + } +} + var passingTestFns = []struct { name string assertionFn assertFn @@ -112,6 +132,8 @@ var passingTestFns = []struct { {"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"}}, } var failingTestFns = []struct { @@ -125,6 +147,8 @@ var failingTestFns = []struct { {"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"}}, } func TestOptionalMessageAndArgs(t *testing.T) { @@ -311,3 +335,15 @@ func TestAssertDeepEqual(t *testing.T) { 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 + + AssertErrorIs(mockT, actual, expected) + + if mockT.FailNowCalled { + t.Fatal("AssertErrorIs failed when it should have passed") + } +} |