Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: assert last event #25

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -65,145 +65,13 @@
let encoded = error.encode();
if encoded[0] == MODULE_INDEX {
let (index, module_error) = (encoded[1], &encoded[2..]);
let data = vec![vec![index], module_error.to_vec()].concat();

Check warning on line 68 in crates/pop-drink/src/error.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `vec!`

warning: useless use of `vec!` --> crates/pop-drink/src/error.rs:68:15 | 68 | let data = vec![vec![index], module_error.to_vec()].concat(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[vec![index], module_error.to_vec()]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec = note: `#[warn(clippy::useless_vec)]` on by default
return Error::Module(decode(&data));
}
Error::Raw(error)
}
}

/// 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 @@

/// 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 All @@ -27,7 +29,7 @@

pub use crate::error::*;

pub mod v0 {

Check warning on line 32 in crates/pop-drink/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a module

warning: missing documentation for a module --> crates/pop-drink/src/lib.rs:32:3 | 32 | pub mod v0 { | ^^^^^^^^^^
pub use pop_api::primitives::v0::{self, Error as ApiError, *};

/// Error type for writing tests (see `error` module).
Expand All @@ -36,8 +38,8 @@
}

// Types used in the pop runtime.
pub type Balance = BalanceFor<Runtime>;

Check warning on line 41 in crates/pop-drink/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a type alias

warning: missing documentation for a type alias --> crates/pop-drink/src/lib.rs:41:2 | 41 | pub type Balance = BalanceFor<Runtime>; | ^^^^^^^^^^^^^^^^
pub type AccountId = AccountIdFor<Runtime>;

Check warning on line 42 in crates/pop-drink/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a type alias

warning: missing documentation for a type alias --> crates/pop-drink/src/lib.rs:42:2 | 42 | pub type AccountId = AccountIdFor<Runtime>; | ^^^^^^^^^^^^^^^^^^

/// Converts an AccountId from Pop's runtime to the account ID used in the contract environment.
pub fn account_id_from_slice(s: &AccountId) -> pop_api::primitives::AccountId {
Expand Down Expand Up @@ -66,12 +68,12 @@
/// ```rs
/// #[drink::test(sandbox = Pop)]
/// fn test_constructor_works(mut session: Session) {
/// let bundle = BundleProvider::local().unwrap();

Check warning on line 71 in crates/pop-drink/src/lib.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/lib.rs:71:5 | 71 | /// let bundle = BundleProvider::local().unwrap(); | ^^^^ 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
///
/// // Deploy contract.

Check warning on line 73 in crates/pop-drink/src/lib.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/lib.rs:73:5 | 73 | /// // Deploy 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
/// //

Check warning on line 74 in crates/pop-drink/src/lib.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/lib.rs:74:5 | 74 | /// // | ^^^^ 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
/// // `ContractError` is the error type used by the contract.

Check warning on line 75 in crates/pop-drink/src/lib.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/lib.rs:75:5 | 75 | /// // `ContractError` is the error type used by the 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
/// assert_ok!(deploy<Pop, ContractError>(&mut session, bundle, "new", input, salt, init_value));

Check warning on line 76 in crates/pop-drink/src/lib.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/lib.rs:76:5 | 76 | /// assert_ok!(deploy<Pop, ContractError>(&mut session, bundle, "new", input, salt, init_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
/// }
/// ```
pub fn deploy<S, E>(
Expand Down Expand Up @@ -105,7 +107,7 @@
///
/// # 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 All @@ -114,19 +116,19 @@
/// ```rs
/// #[drink::test(sandbox = Pop)]
/// fn call_works(mut session: Session) {
/// let bundle = BundleProvider::local().unwrap();

Check warning on line 119 in crates/pop-drink/src/lib.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/lib.rs:119:5 | 119 | /// let bundle = BundleProvider::local().unwrap(); | ^^^^ 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_ok!(deploy<Pop, ContractError>(&mut session, bundle, "new", input, salt, init_value));

Check warning on line 120 in crates/pop-drink/src/lib.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/lib.rs:120:5 | 120 | /// assert_ok!(deploy<Pop, ContractError>(&mut session, bundle, "new", input, salt, init_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
///
/// // Call contract.

Check warning on line 122 in crates/pop-drink/src/lib.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/lib.rs:122:5 | 122 | /// // Call 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
/// //
/// // `()` is the successful result type used by the contract.

Check warning on line 124 in crates/pop-drink/src/lib.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/lib.rs:124:5 | 124 | /// // `()` is the successful result type used by the 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
/// // `ContractError` is the error type used by the contract.

Check warning on line 125 in crates/pop-drink/src/lib.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/lib.rs:125:5 | 125 | /// // `ContractError` is the error type used by the 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
/// call::<Pop, (), ContractError>(

Check warning on line 126 in crates/pop-drink/src/lib.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/lib.rs:126:5 | 126 | /// call::<Pop, (), ContractError>( | ^^^^ 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
/// session,

Check warning on line 127 in crates/pop-drink/src/lib.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/lib.rs:127:5 | 127 | /// 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 128 in crates/pop-drink/src/lib.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/lib.rs:128:5 | 128 | /// "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
/// input,

Check warning on line 129 in crates/pop-drink/src/lib.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/lib.rs:129:5 | 129 | /// input, | ^^^^^^^^ 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
/// init_value,

Check warning on line 130 in crates/pop-drink/src/lib.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/lib.rs:130:5 | 130 | /// init_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 131 in crates/pop-drink/src/lib.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/lib.rs:131:5 | 131 | /// ) | ^^^^ 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
/// }
/// ```
pub fn call<S, O, E>(
Expand Down Expand Up @@ -169,12 +171,12 @@
/// use drink::last_contract_event;
///
/// assert_eq!(
/// last_contract_event::<Pop>(&session).unwrap(),

Check warning on line 174 in crates/pop-drink/src/lib.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/lib.rs:174:5 | 174 | /// last_contract_event::<Pop>(&session).unwrap(), | ^^^^ 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
/// ContractEvent {

Check warning on line 175 in crates/pop-drink/src/lib.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/lib.rs:175:5 | 175 | /// ContractEvent { | ^^^^ 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: 42,

Check warning on line 176 in crates/pop-drink/src/lib.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/lib.rs:176:5 | 176 | /// value: 42, | ^^^^^^^^ 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 177 in crates/pop-drink/src/lib.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/lib.rs:177:5 | 177 | /// } | ^^^^ 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
/// .encode()

Check warning on line 178 in crates/pop-drink/src/lib.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/lib.rs:178:5 | 178 | /// .encode() | ^^^^ 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
/// .as_slice()

Check warning on line 179 in crates/pop-drink/src/lib.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/lib.rs:179:5 | 179 | /// .as_slice() | ^^^^ 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
/// );
/// ```
pub fn last_contract_event<S>(session: &Session<S>) -> Option<Vec<u8>>
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
)
}
Loading