-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from CosmWasm/add-ensure
Add ensure
- Loading branch information
Showing
5 changed files
with
123 additions
and
39 deletions.
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod balance; | ||
mod event; | ||
mod expiration; | ||
mod macros; | ||
mod pagination; | ||
mod payment; | ||
|
||
|
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,94 @@ | ||
/// Quick check for a guard. If the condition (first argument) is false, | ||
/// then return the second argument wrapped in Err(x). | ||
/// | ||
/// ensure!(permissions.delegate, ContractError::DelegatePerm {}); | ||
/// is the same as | ||
/// if !permissions.delegate { | ||
/// return Err(ContractError::DelegatePerm {}); | ||
/// } | ||
#[macro_export] | ||
macro_rules! ensure { | ||
($cond:expr, $e:expr) => { | ||
if !($cond) { | ||
return Err($e); | ||
} | ||
}; | ||
} | ||
|
||
/// Opposite of ensure. If the condition (first argument) is true, | ||
/// then return the second argument wrapped in Err(x). | ||
/// | ||
/// fail_if!(!permissions.delegate, ContractError::DelegatePerm {}); | ||
/// is the same as | ||
/// if !permissions.delegate { | ||
/// return Err(ContractError::DelegatePerm {}); | ||
/// } | ||
#[macro_export] | ||
macro_rules! fail_if { | ||
($cond:expr, $e:expr) => { | ||
if ($cond) { | ||
return Err($e); | ||
} | ||
}; | ||
} | ||
|
||
/// Quick check for a guard. Like assert_eq!, but rather than panic, | ||
/// it returns the second argument wrapped in Err(x). | ||
/// | ||
/// ensure_eq!(info.sender, cfg.admin, ContractError::Unauthorized {}); | ||
/// is the same as | ||
/// if info.sender != cfg.admin { | ||
/// return Err(ContractError::Unauthorized {}); | ||
/// } | ||
#[macro_export] | ||
macro_rules! ensure_eq { | ||
($a:expr, $b:expr, $e:expr) => { | ||
if $a != $b { | ||
return Err($e); | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use cosmwasm_std::StdError; | ||
|
||
#[test] | ||
fn ensure_works() { | ||
let check = |a, b| { | ||
ensure!(a == b, StdError::generic_err("foobar")); | ||
Ok(()) | ||
}; | ||
|
||
let err = check(5, 6).unwrap_err(); | ||
assert!(matches!(err, StdError::GenericErr { .. })); | ||
|
||
check(5, 5).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn fail_if_works() { | ||
let check = |a, b| { | ||
fail_if!(a == b, StdError::generic_err("failure")); | ||
Ok(()) | ||
}; | ||
|
||
let err = check(5, 5).unwrap_err(); | ||
assert!(matches!(err, StdError::GenericErr { .. })); | ||
|
||
check(5, 6).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn ensure_eq_works() { | ||
let check = |a, b| { | ||
ensure_eq!(a, b, StdError::generic_err("foobar")); | ||
Ok(()) | ||
}; | ||
|
||
let err = check("123", "456").unwrap_err(); | ||
assert!(matches!(err, StdError::GenericErr { .. })); | ||
|
||
check("123", "123").unwrap(); | ||
} | ||
} |