Skip to content
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

Merged
merged 1 commit into from
May 16, 2024
Merged

Conversation

peterstace
Copy link
Owner

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 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.

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

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
Copy link
Owner Author

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.

Copy link
Collaborator

@albertteoh albertteoh left a 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
Copy link
Collaborator

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 🙏🏼

Copy link
Owner Author

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 😁

@peterstace
Copy link
Owner Author

Thanks for reviewing!

@peterstace peterstace merged commit 86598d2 into master May 16, 2024
1 check passed
@peterstace peterstace deleted the better_error_assertions branch May 16, 2024 19:59
peterstace added a commit that referenced this pull request May 20, 2024
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants