Skip to content

Commit

Permalink
Extract new processor macro
Browse files Browse the repository at this point in the history
  • Loading branch information
andreisilviudragnea committed Aug 14, 2024
1 parent 9864c2d commit c5d935d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
33 changes: 21 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ pub use solana_program_test::tokio;
pub use solana_program_test::*;

#[macro_export]
macro_rules! processor {
($process_instruction:expr) => {
Some(
|first_instruction_account: usize,
invoke_context: &mut solana_program_test::InvokeContext| {
$crate::builtin_process_instruction(
$process_instruction,
first_instruction_account,
invoke_context,
)
},
)
macro_rules! correct_entry {
($entry:path) => {
fn correct_entry(
program_id: &solana_program::pubkey::Pubkey,
accounts: &[solana_program::account_info::AccountInfo],
data: &[u8],
) -> solana_program::entrypoint::ProgramResult {
$entry(
program_id,
unsafe { &*(accounts as *const [solana_program::account_info::AccountInfo]) },
data,
)
}
};
}

#[macro_export]
macro_rules! processor {
($entry:path) => {{
correct_entry!($entry);
solana_program_test::processor!(correct_entry)
}};
}
12 changes: 1 addition & 11 deletions tests/testsuite/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@ use solana_sdk::{
signature::{Keypair, Signer},
};

use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use std::str::FromStr;

pub fn correct_entry(program_id: &Pubkey, accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
program_for_tests::entry(
program_id,
unsafe { &*(accounts as *const [AccountInfo]) },
data,
)
}

pub fn add_program() -> (ProgramTest, Pubkey) {
let program_id = Pubkey::from_str("CwrqeMj2U8tFr1Rhkgwc84tpAsqbt9pTt2a4taoTADPr").unwrap();
let program = ProgramTest::new(
"program_for_tests",
program_id,
solana_program_test::processor!(correct_entry),
processor!(program_for_tests::entry),
);

(program, program_id)
Expand Down
3 changes: 1 addition & 2 deletions tests/testsuite/program_test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use solana_sdk::{pubkey::Pubkey, sysvar::clock::Clock};

use std::str::FromStr;

use crate::helpers::correct_entry;
#[cfg(feature = "pyth")]
use pyth_sdk_solana::state::{PriceInfo, PriceStatus, SolanaPriceAccount};

Expand All @@ -14,7 +13,7 @@ async fn transaction_from_instructions() {
let program = ProgramTest::new(
"program_for_tests",
program_id,
solana_program_test::processor!(correct_entry),
processor!(program_for_tests::entry),
);

let mut program_context = program.start_with_context().await;
Expand Down

0 comments on commit c5d935d

Please sign in to comment.