#[macro_export]
macro_rules! ensure {
($cond:expr, $e:expr $(,)?) => {
if !$cond {
return Err($e);
}
};
}
#[macro_export]
macro_rules! assert_ok {
( $x:expr ) => {
match $x {
Ok(v) => v,
Err(e) => {
panic!("Error calling {}: {:?}", stringify!($x), e);
}
}
};
}
#[macro_export]
macro_rules! assert_err {
( $x:expr, $expected:pat ) => {
match $x {
Err(e) => {
if !matches!(e, $expected) {
panic!("Expected error {}: {:?}", stringify!($expected), e);
}
}
Ok(v) => {
panic!("Expected error {}, found {:?}", stringify!($expected), v)
}
}
};
}
#[macro_export]
macro_rules! assert_contract_err {
($given:expr, $expected:expr) => {
match $given {
Ok(v) => panic!(
"Expected error {:?}, got {:?} instead",
stringify!($expected),
v
),
Err(e) => match e {
Err(e) => panic!("Unexpected error {e:?}"),
Ok(v) if v != $expected => {
panic!("Expected error {:?}, got {:?} instead", $expected, v)
}
_ => (),
},
}
};
}
#[macro_export]
macro_rules! assert_some {
( $x:expr ) => {
match $x {
core::option::Option::Some(s) => s,
core::option::Option::None => {
panic!("Expected value when calling {}, got None", stringify!($x));
}
}
};
}
#[macro_export]
macro_rules! assert_auth {
($caller:expr, $client:ident . $method:ident ( $($arg:expr),* $(,)? )) => {{
use stellar_axelar_std::IntoVal;
let caller = $caller.clone();
paste::paste! {
let result = $client
.mock_auths(&[$crate::mock_auth!(
caller,
$client.$method($($arg),*)
)])
.[<try_ $method>]($($arg),*);
}
let result = match result {
Ok(outer) => {
match outer {
Ok(inner) => inner,
Err(err) => panic!("Expected Ok result, but got an error {:?}", err),
}
}
Err(err) => panic!("Expected Ok result, but got an error {:?}", err),
};
assert_eq!(
$client.env.auths(),
std::vec![(
caller,
stellar_axelar_std::testutils::AuthorizedInvocation {
function: stellar_axelar_std::testutils::AuthorizedFunction::Contract((
$client.address.clone(),
stellar_axelar_std::Symbol::new(&$client.env, stringify!($method)),
($($arg.clone(),)*).into_val(&$client.env)
)),
sub_invocations: std::vec![]
}
)]
);
result
}};
}
#[macro_export]
macro_rules! assert_auth_err {
($caller:expr, $client:ident . $method:ident ( $($arg:expr),* $(,)? )) => {{
use stellar_axelar_std::xdr::{ScError, ScErrorCode, ScVal};
let caller = $caller.clone();
paste::paste! {
let call_result = $client
.mock_auths(&[$crate::mock_auth!(
caller,
$client.$method($($arg),*)
)])
.[<try_ $method>]($($arg),*);
}
match call_result {
Err(_) => {
let val = ScVal::Error(ScError::Context(ScErrorCode::InvalidAction));
match ScError::try_from(val) {
Ok(ScError::Context(ScErrorCode::InvalidAction)) => {}
_ => panic!("Expected ScErrorCode::InvalidAction"),
}
}
Ok(_) => panic!("Expected error, but got Ok result."),
}
}};
}
#[macro_export]
macro_rules! mock_auth {
(
$caller:expr,
$client:ident . $method:ident ( $($arg:expr),* $(,)? ),
$sub_invokes:expr
) => {{
use stellar_axelar_std::IntoVal;
stellar_axelar_std::testutils::MockAuth {
address: &$caller,
invoke: &stellar_axelar_std::testutils::MockAuthInvoke {
contract: &$client.address,
fn_name: &stringify!($method).replace("try_", ""),
args: ($($arg.clone(),)*).into_val(&$client.env),
sub_invokes: $sub_invokes,
},
}
}};
(
$caller:expr,
$client:ident . $method:ident ( $($arg:expr),* $(,)? )
) => {{
$crate::mock_auth!($caller, $client.$method($($arg),*), &[])
}};
}