Skip to content

Commit

Permalink
Merge pull request #25 from r0gue-io/chungquantin/feat-assert_last_event
Browse files Browse the repository at this point in the history
feat: assert last event
  • Loading branch information
chungquantin authored Nov 5, 2024
2 parents 13ea823 + 4b6fe8c commit e131e72
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[workspace]
resolver = "2"
members = [
"crates/ink-sandbox",
"crates/drink/drink",
"crates/drink/drink-cli",
"crates/drink/drink/test-macro",
"crates/pop-drink",
"crates/ink-sandbox",
"crates/drink/drink",
"crates/drink/drink-cli",
"crates/drink/drink/test-macro",
"crates/pop-drink",
]
exclude = ["crates/drink/examples"]

Expand All @@ -30,7 +30,7 @@ proc-macro2 = { version = "1" }
quote = { version = "1" }
ratatui = { version = "0.21.0" }
scale = { package = "parity-scale-codec", version = "3.6.9", features = [
"derive",
"derive",
] }
scale-info = { version = "2.10.0" }
serde_json = { version = "1.0" }
Expand Down
132 changes: 0 additions & 132 deletions crates/pop-drink/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,138 +72,6 @@ where
}
}

/// 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,
/// AssetsError::BalanceLow,
/// v0::{
/// Arithmetic,
/// ArithmeticError::Overflow,
/// BadOrigin
/// },
/// };
/// ```
///
/// [`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 {
/// ...,
/// /// Error with status code.
/// StatusCode(u32),
/// }
///
/// impl From<StatusCode> for CustomError {
/// /// Converts a `StatusCode` (returned by the api) to a `CustomError`.
/// fn from(value: StatusCode) -> Self {
/// match value {
/// ...,
/// _ => CustomError::StatusCode(value.0),
/// }
/// }
/// }
///
/// impl From<CustomError> for u32 {
/// /// Converts a `CustomError to a `u32`.
/// //
/// // Required for the `assert_err` macro to assert to `Error`.
/// fn from(value: CustomError) -> Self {
/// match value {
/// ...,
/// CustomError::StatusCode(status_code) => status_code,
/// }
/// }
/// }
///
/// - Use `assert_err` in a test.
///
/// #[drink::test(sandbox = Pop)]
/// fn test_custom_error(mut session: Session) {
/// ...
///
/// // Call a contract method that returns a `Result<(), CustomError>`.
/// let result = call::<Pop, (), CustomError>(session, "hello_world", vec![], None);
///
/// // Assert the result to the expected error.
/// assert_err!(result, Error::Raw(BadOrigin)));
///
/// // Other assertions:
/// ...
/// assert_err!(result, Error::Raw(Arithmetic(Overflow)));
/// ...
/// assert_err!(result, Error::Module(Assets(BalanceLow)));
/// }
/// ```
///
/// # 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::error::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,
{
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!(
r#"assertion `left == right` failed
left: {:?}
right: {:?}"#,
Error::from(error_code),
expected_error
);
}
} else {
panic!(
r#"assertion `left == right` failed
left: Ok()
right: {:?}"#,
expected_error
);
}
}

fn decode<T: Decode>(data: &[u8]) -> T {
T::decode(&mut &data[..]).expect("Decoding failed")
}
Expand Down
4 changes: 3 additions & 1 deletion crates/pop-drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub use sp_io::TestExternalities;

/// Error type and utilities for testing contracts using the Pop API.
pub mod error;
/// Collection of macros for testing contracts using the Pop API.
pub mod macros;
#[cfg(test)]
mod mock;

Expand Down Expand Up @@ -105,7 +107,7 @@ where
///
/// # Parameters:
/// - `session` - The session for interacting with contracts.
/// - `func_name`: The name of the contract method.
/// - `func_name` - The name of the contract method.
/// - `input` - The input arguments.
/// - `init_value` - Balance to transfer during the call. Requires the contract method to be
/// `payable`.
Expand Down
183 changes: 183 additions & 0 deletions crates/pop-drink/src/macros.rs
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

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:20:5 | 20 | /// Assets, | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments = note: `#[warn(clippy::tabs_in_doc_comments)]` on by default
/// AssetsError::BalanceLow,

Check warning on line 21 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:21:5 | 21 | /// AssetsError::BalanceLow, | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// v0::{

Check warning on line 22 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:22:5 | 22 | /// v0::{ | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// Arithmetic,

Check warning on line 23 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:23:5 | 23 | /// Arithmetic, | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// ArithmeticError::Overflow,

Check warning on line 24 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:24:5 | 24 | /// ArithmeticError::Overflow, | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// BadOrigin

Check warning on line 25 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:25:5 | 25 | /// BadOrigin | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// },

Check warning on line 26 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:26:5 | 26 | /// }, | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// };
/// ```
///
/// [`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

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:54:5 | 54 | /// ..., | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// /// Error with status code.

Check warning on line 55 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:55:5 | 55 | /// /// Error with status code. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// StatusCode(u32),

