From 43fe7364aa190e5d4f12eaeacc12aac4083ac00c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 15:32:46 -0500 Subject: Add AssertErrorIs and AssertErrorContains --- assert.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'assert.go') diff --git a/assert.go b/assert.go index 897d8c5..da05ffb 100644 --- a/assert.go +++ b/assert.go @@ -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 -- cgit v1.2.3