From 4381bee03524f4276986130cc2abf7503276b57a Mon Sep 17 00:00:00 2001 From: John Behm Date: Sun, 20 Aug 2023 21:45:17 +0200 Subject: [PATCH] add errorutils tests --- internal/errorutils/errorutils_test.go | 15 +++-- internal/errorutils/filter_test.go | 93 ++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 internal/errorutils/filter_test.go diff --git a/internal/errorutils/errorutils_test.go b/internal/errorutils/errorutils_test.go index 6b4e87aa..7745609e 100644 --- a/internal/errorutils/errorutils_test.go +++ b/internal/errorutils/errorutils_test.go @@ -10,16 +10,17 @@ import ( func TestUnwrapErrors(t *testing.T) { err := Join(errors.New("foo"), errors.New("bar")) + require.NotEmpty(t, err.Error()) - errs := Unwrap(err) + errs := ShallowUnwrap(err) require.Len(t, errs, 2) } func TestUnwrapError(t *testing.T) { err := fmt.Errorf("foo: %w", errors.New("bar")) - errs := Unwrap(err) - require.Len(t, errs, 2) + errs := ShallowUnwrap(err) + require.Len(t, errs, 1) } func TestUnwrapHierarchyError(t *testing.T) { @@ -31,11 +32,15 @@ func TestUnwrapHierarchyError(t *testing.T) { err := Join(Join(nil, err2), Join(nil, err4, nil)) - errs := Unwrap(err) - require.Len(t, errs, 4) + errs := ShallowUnwrap(err) + require.Len(t, errs, 2) } func TestJoinNils(t *testing.T) { err := Join(nil, nil) require.Nil(t, err) } + +func TestDeepMultiErrorUnwrapNil(t *testing.T) { + require.Nil(t, deepUnwrapMultiError(nil)) +} diff --git a/internal/errorutils/filter_test.go b/internal/errorutils/filter_test.go new file mode 100644 index 00000000..b53b67a8 --- /dev/null +++ b/internal/errorutils/filter_test.go @@ -0,0 +1,93 @@ +package errorutils + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +type customErr struct { + FieldValue string +} + +func (e *customErr) Error() string { + return e.FieldValue +} + +func shallowFilter(err error) bool { + _, ok := err.(*customErr) + return ok +} + +func deepFilter(err error) bool { + var cerr *customErr + return errors.As(err, &cerr) +} + +func removeAllFilter(err error) bool { + return false +} + +func TestFilter(t *testing.T) { + + errs := []error{ + &customErr{FieldValue: "foo"}, + fmt.Errorf("foo: %w", &customErr{FieldValue: "foo"}), + errors.New("bar"), + nil, + } + + filtered := Filter(errs, shallowFilter) + + require.Len(t, filtered, 1) + + for _, err := range filtered { + require.NotNil(t, err) + cerr, ok := err.(*customErr) + require.True(t, ok, "cannot type assert to customErr") + + require.NotEmpty(t, cerr.FieldValue) + } + + filtered = Filter(errs, deepFilter) + + require.Len(t, filtered, 2) +} + +func TestFilteredNormal(t *testing.T) { + errs := []error{ + &customErr{FieldValue: "foo"}, + fmt.Errorf("foo: %w", &customErr{FieldValue: "foo"}), + errors.New("bar"), + nil, + } + err := Join(errs...) + + filtered := Filtered(err, shallowFilter) + + require.Len(t, ShallowUnwrap(filtered), 1) + + filtered = Filtered(err, deepFilter) + require.Len(t, ShallowUnwrap(filtered), 2) +} + +func TestFilteredNil(t *testing.T) { + + filtered := Filtered(nil, shallowFilter) + require.Nil(t, filtered) +} + +func TestFilteredEmpty(t *testing.T) { + errs := []error{ + &customErr{FieldValue: "foo"}, + fmt.Errorf("foo: %w", &customErr{FieldValue: "foo"}), + errors.New("bar"), + nil, + } + err := Join(errs...) + + filtered := Filtered(err, removeAllFilter) + require.Len(t, ShallowUnwrap(filtered), 0) +}