78 lines
3.1 KiB
Rust
78 lines
3.1 KiB
Rust
// file: crates/ksp-program-api/tests/public_api.rs
|
|
// version: 3
|
|
|
|
//! Integration canaries for the public `ksp-program-api` foundation.
|
|
|
|
fn consume_result(value: ksp_program_api::Result<ksp_program_api::Pubkey>) -> ksp_program_api::Result<ksp_program_api::Pubkey> {
|
|
return value;
|
|
}
|
|
|
|
#[test]
|
|
fn public_pre_002_core_and_interface_facade_is_available_from_crate_root() {
|
|
let program_id = ksp_program_api::Pubkey::new_from_array([0xA1_u8; 32]);
|
|
let account_id = ksp_program_api::Pubkey::new_from_array([0xA2_u8; 32]);
|
|
let account = ksp_program_api::ProgramAccountMeta::readonly(account_id, true);
|
|
let instruction = ksp_program_api::ProgramInstruction::try_new(program_id, std::vec![account], std::vec![0xA3_u8]);
|
|
assert!(instruction.is_ok());
|
|
let forwarded = consume_result(std::result::Result::Ok(program_id));
|
|
assert!(forwarded.is_ok());
|
|
let error_code_type: std::option::Option<ksp_program_api::ErrorCode> = std::option::Option::None;
|
|
let error_context_type: std::option::Option<ksp_program_api::ErrorContext> = std::option::Option::None;
|
|
let error_type: std::option::Option<ksp_program_api::Error> = std::option::Option::None;
|
|
assert!(error_code_type.is_none());
|
|
assert!(error_context_type.is_none());
|
|
assert!(error_type.is_none());
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn public_pre_002_scaffold_does_not_require_private_modules() {
|
|
let source = include_str!("../src/lib.rs");
|
|
assert!(!source.contains("pub mod "));
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn public_pre_003_recognition_and_decode_outcome_are_available_from_crate_root() {
|
|
let recognition = ksp_program_api::ProgramInstructionRecognition::ProgramMatch;
|
|
assert_eq!(std::format!("{recognition:?}"), "ProgramMatch");
|
|
let decoded = ksp_program_api::ProgramInstructionDecodeOutcome::Decoded(31_u16);
|
|
assert_eq!(std::format!("{decoded:?}"), "Decoded");
|
|
let decoded_value = match decoded {
|
|
ksp_program_api::ProgramInstructionDecodeOutcome::Decoded(value) => value,
|
|
_ => 0_u16,
|
|
};
|
|
assert_eq!(decoded_value, 31_u16);
|
|
let unsupported = ksp_program_api::ProgramInstructionDecodeOutcome::<u16>::Unsupported;
|
|
assert_eq!(std::format!("{unsupported:?}"), "Unsupported");
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn public_pre_004_instruction_decoder_trait_is_available_from_crate_root() {
|
|
let decoder = NeverInstantiatedDecoder;
|
|
assert!(ksp_program_api::ProgramInstructionDecoder::program_ids(&decoder).is_empty());
|
|
return;
|
|
}
|
|
|
|
struct NeverInstantiatedDecoder;
|
|
|
|
impl ksp_program_api::ProgramInstructionDecoder for NeverInstantiatedDecoder {
|
|
type Decoded = u8;
|
|
|
|
fn program_ids(&self) -> &[ksp_program_api::Pubkey] {
|
|
return &[];
|
|
}
|
|
|
|
fn recognize(&self, _instruction: &ksp_program_api::ProgramInstruction) -> ksp_program_api::ProgramInstructionRecognition {
|
|
return ksp_program_api::ProgramInstructionRecognition::NoMatch;
|
|
}
|
|
|
|
fn decode(
|
|
&self,
|
|
_instruction: &ksp_program_api::ProgramInstruction,
|
|
) -> ksp_program_api::Result<ksp_program_api::ProgramInstructionDecodeOutcome<Self::Decoded>> {
|
|
return std::result::Result::Ok(ksp_program_api::ProgramInstructionDecodeOutcome::Unsupported);
|
|
}
|
|
}
|