Skip to content

Commit

Permalink
Merge pull request #624 from peterstace/better_sentinel_error_checkin…
Browse files Browse the repository at this point in the history
…g_in_geos_tests

Use sentinel error rather than string matching in GEOS tests
  • Loading branch information
peterstace authored Jun 13, 2024
2 parents f5eca62 + c5063ff commit 4c85f2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
23 changes: 13 additions & 10 deletions geos/entrypoints_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package geos_test

import (
"errors"
"math"
"os"
"strconv"
"strings"
"testing"

"github.com/peterstace/simplefeatures/geom"
"github.com/peterstace/simplefeatures/geos"
"github.com/peterstace/simplefeatures/internal/rawgeos"
)

func geomFromWKT(t *testing.T, wkt string, nv ...geom.NoValidate) geom.Geometry {
Expand Down Expand Up @@ -48,6 +49,14 @@ func expectGeomEq(t *testing.T, got, want geom.Geometry, opts ...geom.ExactEqual
}
}

func skipIfUnsupported(tb testing.TB, err error) {
tb.Helper()
var verErr rawgeos.UnsupportedGEOSVersionError
if errors.As(err, &verErr) {
tb.Skipf(verErr.Error())
}
}

// These tests aren't exhaustive, because we are leveraging GEOS. The
// testing is just enough to make use confident that we're invoking GEOS
// correctly.
Expand Down Expand Up @@ -839,9 +848,7 @@ func TestMakeValid(t *testing.T) {
expectErr(t, err)
in := geomFromWKT(t, tt.input, geom.NoValidate{})
gotGeom, err := geos.MakeValid(in)
if err != nil && strings.Contains(err.Error(), "unsupported") {
t.Skip(err)
}
skipIfUnsupported(t, err)
expectNoErr(t, err)
wantGeom := geomFromWKT(t, tt.wantOutput)
expectGeomEq(t, gotGeom, wantGeom, geom.IgnoreOrder)
Expand Down Expand Up @@ -922,9 +929,7 @@ func TestCoverageUnion(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
in := geomFromWKT(t, tc.input)
gotGeom, err := geos.CoverageUnion(in)
if err != nil && strings.Contains(err.Error(), "unsupported") {
t.Skip(err)
}
skipIfUnsupported(t, err)
if tc.wantErr {
expectErr(t, err)
} else {
Expand All @@ -944,9 +949,7 @@ func TestCoverageSimplifyVW(t *testing.T) {
},
)
got, err := geos.CoverageSimplifyVW(input.AsGeometry(), 0.001, false)
if err != nil && strings.Contains(err.Error(), "unsupported") {
t.Skip(err)
}
skipIfUnsupported(t, err)
expectNoErr(t, err)
want := geomFromWKTFile(t, "testdata/coverage_simplify_output.wkt")
expectGeomEq(t, got, want)
Expand Down
8 changes: 4 additions & 4 deletions internal/rawgeos/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func SymmetricDifference(a, b geom.Geometry) (geom.Geometry, error) {

func MakeValid(g geom.Geometry) (geom.Geometry, error) {
if C.MAKE_VALID_MISSING != 0 {
return geom.Geometry{}, unsupportedGEOSVersionError{
return geom.Geometry{}, UnsupportedGEOSVersionError{
C.MAKE_VALID_MIN_VERSION, "MakeValid",
}
}
Expand All @@ -267,7 +267,7 @@ func MakeValid(g geom.Geometry) (geom.Geometry, error) {

func CoverageUnion(g geom.Geometry) (geom.Geometry, error) {
if C.COVERAGE_UNION_MISSING != 0 {
return geom.Geometry{}, unsupportedGEOSVersionError{
return geom.Geometry{}, UnsupportedGEOSVersionError{
C.COVERAGE_UNION_MIN_VERSION, "CoverageUnion",
}
}
Expand All @@ -279,7 +279,7 @@ func CoverageUnion(g geom.Geometry) (geom.Geometry, error) {

func CoverageSimplifyVW(g geom.Geometry, tolerance float64, preserveBoundary bool) (geom.Geometry, error) {
if C.COVERAGE_SIMPLIFY_VW_MISSING != 0 {
return geom.Geometry{}, unsupportedGEOSVersionError{
return geom.Geometry{}, UnsupportedGEOSVersionError{
C.COVERAGE_SIMPLIFY_VW_MIN_VERSION, "CoverageSimplifyVW",
}
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func ConvexHull(g geom.Geometry) (geom.Geometry, error) {

func ConcaveHull(g geom.Geometry, ratio float64, allowHoles bool) (geom.Geometry, error) {
if C.CONCAVE_HULL_MISSING != 0 {
return geom.Geometry{}, unsupportedGEOSVersionError{
return geom.Geometry{}, UnsupportedGEOSVersionError{
C.CONCAVE_HULL_MIN_VERSION, "ConcaveHull",
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/rawgeos/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var currentGEOSVersion = fmt.Sprintf(
C.GEOS_VERSION_PATCH,
)

type unsupportedGEOSVersionError struct {
type UnsupportedGEOSVersionError struct {
requiredGEOSVersion string
operation string
}

func (e unsupportedGEOSVersionError) Error() string {
func (e UnsupportedGEOSVersionError) Error() string {
return fmt.Sprintf("%s is unsupported in GEOS %s, requires at least GEOS %s",
e.operation, currentGEOSVersion, e.requiredGEOSVersion)
}

0 comments on commit 4c85f2d

Please sign in to comment.