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

Add "must" functions for tersely asserting function success #2

Merged
merged 2 commits into from
Oct 8, 2024

Conversation

jonathansharman
Copy link
Contributor

@jonathansharman jonathansharman commented Oct 8, 2024

Dependencies

None.

Documentation

Inspired by test utilities such as this one in a go-common branch.

Description

This adds a family of four new functions: Must0, Must, Must2, and Must3 (named in the spirit of the iter types). These functions take some number of values and an error, and return a function that when called with a *testing.T either returns the values or fails immediately, if the error is non-nil. Currying the *testing.T argument allows functions with multiple return values to be invoked directly within the Must* call, without assigning them to intermediate values first. The _must function these are based on panics, but I think failing using Fatalf can produce nicer error messages.

Versioning

Minor. Note that this contains a small breaking change since I added a method (Fatalf) to the expect.T interface. However, expect is still v0, and looking at how expect.T is used in our code, there shouldn't be any breakage anyway.

Comment on lines +156 to +158
// Must0 is similar to Must but for functions returning just an error, without a
// value.
func Must0(err error) func(T) {
Copy link
Contributor Author

@jonathansharman jonathansharman Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function wouldn't have to be curried since a unary function call can be passed to another function directly even when there are other parameters. However, I opted to follow the pattern of the other functions for consistency.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely took me a minute to really understand why we're returning an unary function of T. But now I get it. I kinda long for a type-safe arg spread operator. this is a reasonably un-awkward solution.

Comment on lines +178 to +180
// Must3 is similar to Must but for functions returning three values and an
// error.
func Must3[V1 any, V2 any, V3 any](value1 V1, value2 V2, value3 V3, err error) func(T) (V1, V2, V3) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to draw the line somewhere, and I stopped at three return values (plus the error). Anything over one or maybe two non-error return values is arguably already excessive.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable - though we could go-generate MustN if we had to

@jonathansharman jonathansharman marked this pull request as ready for review October 8, 2024 13:44
@wmarshall wmarshall self-requested a review October 8, 2024 18:17
Copy link

@wmarshall wmarshall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:noice: - I see my strategy of annoying folks with my hacks until they standardize them works

Comment on lines +156 to +158
// Must0 is similar to Must but for functions returning just an error, without a
// value.
func Must0(err error) func(T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely took me a minute to really understand why we're returning an unary function of T. But now I get it. I kinda long for a type-safe arg spread operator. this is a reasonably un-awkward solution.

@jonathansharman jonathansharman merged commit 6d71217 into main Oct 8, 2024
6 checks passed
@jonathansharman jonathansharman deleted the feature/must branch October 8, 2024 18:54
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.

2 participants