-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better error assertions in tests #616
Conversation
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.
@@ -59,11 +59,11 @@ func wrapWithGeoJSONSyntaxError(err error) error { | |||
} | |||
|
|||
type mismatchedGeometryCollectionDimsError struct { | |||
ct1, ct2 CoordinatesType | |||
CT1, CT2 CoordinatesType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be exported, otherwise they can't be accessed from the geom_test
package. Because mismatchedGeometryCollectionDimsError
isn't exported (except during tests), having CT1
and CT2
exported doesn't make any difference for users of the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing! 👍🏼
@@ -0,0 +1,3 @@ | |||
package geom | |||
|
|||
type MismatchedGeometryCollectionDimsError = mismatchedGeometryCollectionDimsError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wizard! TIL something new about Go 🙏🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only learned about this yesterday 😁
Thanks for reviewing! |
This is a follow-on from #616. It adds additional improvements to error assertions during testing, including: - Asserting on GeoJSON unmarshalling target mismatch errors (when GeoJSON is unmarshalled into the wrong type). - Asserting on validation errors (with assertions that the expected validation rule violation occurred).
Description
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
anderrors.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 thegeom
package. Because_test.go
files are only included during tests, this is a safe way to expose the type only during tests.Check List
Have you:
Added unit tests? Yes.
Add cmprefimpl tests? (if appropriate?) N/A
Updated release notes? (if appropriate?) N/A (internal change, so probably doesn't need them).
Related Issue