From 23f639e16767a976ba1abeefcd99615d9a80134e Mon Sep 17 00:00:00 2001 From: Vy Ta Date: Mon, 22 Jan 2024 11:37:11 -0700 Subject: [PATCH] Runtime test for options --- crates/test-rust-wasm/Cargo.toml | 4 ++ crates/test-rust-wasm/src/bin/options.rs | 3 ++ tests/runtime/main.rs | 1 + tests/runtime/options.rs | 53 ++++++++++++++++++++++++ tests/runtime/options/wasm.rs | 43 +++++++++++++++++++ tests/runtime/options/world.wit | 17 ++++++++ 6 files changed, 121 insertions(+) create mode 100644 crates/test-rust-wasm/src/bin/options.rs create mode 100644 tests/runtime/options.rs create mode 100644 tests/runtime/options/wasm.rs create mode 100644 tests/runtime/options/world.wit diff --git a/crates/test-rust-wasm/Cargo.toml b/crates/test-rust-wasm/Cargo.toml index 374aa9103..100c2e9d7 100644 --- a/crates/test-rust-wasm/Cargo.toml +++ b/crates/test-rust-wasm/Cargo.toml @@ -44,6 +44,10 @@ test = false name = "results" test = false +[[bin]] +name = "options" +test = false + [[bin]] name = "owning" test = false diff --git a/crates/test-rust-wasm/src/bin/options.rs b/crates/test-rust-wasm/src/bin/options.rs new file mode 100644 index 000000000..b5ecf242a --- /dev/null +++ b/crates/test-rust-wasm/src/bin/options.rs @@ -0,0 +1,3 @@ +include!("../../../../tests/runtime/options/wasm.rs"); + +fn main() {} diff --git a/tests/runtime/main.rs b/tests/runtime/main.rs index d88cee650..55035cb32 100644 --- a/tests/runtime/main.rs +++ b/tests/runtime/main.rs @@ -16,6 +16,7 @@ mod flavorful; mod lists; mod many_arguments; mod numbers; +mod options; mod other_dependencies; mod ownership; mod records; diff --git a/tests/runtime/options.rs b/tests/runtime/options.rs new file mode 100644 index 000000000..20c09bc39 --- /dev/null +++ b/tests/runtime/options.rs @@ -0,0 +1,53 @@ +use anyhow::Result; +use wasmtime::Store; + +wasmtime::component::bindgen!(in "tests/runtime/options"); + +#[derive(Default)] +pub struct MyImports; + +impl test::options::test::Host for MyImports { + fn option_none_param(&mut self, a: Option) -> Result<()> { + assert!(a.is_none()); + Ok(()) + } + + fn option_none_result(&mut self) -> Result> { + Ok(None) + } + + fn option_some_param(&mut self, a: Option) -> Result<()> { + assert_eq!(a, Some("foo".to_string())); + Ok(()) + } + + fn option_some_result(&mut self) -> Result> { + Ok(Some("foo".to_string())) + } + + fn option_roundtrip(&mut self, a: Option) -> Result> { + Ok(a) + } + +} + +#[test] +fn run() -> Result<()> { + crate::run_test( + "options", + |linker| Options::add_to_linker(linker, |x| &mut x.0), + |store, component, linker| Options::instantiate(store, component, linker), + run_test, + ) +} + +fn run_test(exports: Options, store: &mut Store>) -> Result<()> { + exports.call_test_imports(&mut *store)?; + let exports = exports.test_options_test(); + assert!(exports.call_option_none_result(&mut *store)?.is_none()); + assert_eq!(exports.call_option_some_result(&mut *store)?, Some("foo".to_string())); + exports.call_option_none_param(&mut *store, None)?; + exports.call_option_some_param(&mut *store, Some("foo"))?; + assert_eq!(exports.call_option_roundtrip(&mut *store,Some("foo"))?, Some("foo".to_string())); + Ok(()) +} diff --git a/tests/runtime/options/wasm.rs b/tests/runtime/options/wasm.rs new file mode 100644 index 000000000..576a012b3 --- /dev/null +++ b/tests/runtime/options/wasm.rs @@ -0,0 +1,43 @@ +wit_bindgen::generate!({ + path: "../../tests/runtime/options", + exports: { + world: Component, + "test:options/test": Component + }, +}); + +struct Component; + +impl Guest for Component { + fn test_imports() { + use test::options::test::*; + + option_none_param(None); + option_some_param(Some("foo")); + assert!(option_none_result().is_none()); + assert_eq!(option_some_result(), Some("foo".to_string())); + assert_eq!(option_roundtrip(Some("foo")), Some("foo".to_string())); + } +} + +impl exports::test::options::test::Guest for Component { + fn option_none_param(a: Option) { + assert!(a.is_none()); + } + + fn option_none_result() -> Option { + None + } + + fn option_some_param(a: Option) { + assert_eq!(a, Some("foo".to_string())); + } + + fn option_some_result() -> Option { + Some("foo".to_string()) + } + + fn option_roundtrip(a: Option) -> Option { + a + } +} diff --git a/tests/runtime/options/world.wit b/tests/runtime/options/world.wit new file mode 100644 index 000000000..212cc8f9a --- /dev/null +++ b/tests/runtime/options/world.wit @@ -0,0 +1,17 @@ +package test:options; + +interface test { + option-none-param: func(a: option); + option-some-param: func(a: option); + option-none-result: func() -> option; + option-some-result: func() -> option; + + option-roundtrip: func(a: option) -> option; +} + +world options { + import test; + export test; + + export test-imports: func(); +}