Skip to content

Commit

Permalink
chore: add mock_input host function
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed May 17, 2024
1 parent 3b1bb65 commit 2984173
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use extism_pdk::{Memory, ToMemory};
use extism_pdk::{FromBytesOwned, Memory, ToMemory};

mod harness {
#[link(wasm_import_module = "xtp:test/harness")]
Expand All @@ -9,9 +9,23 @@ mod harness {
pub fn assert(name: u64, value: u64, message: u64);
pub fn reset();
pub fn group(name: u64);
pub fn mock_input() -> u64;
}
}

pub fn mock_input<T: FromBytesOwned>() -> Result<T> {
let offs = unsafe { harness::mock_input() };
if offs == 0 {
anyhow::bail!("No mock input configured");
}

let output = match Memory::find(offs) {
None => anyhow::bail!("Error fetching mock input: invalid output offset"),
Some(x) => x,
};
output.to()
}

/// Call a function from the Extism plugin being tested, passing input and returning its output Memory.
pub fn call_memory(func_name: impl AsRef<str>, input: impl ToMemory) -> Result<Memory> {
let func_name = func_name.as_ref();
Expand Down

0 comments on commit 2984173

Please sign in to comment.