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

create expect error #1558

Merged
merged 2 commits into from
Apr 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn tx_expect_error_test() {
.sc_panic()
.with_result(ExpectStatus(4))
.with_result(ExpectMessage("sc_panic! test"))
.with_result(ExpectError(4, "sc_panic! test"))
.run();
}

Expand All @@ -89,5 +90,6 @@ fn query_expect_error_test() {
.sc_panic()
.with_result(ExpectStatus(4))
.with_result(ExpectMessage("sc_panic! test"))
.with_result(ExpectError(4, "sc_panic! test"))
.run();
}
2 changes: 2 additions & 0 deletions framework/scenario/src/facade/result_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod expect_error;
mod expect_message;
mod expect_status;
mod returns_message;
Expand All @@ -6,6 +7,7 @@ mod returns_new_token_identifier;
mod returns_status;
mod with_tx_raw_response;

pub use expect_error::ExpectError;
pub use expect_message::ExpectMessage;
pub use expect_status::ExpectStatus;
pub use returns_message::ReturnsMessage;
Expand Down
33 changes: 33 additions & 0 deletions framework/scenario/src/facade/result_handlers/expect_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use multiversx_chain_scenario_format::serde_raw::ValueSubTree;
use multiversx_sc::types::{RHListItem, RHListItemExec, TxEnv};

use crate::scenario_model::{BytesValue, CheckValue, TxExpect, TxResponse};

/// Verifies that transaction result error matches the given one.
///
/// Can only be used in tests and interactors, not available in contracts.
pub struct ExpectError<'a>(pub u64, pub &'a str);

impl<'a, Env, Original> RHListItem<Env, Original> for ExpectError<'a>
where
Env: TxEnv,
{
type Returns = ();
}

impl<'a, Env, Original> RHListItemExec<TxResponse, Env, Original> for ExpectError<'a>
where
Env: TxEnv<RHExpect = TxExpect>,
{
fn item_tx_expect(&self, mut prev: TxExpect) -> TxExpect {
prev.status = CheckValue::Equal(self.0.into());
let expect_message_expr = BytesValue {
value: self.1.to_string().into_bytes(),
original: ValueSubTree::Str(format!("str:{}", self.1)),
};
prev.message = CheckValue::Equal(expect_message_expr);
prev
}

fn item_process_result(self, _: &TxResponse) -> Self::Returns {}
}
Loading