Check warning on line 56 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:56:5 | 56 | /// StatusCode(u32), | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }
///
/// 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

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:60:5 | 60 | /// /// Converts a `StatusCode` (returned by the api) to a `CustomError`. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// fn from(value: StatusCode) -> Self {

Check warning on line 61 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:61:5 | 61 | /// fn from(value: StatusCode) -> Self { | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// match value {

Check warning on line 62 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:62:5 | 62 | /// match value { | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// ...,

Check warning on line 63 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:63:5 | 63 | /// ..., | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// _ => CustomError::StatusCode(value.0),

Check warning on line 64 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:64:5 | 64 | /// _ => CustomError::StatusCode(value.0), | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }

Check warning on line 65 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:65:5 | 65 | /// } | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }

Check warning on line 66 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:66:5 | 66 | /// } | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }
///
/// impl From<CustomError> for u32 {
/// /// Converts a `CustomError to a `u32`.

Check warning on line 70 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:70:5 | 70 | /// /// Converts a `CustomError to a `u32`. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// //

Check warning on line 71 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:71:5 | 71 | /// // | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// // Required for the `assert_err` macro to assert to `Error`.

Check warning on line 72 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:72:5 | 72 | /// // Required for the `assert_err` macro to assert to `Error`. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// fn from(value: CustomError) -> Self {

Check warning on line 73 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:73:5 | 73 | /// fn from(value: CustomError) -> Self { | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// match value {

Check warning on line 74 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:74:5 | 74 | /// match value { | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// ...,

Check warning on line 75 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:75:5 | 75 | /// ..., | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// CustomError::StatusCode(status_code) => status_code,

Check warning on line 76 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:76:5 | 76 | /// CustomError::StatusCode(status_code) => status_code, | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }

Check warning on line 77 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:77:5 | 77 | /// } | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }

Check warning on line 78 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:78:5 | 78 | /// } | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }
///
/// - 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

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:85:5 | 85 | /// ... | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
///
/// // Call a contract method that returns a `Result<(), CustomError>`.

Check warning on line 87 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:87:5 | 87 | /// // Call a contract method that returns a `Result<(), CustomError>`. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// let result = call::<Pop, (), CustomError>(session, "hello_world", vec![], None);

Check warning on line 88 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:88:5 | 88 | /// let result = call::<Pop, (), CustomError>(session, "hello_world", vec![], None); | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
///
/// // Assert the result to the expected error.

Check warning on line 90 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:90:5 | 90 | /// // Assert the result to the expected error. | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// assert_err!(result, Error::Raw(BadOrigin)));

Check warning on line 91 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:91:5 | 91 | /// assert_err!(result, Error::Raw(BadOrigin))); | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
///
/// // Other assertions:

Check warning on line 93 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:93:5 | 93 | /// // Other assertions: | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// ...

Check warning on line 94 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:94:5 | 94 | /// ... | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// assert_err!(result, Error::Raw(Arithmetic(Overflow)));

Check warning on line 95 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:95:5 | 95 | /// assert_err!(result, Error::Raw(Arithmetic(Overflow))); | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// ...

Check warning on line 96 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:96:5 | 96 | /// ... | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// assert_err!(result, Error::Module(Assets(BalanceLow)));

Check warning on line 97 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:97:5 | 97 | /// assert_err!(result, Error::Module(Assets(BalanceLow))); | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }
/// ```
///
/// # 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

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a function

warning: missing documentation for a function --> crates/pop-drink/src/macros.rs:112:1 | 112 | / pub fn assert_err_inner<R, E, Error>(result: Result<R, E>, expected_error: Error) 113 | | where 114 | | E: Into<u32>, 115 | | Error: From<u32> + Into<u32> + Debug, | |_________________________________________^ | = note: requested on the command line with `-W missing-docs`
{
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

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:138:5 | 138 | /// &session, | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// Transfer {

Check warning on line 139 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:139:5 | 139 | /// Transfer { | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// from: Some(account_id_from_slice(&contract)),

Check warning on line 140 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:140:5 | 140 | /// from: Some(account_id_from_slice(&contract)), | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// to: Some(account_id_from_slice(&BOB)),

Check warning on line 141 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:141:5 | 141 | /// to: Some(account_id_from_slice(&BOB)), | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// value,

Check warning on line 142 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:142:5 | 142 | /// value, | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// }

Check warning on line 143 in crates/pop-drink/src/macros.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> crates/pop-drink/src/macros.rs:143:5 | 143 | /// } | ^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
/// );
/// ```
///
/// # 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

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a function

warning: missing documentation for a function --> crates/pop-drink/src/macros.rs:158:1 | 158 | / pub fn assert_last_contract_event_inner<S, E>(session: &Session<S>, event: E) 159 | | where 160 | | S: Sandbox, 161 | | S::Runtime: pallet_contracts::Config, 162 | | <S::Runtime as frame_system::Config>::RuntimeEvent: 163 | | TryInto<pallet_contracts::Event<S::Runtime>>, 164 | | E: Decode + Encode + Debug, | |_______________________________^
{
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
)
}

0 comments on commit e131e72

Please sign in to comment.