Skip to content

Commit

Permalink
fix: expect return error method
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Sep 24, 2024
1 parent 517ddd3 commit 86e7d85
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion pop-api/examples/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,19 @@ fn transfer_fails_with_insufficient_balance(
const AMOUNT: Balance = TOKEN_MIN_BALANCE * 4;
assert_ok!(session.sandbox().mint_into(&TOKEN_ID, &contract.clone(), AMOUNT));
assert_ok!(session.sandbox().mint_into(&TOKEN_ID, &BOB, AMOUNT));
assert_eq!(session.sandbox().balance_of(&TOKEN_ID, &contract.clone()), AMOUNT);

session.set_actor(contract.clone());
// Failed with `InsufficientBalance`.
let data = serde_json::to_string::<[u8; 0]>(&[]).unwrap();
expect_call_reverted(
&mut session,
TRANSFER,
vec![BOB.to_string(), (AMOUNT + 1).to_string()],
vec![BOB.to_string(), (AMOUNT + 1).to_string(), data],
PSP22Error::InsufficientBalance,
);
// Check that balance of account is not changed.
assert_eq!(session.sandbox().balance_of(&TOKEN_ID, &contract), AMOUNT);
Ok(())
}

Expand Down
9 changes: 6 additions & 3 deletions pop-api/examples/fungibles/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ pub(super) fn last_contract_event(session: &Session<Sandbox>) -> Option<Vec<u8>>
session.record().last_event_batch().contract_events().last().cloned()
}

/// Execute a contract method and exepct CallReverted error to be returned.
/// Execute a contract method and expect CallReverted error to be returned.
pub(super) fn expect_call_reverted(
session: &mut Session<Sandbox>,
function: &str,
params: Vec<String>,
err: PSP22Error,
) {
let call = session.call::<String, ()>(function, &params, None);
if let Err(SessionError::CallReverted(error)) = call {
assert_eq!(error[1..], Err::<(), PSP22Error>(err).encode());
match call {
Err(SessionError::CallReverted(error)) => {
assert_eq!(error[1..], Err::<(), PSP22Error>(err).encode())
},
_ => panic!("Expect call reverted"),
}
}

Expand Down

0 comments on commit 86e7d85

Please sign in to comment.