Skip to content

test: Allow error-checking#157

Open
alarso16 wants to merge 2 commits intomainfrom
alarso16/rpc-test-err
Open

test: Allow error-checking#157
alarso16 wants to merge 2 commits intomainfrom
alarso16/rpc-test-err

Conversation

@alarso16
Copy link
Contributor

Credit to @powerslider for the implementation here, but thought we should pull it out from #154, since it can be used elsewhere, and can easily be isolated.

sae/rpc_test.go Outdated
t.Run(tc.method, func(t *testing.T) {
if tc.wantErr != "" {
err := call(t, nil, tc) // won't unmarshal anything
require.ErrorContains(t, err, tc.wantErr)
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 wish we could use require.ErrorIs, but almost all of these errors are from libevm

@alarso16 alarso16 marked this pull request as ready for review February 10, 2026 21:13
Copy link
Member

@JonathanOppenheimer JonathanOppenheimer left a comment

Choose a reason for hiding this comment

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

I am a big fan of this change and will use it in my PRs :)

sae/rpc_test.go Outdated
Comment on lines 72 to 75
if tc.want == nil {
require.NoError(t, call(t, nil, tc))
return
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This code doesn't seem to be used in this PR fyi

Copy link
Contributor Author

@alarso16 alarso16 Feb 11, 2026

Choose a reason for hiding this comment

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

Now it is! Kind of, it still doesn't unmarshal anything

sae/rpc_test.go Outdated
cmputils.TransactionsByHash(),
}

call := func(t *testing.T, store any, tc rpcTest) error {
Copy link
Collaborator

@ARR4N ARR4N Feb 11, 2026

Choose a reason for hiding this comment

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

I don't think it's necessary to pull this out and call it multiple times. It's possible to use a black-hole json.RawMessage when want == nil and to do an in-line error check.

About to do the school run and I'll send an example when I'm back.

EDIT: see #159

Copy link
Collaborator

@ARR4N ARR4N left a comment

Choose a reason for hiding this comment

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

Special-casing tests is generally a sign that either (a) the test cases should be separated into different tables; or (b) the test loop needs refactoring. Packages like errdiff were developed to avoid this, sometimes allowing the testing to keep going (like in #159) but sometimes only requiring the loop to end early when an error is expected.

@alarso16
Copy link
Contributor Author

Packages like errdiff were developed to avoid this, sometimes allowing the testing to keep going (like in #159) but sometimes only requiring the loop to end early when an error is expected.

I like the json.RawMessage cleanup, but errdiff doesn't necessarily seem like a helpful add. It does just more or less combine the functionality that I just put using require.

Also, is it idiomatic to check the return VALUE if an error is returned? I had learned through observation that it doesn't happen often, but that doesn't make it good practice

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.

5 participants