Skip to content

Commit 539d06b

Browse files
committed
Use assert_matches
1 parent 6ee62a4 commit 539d06b

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed

Diff for: p-token/tests/setup/mod.rs

-43
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,3 @@ pub mod account;
66
pub mod mint;
77

88
pub const TOKEN_PROGRAM_ID: Pubkey = Pubkey::new_from_array(spl_token_interface::program::ID);
9-
10-
#[macro_export]
11-
macro_rules! assert_instruction_error {
12-
( $error:expr, $matcher:pat ) => {
13-
match $error {
14-
solana_program_test::BanksClientError::TransactionError(
15-
solana_sdk::transaction::TransactionError::InstructionError(_, $matcher),
16-
) => {
17-
assert!(true)
18-
}
19-
err => assert!(false, "Expected instruction error but got '{:#?}'", err),
20-
};
21-
};
22-
}
23-
24-
#[macro_export]
25-
macro_rules! assert_custom_error {
26-
( $error:expr, $matcher:pat ) => {
27-
match $error {
28-
solana_program_test::BanksClientError::TransactionError(
29-
solana_sdk::transaction::TransactionError::InstructionError(
30-
_,
31-
solana_sdk::instruction::InstructionError::Custom(x),
32-
),
33-
) => match num_traits::FromPrimitive::from_i32(x as i32) {
34-
Some($matcher) => assert!(true),
35-
Some(other) => {
36-
assert!(
37-
false,
38-
"Expected another custom instruction error than '{:#?}'",
39-
other
40-
)
41-
}
42-
None => assert!(false, "Expected custom instruction error"),
43-
},
44-
err => assert!(
45-
false,
46-
"Expected custom instruction error but got '{:#?}'",
47-
err
48-
),
49-
};
50-
};
51-
}

Diff for: p-token/tests/withdraw_excess_lamports.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
mod setup;
22

3-
use std::mem::size_of;
4-
3+
use assert_matches::assert_matches;
54
use setup::{mint, TOKEN_PROGRAM_ID};
6-
use solana_program_test::{tokio, ProgramTest};
5+
use solana_program_test::{tokio, BanksClientError, ProgramTest};
76
use solana_sdk::{
87
instruction::InstructionError,
98
pubkey::Pubkey,
109
signature::{Keypair, Signer},
1110
system_instruction,
12-
transaction::Transaction,
11+
transaction::{Transaction, TransactionError},
1312
};
1413
use spl_token_interface::state::{account::Account, mint::Mint, multisig::Multisig};
14+
use std::mem::size_of;
1515

1616
#[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
1717
#[tokio::test]
@@ -417,7 +417,13 @@ async fn fail_withdraw_excess_lamports_from_mint_wrong_authority(token_program:
417417

418418
// The we expect an error.
419419

420-
assert_custom_error!(error, spl_token::error::TokenError::OwnerMismatch);
420+
assert_matches!(
421+
error,
422+
BanksClientError::TransactionError(TransactionError::InstructionError(
423+
_,
424+
InstructionError::Custom(4) // TokenError::OwnerMismatch
425+
))
426+
);
421427
}
422428

423429
#[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
@@ -527,7 +533,13 @@ async fn fail_withdraw_excess_lamports_from_account_wrong_authority(token_progra
527533

528534
// The we expect an error.
529535

530-
assert_custom_error!(error, spl_token::error::TokenError::OwnerMismatch);
536+
assert_matches!(
537+
error,
538+
BanksClientError::TransactionError(TransactionError::InstructionError(
539+
_,
540+
InstructionError::Custom(4) // TokenError::OwnerMismatch
541+
))
542+
);
531543
}
532544

533545
#[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
@@ -628,7 +640,13 @@ async fn fail_withdraw_excess_lamports_from_multisig_wrong_authority(token_progr
628640

629641
// The we expect an error.
630642

631-
assert_custom_error!(error, spl_token::error::TokenError::OwnerMismatch);
643+
assert_matches!(
644+
error,
645+
BanksClientError::TransactionError(TransactionError::InstructionError(
646+
_,
647+
InstructionError::Custom(4) // TokenError::OwnerMismatch
648+
))
649+
);
632650
}
633651

634652
#[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
@@ -728,5 +746,11 @@ async fn fail_withdraw_excess_lamports_from_multisig_missing_signer(token_progra
728746

729747
// The we expect an error.
730748

731-
assert_instruction_error!(error, InstructionError::MissingRequiredSignature);
749+
assert_matches!(
750+
error,
751+
BanksClientError::TransactionError(TransactionError::InstructionError(
752+
_,
753+
InstructionError::MissingRequiredSignature
754+
))
755+
);
732756
}

0 commit comments

Comments
 (0)