pub mod bench; pub mod decode; pub mod into; pub mod print; pub mod spl; #[cfg(test)] mod tests { use { super::*, serial_test::serial, miraland_program::{ decode_error::DecodeError, program_error::{PrintProgramError, ProgramError}, }, std::sync::{Arc, RwLock}, }; // Used to capture output for `PrintProgramError` for testing lazy_static::lazy_static! { static ref EXPECTED_DATA: Arc>> = Arc::new(RwLock::new(Vec::new())); } fn set_expected_data(expected_data: Vec) { *EXPECTED_DATA.write().unwrap() = expected_data; } pub struct SyscallStubs {} impl miraland_sdk::program_stubs::SyscallStubs for SyscallStubs { fn sol_log(&self, message: &str) { assert_eq!( message, String::from_utf8_lossy(&EXPECTED_DATA.read().unwrap()) ); } } // `#[derive(IntoProgramError)]` #[test] fn test_derive_into_program_error() { // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), Into::::into(into::ExampleError::MintHasNoMintAuthority), ); assert_eq!( Into::::into(bench::ExampleError::IncorrectMintAuthority), Into::::into(into::ExampleError::IncorrectMintAuthority), ); } // `#[derive(DecodeError)]` #[test] fn test_derive_decode_error() { // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), Into::::into(decode::ExampleError::MintHasNoMintAuthority), ); assert_eq!( Into::::into(bench::ExampleError::IncorrectMintAuthority), Into::::into(decode::ExampleError::IncorrectMintAuthority), ); // `DecodeError` assert_eq!( >::type_of(), >::type_of(), ); } // `#[derive(PrintProgramError)]` #[test] #[serial] fn test_derive_print_program_error() { use std::sync::Once; static ONCE: Once = Once::new(); ONCE.call_once(|| { miraland_sdk::program_stubs::set_syscall_stubs(Box::new(SyscallStubs {})); }); // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), Into::::into(print::ExampleError::MintHasNoMintAuthority), ); assert_eq!( Into::::into(bench::ExampleError::IncorrectMintAuthority), Into::::into(print::ExampleError::IncorrectMintAuthority), ); // `DecodeError` assert_eq!( >::type_of(), >::type_of(), ); // `PrintProgramError` set_expected_data("Mint has no mint authority".as_bytes().to_vec()); PrintProgramError::print::( &print::ExampleError::MintHasNoMintAuthority, ); set_expected_data( "Incorrect mint authority has signed the instruction" .as_bytes() .to_vec(), ); PrintProgramError::print::( &print::ExampleError::IncorrectMintAuthority, ); } // `#[spl_program_error]` #[test] #[serial] fn test_spl_program_error() { use std::sync::Once; static ONCE: Once = Once::new(); ONCE.call_once(|| { miraland_sdk::program_stubs::set_syscall_stubs(Box::new(SyscallStubs {})); }); // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), Into::::into(spl::ExampleError::MintHasNoMintAuthority), ); assert_eq!( Into::::into(bench::ExampleError::IncorrectMintAuthority), Into::::into(spl::ExampleError::IncorrectMintAuthority), ); // `DecodeError` assert_eq!( >::type_of(), >::type_of(), ); // `PrintProgramError` set_expected_data("Mint has no mint authority".as_bytes().to_vec()); PrintProgramError::print::(&spl::ExampleError::MintHasNoMintAuthority); set_expected_data( "Incorrect mint authority has signed the instruction" .as_bytes() .to_vec(), ); PrintProgramError::print::(&spl::ExampleError::IncorrectMintAuthority); } }