|
| 1 | +use anyhow::Result; |
| 2 | +use wasmtime::Store; |
| 3 | + |
| 4 | +wasmtime::component::bindgen!(in "tests/runtime/options"); |
| 5 | + |
| 6 | +#[derive(Default)] |
| 7 | +pub struct MyImports; |
| 8 | + |
| 9 | +impl test::options::test::Host for MyImports { |
| 10 | + fn option_none_param(&mut self, a: Option<String>) -> Result<()> { |
| 11 | + assert!(a.is_none()); |
| 12 | + Ok(()) |
| 13 | + } |
| 14 | + |
| 15 | + fn option_none_result(&mut self) -> Result<Option<String>> { |
| 16 | + Ok(None) |
| 17 | + } |
| 18 | + |
| 19 | + fn option_some_param(&mut self, a: Option<String>) -> Result<()> { |
| 20 | + assert_eq!(a, Some("foo".to_string())); |
| 21 | + Ok(()) |
| 22 | + } |
| 23 | + |
| 24 | + fn option_some_result(&mut self) -> Result<Option<String>> { |
| 25 | + Ok(Some("foo".to_string())) |
| 26 | + } |
| 27 | + |
| 28 | + fn option_roundtrip(&mut self, a: Option<String>) -> Result<Option<String>> { |
| 29 | + Ok(a) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn run() -> Result<()> { |
| 35 | + crate::run_test( |
| 36 | + "options", |
| 37 | + |linker| Options::add_to_linker(linker, |x| &mut x.0), |
| 38 | + |store, component, linker| Options::instantiate(store, component, linker), |
| 39 | + run_test, |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +fn run_test(exports: Options, store: &mut Store<crate::Wasi<MyImports>>) -> Result<()> { |
| 44 | + exports.call_test_imports(&mut *store)?; |
| 45 | + let exports = exports.test_options_test(); |
| 46 | + assert!(exports.call_option_none_result(&mut *store)?.is_none()); |
| 47 | + assert_eq!( |
| 48 | + exports.call_option_some_result(&mut *store)?, |
| 49 | + Some("foo".to_string()) |
| 50 | + ); |
| 51 | + exports.call_option_none_param(&mut *store, None)?; |
| 52 | + exports.call_option_some_param(&mut *store, Some("foo"))?; |
| 53 | + assert_eq!( |
| 54 | + exports.call_option_roundtrip(&mut *store, Some("foo"))?, |
| 55 | + Some("foo".to_string()) |
| 56 | + ); |
| 57 | + Ok(()) |
| 58 | +} |
0 commit comments