cosmwasm_std/assertions.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
//! A module containing an assertion framework for CosmWasm contracts.
//! The methods in here never panic but return errors instead.
/// Quick check for a guard. If the condition (first argument) is false,
/// then return the second argument `x` wrapped in `Err(x)`.
///
/// ```
/// # enum ContractError {
/// # DelegatePerm {},
/// # }
/// #
/// # struct Permissions {
/// # delegate: bool,
/// # }
/// #
/// # fn body() -> Result<(), ContractError> {
/// # let permissions = Permissions { delegate: true };
/// use cosmwasm_std::ensure;
/// ensure!(permissions.delegate, ContractError::DelegatePerm {});
///
/// // is the same as
///
/// if !permissions.delegate {
/// return Err(ContractError::DelegatePerm {});
/// }
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! ensure {
($cond:expr, $e:expr) => {
if !($cond) {
return Err(core::convert::From::from($e));
}
};
}
/// Quick check for a guard. Like `assert_eq!`, but rather than panic,
/// it returns the third argument `x` wrapped in `Err(x)`.
///
/// ```
/// # use cosmwasm_std::{MessageInfo, Addr};
/// #
/// # enum ContractError {
/// # Unauthorized {},
/// # }
/// # struct Config {
/// # admin: Addr,
/// # }
/// #
/// # fn body() -> Result<(), ContractError> {
/// # let info = MessageInfo { sender: Addr::unchecked("foo"), funds: Vec::new() };
/// # let cfg = Config { admin: Addr::unchecked("foo") };
/// use cosmwasm_std::ensure_eq;
///
/// ensure_eq!(info.sender, cfg.admin, ContractError::Unauthorized {});
///
/// // is the same as
///
/// if info.sender != cfg.admin {
/// return Err(ContractError::Unauthorized {});
/// }
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! ensure_eq {
($a:expr, $b:expr, $e:expr) => {
// Not implemented via `ensure!` because the caller would have to import both macros.
if !($a == $b) {
return Err(core::convert::From::from($e));
}
};
}
/// Quick check for a guard. Like `assert_ne!`, but rather than panic,
/// it returns the third argument `x` wrapped in Err(x).
///
/// ```
/// # enum ContractError {
/// # NotAVoter {},
/// # }
/// #
/// # fn body() -> Result<(), ContractError> {
/// # let voting_power = 123;
/// use cosmwasm_std::ensure_ne;
///
/// ensure_ne!(voting_power, 0, ContractError::NotAVoter {});
///
/// // is the same as
///
/// if voting_power != 0 {
/// return Err(ContractError::NotAVoter {});
/// }
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! ensure_ne {
($a:expr, $b:expr, $e:expr) => {
// Not implemented via `ensure!` because the caller would have to import both macros.
if !($a != $b) {
return Err(core::convert::From::from($e));
}
};
}
#[cfg(test)]
mod tests {
use crate::StdError;
#[test]
fn ensure_works() {
fn check(a: usize, b: usize) -> Result<(), StdError> {
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 ensure_can_infer_error_type() {
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 ensure_can_convert_into() {
#[derive(Debug)]
struct ContractError;
impl From<StdError> for ContractError {
fn from(_original: StdError) -> Self {
ContractError
}
}
fn check(a: usize, b: usize) -> Result<(), ContractError> {
ensure!(a == b, StdError::generic_err("foobar"));
Ok(())
}
let err = check(5, 6).unwrap_err();
assert!(matches!(err, ContractError));
check(5, 5).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();
}
#[test]
fn ensure_eq_gets_precedence_right() {
// If this was expanded to `true || false == false` we'd get equality.
// It must be expanded to `(true || false) == false` and we expect inequality.
#[allow(clippy::nonminimal_bool)]
fn check() -> Result<(), StdError> {
ensure_eq!(true || false, false, StdError::generic_err("foobar"));
Ok(())
}
let _err = check().unwrap_err();
}
#[test]
fn ensure_ne_works() {
let check = |a, b| {
ensure_ne!(a, b, StdError::generic_err("foobar"));
Ok(())
};
let err = check("123", "123").unwrap_err();
assert!(matches!(err, StdError::GenericErr { .. }));
check("123", "456").unwrap();
}
#[test]
fn ensure_ne_gets_precedence_right() {
// If this was expanded to `true || false == false` we'd get equality.
// It must be expanded to `(true || false) == false` and we expect inequality.
#[allow(clippy::nonminimal_bool)]
fn check() -> Result<(), StdError> {
ensure_ne!(true || false, false, StdError::generic_err("foobar"));
Ok(())
}
check().unwrap();
}
}