Skip to content

Commit

Permalink
Better error assertions in tests
Browse files Browse the repository at this point in the history
This change introduces better error assertions, prompted by a comment
from Albert Teoh here:
#615 (comment)

The change introduces two new test helper functions:

- expectErrIs(tb testing.TB, err, want error)

- expectErrAs(tb testing.TB, err error, target interface{})

These wrap `errors.Is` and `errors.As` functions from the standard
library, respectively.

In order for this to work, the `mismatchedGeometryCollectionDimsError`
must be exported, but _only during tests_ (because we don't want to
expose it to users of the library). This is achieved by using type
aliases in a `_test.go` file in the `geom` package. Because `_test.go`
files are only included during tests, this is a safe way to expose the
type only during tests.
  • Loading branch information
peterstace committed May 15, 2024
1 parent d1a3d0b commit 36542f9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions geom/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func wrapWithGeoJSONSyntaxError(err error) error {
}

type mismatchedGeometryCollectionDimsError struct {
ct1, ct2 CoordinatesType
CT1, CT2 CoordinatesType
}

func (e mismatchedGeometryCollectionDimsError) Error() string {
return fmt.Sprintf("mixed dimensions in geometry collection: %s and %s", e.ct1, e.ct2)
return fmt.Sprintf("mixed dimensions in geometry collection: %s and %s", e.CT1, e.CT2)
}

type ruleViolation string
Expand Down
3 changes: 3 additions & 0 deletions geom/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package geom

type MismatchedGeometryCollectionDimsError = mismatchedGeometryCollectionDimsError
17 changes: 17 additions & 0 deletions geom/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package geom_test

import (
"bytes"
"errors"
"math"
"reflect"
"strconv"
Expand Down Expand Up @@ -97,6 +98,21 @@ func expectErr(tb testing.TB, err error) {
}
}

func expectErrIs(tb testing.TB, err, want error) {
tb.Helper()
if !errors.Is(err, want) {
tb.Errorf("\ngot: %v\nwant: %v\n", err, want)
}
}

func expectErrAs(tb testing.TB, err error, target interface{}) {
tb.Helper()
if !errors.As(err, target) {
targetType := reflect.ValueOf(target).Elem().Interface()
tb.Errorf("%#v not assignable after unwrapping to %T", err, targetType)
}
}

func expectDeepEq(tb testing.TB, got, want interface{}) {
tb.Helper()
if !reflect.DeepEqual(got, want) {
Expand Down Expand Up @@ -156,6 +172,7 @@ func expectStringEq(tb testing.TB, got, want string) {
}
}

//nolint:unused
func expectSubstring(tb testing.TB, got, wantSubstring string) {
tb.Helper()
if !strings.Contains(got, wantSubstring) {
Expand Down
3 changes: 2 additions & 1 deletion geom/wkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ func TestWKBGeometryCollectionMixedCoordinateTypes(t *testing.T) {
if gc.ct == point.ct {
expectNoErr(t, err)
} else {
expectErr(t, err)
wantErr := geom.MismatchedGeometryCollectionDimsError{gc.ct, point.ct}
expectErrIs(t, err, wantErr)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions geom/wkt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ func TestInconsistentDimensionTypeInWKT(t *testing.T) {
expectNoErr(t, err)
return
}
expectErr(t, err)
expectSubstring(t, err.Error(), "mixed dimensions in geometry collection")
want := geom.MismatchedGeometryCollectionDimsError{}
expectErrAs(t, err, &want)
})
}
}

0 comments on commit 36542f9

Please sign in to comment.