-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
feat: assert last event
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use std::fmt::Debug; | ||
|
||
use drink::{session::Session, Sandbox}; | ||
use scale::{Decode, Encode}; | ||
|
||
use crate::last_contract_event; | ||
|
||
/// Asserts that a result matches an expected `Error`. | ||
/// | ||
/// This can be used to assert that a contract execution resulted in a specific runtime error | ||
/// `Error`. The contract error must be convertible to a `u32` (i.e. the status code received from | ||
/// the api). | ||
/// | ||
/// # Example | ||
/// | ||
/// ## Errors | ||
/// | ||
/// ```rs | ||
/// use drink::devnet::{ | ||
/// Assets, | ||
Check warning on line 20 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// AssetsError::BalanceLow, | ||
Check warning on line 21 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// v0::{ | ||
Check warning on line 22 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// Arithmetic, | ||
Check warning on line 23 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ArithmeticError::Overflow, | ||
Check warning on line 24 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// BadOrigin | ||
Check warning on line 25 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// }, | ||
Check warning on line 26 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// }; | ||
/// ``` | ||
/// | ||
/// [`BadOrigin`](https://github.com/r0gue-io/pop-node/blob/main/primitives/src/lib.rs#L36C4-L36C18): | ||
/// ```rs | ||
/// Error::Raw(BadOrigin) | ||
/// ``` | ||
/// | ||
/// [`Arithmetic(Overflow)`](https://github.com/r0gue-io/pop-node/blob/main/primitives/src/lib.rs#L55): | ||
/// ```rs | ||
/// Error::Raw(Arithmetic(Overflow)) | ||
/// ``` | ||
/// | ||
/// [`Assets(BalanceLow)`](https://paritytech.github.io/polkadot-sdk/master/pallet_assets/pallet/enum.Error.html#variant.BalanceLow): | ||
/// ```rs | ||
/// Error::Module(Assets(BalanceLow)) | ||
/// ``` | ||
/// | ||
/// ## How to use `assert_err` macro. | ||
/// | ||
/// - Create a custom error type that holds the status code. | ||
/// | ||
/// ```rs | ||
/// use pop_api::StatusCode; | ||
/// | ||
/// /// Custom error in contract. | ||
/// pub enum CustomError { | ||
/// ..., | ||
Check warning on line 54 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// /// Error with status code. | ||
Check warning on line 55 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// StatusCode(u32), | ||
Check warning on line 56 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
/// | ||
/// impl From<StatusCode> for CustomError { | ||
/// /// Converts a `StatusCode` (returned by the api) to a `CustomError`. | ||
Check warning on line 60 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// fn from(value: StatusCode) -> Self { | ||
Check warning on line 61 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// match value { | ||
Check warning on line 62 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ..., | ||
Check warning on line 63 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// _ => CustomError::StatusCode(value.0), | ||
Check warning on line 64 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
Check warning on line 65 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
Check warning on line 66 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
/// | ||
/// impl From<CustomError> for u32 { | ||
/// /// Converts a `CustomError to a `u32`. | ||
Check warning on line 70 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// // | ||
Check warning on line 71 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// // Required for the `assert_err` macro to assert to `Error`. | ||
Check warning on line 72 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// fn from(value: CustomError) -> Self { | ||
Check warning on line 73 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// match value { | ||
Check warning on line 74 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ..., | ||
Check warning on line 75 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// CustomError::StatusCode(status_code) => status_code, | ||
Check warning on line 76 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
Check warning on line 77 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
Check warning on line 78 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
/// | ||
/// - Use `assert_err` in a test. | ||
/// | ||
/// #[drink::test(sandbox = Pop)] | ||
/// fn test_custom_error(mut session: Session) { | ||
/// ... | ||
Check warning on line 85 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// | ||
/// // Call a contract method that returns a `Result<(), CustomError>`. | ||
Check warning on line 87 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// let result = call::<Pop, (), CustomError>(session, "hello_world", vec![], None); | ||
Check warning on line 88 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// | ||
/// // Assert the result to the expected error. | ||
Check warning on line 90 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// assert_err!(result, Error::Raw(BadOrigin))); | ||
Check warning on line 91 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// | ||
/// // Other assertions: | ||
Check warning on line 93 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ... | ||
Check warning on line 94 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// assert_err!(result, Error::Raw(Arithmetic(Overflow))); | ||
Check warning on line 95 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ... | ||
Check warning on line 96 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// assert_err!(result, Error::Module(Assets(BalanceLow))); | ||
Check warning on line 97 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
/// ``` | ||
/// | ||
/// # Parameters: | ||
/// - `result` - The result which contains the custom error type. | ||
/// - `error` - The expected error. | ||
#[macro_export] | ||
macro_rules! assert_err { | ||
($result:expr, $error:expr $(,)?) => { | ||
$crate::macros::assert_err_inner::<_, _, _>($result, $error); | ||
}; | ||
} | ||
|
||
#[track_caller] | ||
pub fn assert_err_inner<R, E, Error>(result: Result<R, E>, expected_error: Error) | ||
where | ||
E: Into<u32>, | ||
Error: From<u32> + Into<u32> + Debug, | ||
Check warning on line 115 in crates/pop-drink/src/macros.rs GitHub Actions / clippymissing documentation for a function
|
||
{ | ||
let expected_code: u32 = expected_error.into(); | ||
let expected_error = Error::from(expected_code); | ||
if let Err(error) = result { | ||
let error_code: u32 = error.into(); | ||
if error_code != expected_code { | ||
panic!("{}", assert_message(&Error::from(error_code), &expected_error)); | ||
} | ||
} else { | ||
panic!("{}", assert_message(&"Ok()", &expected_error)); | ||
} | ||
} | ||
|
||
/// Asserts that the latest event matches an expected `event`. | ||
/// | ||
/// This can be used to assert that an event emitted from the latest contract execution resulted in | ||
/// a specific event. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rs | ||
/// assert_last_contract_event!( | ||
/// &session, | ||
Check warning on line 138 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// Transfer { | ||
Check warning on line 139 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// from: Some(account_id_from_slice(&contract)), | ||
Check warning on line 140 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// to: Some(account_id_from_slice(&BOB)), | ||
Check warning on line 141 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// value, | ||
Check warning on line 142 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// } | ||
Check warning on line 143 in crates/pop-drink/src/macros.rs GitHub Actions / clippyusing tabs in doc comments is not recommended
|
||
/// ); | ||
/// ``` | ||
/// | ||
/// # Parameters: | ||
/// - `session` - The session for interacting with contracts. | ||
/// - `event` - The expected event. | ||
#[macro_export] | ||
macro_rules! assert_last_contract_event { | ||
($session:expr, $event:expr $(,)?) => { | ||
$crate::macros::assert_last_contract_event_inner::<_, _>($session, $event); | ||
}; | ||
} | ||
|
||
#[track_caller] | ||
pub fn assert_last_contract_event_inner<S, E>(session: &Session<S>, event: E) | ||
where | ||
S: Sandbox, | ||
S::Runtime: pallet_contracts::Config, | ||
<S::Runtime as frame_system::Config>::RuntimeEvent: | ||
TryInto<pallet_contracts::Event<S::Runtime>>, | ||
E: Decode + Encode + Debug, | ||
Check warning on line 164 in crates/pop-drink/src/macros.rs GitHub Actions / clippymissing documentation for a function
|
||
{ | ||
match last_contract_event(session) { | ||
Some(last_event) => | ||
if last_event != event.encode().as_slice() { | ||
let decoded = E::decode(&mut &last_event[..]).expect("Decoding failed"); | ||
panic!("{}", assert_message(&decoded, &event)); | ||
}, | ||
None => panic!("{}", assert_message(&"None", &event)), | ||
} | ||
} | ||
|
||
fn assert_message<L: Debug, R: Debug>(left: &L, right: &R) -> String { | ||
format!( | ||
r#"assertion `left == right` failed | ||
left: {:?} | ||
right: {:?}"#, | ||
left, right | ||
) | ||
} |