-
Notifications
You must be signed in to change notification settings - Fork 76
Custom assertion methods return Result #848
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
Open
Abeeujah
wants to merge
13
commits into
0xSpaceShard:main
Choose a base branch
from
Abeeujah:custom-assert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
edf6f5d
dev: Return result instead of panicking in assert_tx_succeeded_accepted
Abeeujah 909b38a
Return result instead of panicking in assert_contains
Abeeujah c77afb1
Return result instead of panicking in assert_tx_reverted
Abeeujah 1f12195
Return result instead of panicking in assert_equal_elements
Abeeujah e37d93e
Return result instead of panicking in get_contract_balance, get_contr…
Abeeujah d104800
Return result instead of panicking in extract_message_error
Abeeujah e1d3bd5
Return result instead of panicking in extract_nested_error
Abeeujah dc4fa67
dev: Return result instead of panicking in assert_no_notifications
Abeeujah a9bd618
dev: Clean up match blocks in test for unwrap
Abeeujah f000fe6
dev: Routine check
Abeeujah 0102175
dev: review comments
Abeeujah e16a2b8
dev: Replace Panics in helper functions
Abeeujah ae93217
dev: Add context to anyhow's ensure macro
Abeeujah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,3 +54,5 @@ impl fmt::Display for RpcError { | |
write!(f, "{self:?}") | ||
} | ||
} | ||
|
||
impl std::error::Error for RpcError {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Problem
I was not aware that
anyhow::ensure
only accepts a boolean argument. This reduces the information in the reported error.assert_eq
nicely reports the failure telling us theleft
and theright
argument which don't match, but the stack trace is messy. Withanyhow::ensure
we get a nice stack trace, but lose the mismatched content.Suggestion
The best would be to have a custom equality assertion function that would only be used in these helper functions (basically the places where you've now configured
anyhow::ensure
). The function would accept two arguments implementing theDebug
trait, returnOk(())
if equal, or return anErr
containing info onleft
andright
, but propagating it. Though there might be issues when the two arguments are not of the same type, meaning it would require something like this:Let me know if you think this is too much of hassle and if it's even feasible.
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.
ensure!(user == 0, "only user 0 is allowed");
takes in an optional error message argument, we could use that, but if it don't report as much info as needed, I'd look into implementing your suggestion.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.
@FabijanC what do you think about this?
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.
Situations where we assert non-equality (i.e. x != y) or "greater-than" relation (i.e. x > y) are less problematic. Personally, I'd rather we kept using
assert_eq
thananyhow::ensure
and now I regret recommending it in the first place. The ideal solution would be to have a macro or a custom function that I proposed above, but I will understand if you don't want to implement one of those.So: let me know if you will just revert the anyhow::ensure changes or also implement a custom asserter. Sorry once again for the double work on your side.
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.
Okay, It's fine, my bad, I went with providing custom error messages, I'd implement the custom assert, and push.