-
Notifications
You must be signed in to change notification settings - Fork 67
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 assert_with_error! macro #749
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate as soroban_sdk; | ||
use soroban_sdk::{contractimpl, xdr::ScStatusType, Env, Status}; | ||
use soroban_sdk_macros::contracterror; | ||
|
||
pub struct Contract; | ||
|
||
#[contracterror] | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum Error { | ||
Zero = 1, | ||
} | ||
|
||
#[contractimpl] | ||
impl Contract { | ||
pub fn assert(env: Env, value: u32) -> u32 { | ||
assert_error!(&env, value > 0, Error::Zero); | ||
value | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Status(ContractError(1)")] | ||
fn test_invoke_expect_status() { | ||
let e = Env::default(); | ||
let contract_id = e.register_contract(None, Contract); | ||
|
||
ContractClient::new(&e, &contract_id).assert(&0); | ||
} | ||
|
||
#[test] | ||
fn test_try_invoke() { | ||
let e = Env::default(); | ||
let contract_id = e.register_contract(None, Contract); | ||
|
||
let res = ContractClient::new(&e, &contract_id).try_assert(&0); | ||
assert_eq!( | ||
res, | ||
Err(Ok(Status::from_type_and_code( | ||
ScStatusType::ContractError, | ||
1, | ||
))) | ||
); | ||
} |
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.
I chose the name
assert_error
because it parallelspanic_error
(which parallelspanic_any
). I don't love the nameassert_error
, because it reads more like we're asserting that an error should occur, rather than creating an assertion that will cause an error if false. I've gone with consistency, but open to suggestions.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 find it a little misleading. You are not asserting that there is an error, rather the opposite. For near's sdk, they went with
require
, which I would advocate for.Also I can't wait until macro's can be bound to types!
env.require!(...)
reads better to me.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.
Is this in development? Do you have a link to the tracking PR / RFC? I can't find it.
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.
It's called postfix macros polyfill here.
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.
@graydon suggested
assert_with_error
orassert_or_error
.On it's own I think I prefer
assert_or_error
overrequire
because it retains the assert vocab. i.e.:assert_or_error
panic_error
If we wanted panic_error to also follow, then the
_with_error
variant would probably fit better, i.e.:assert_with_error
panic_with_error
.Thoughts? cc @willemneal @graydon @paulbellamy
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.
*_with_error
makes sense to me.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.
Follow up: #752