Skip to content

Commit 475ce00

Browse files
authored
Add runtime test for options (bytecodealliance#827)
* Runtime test for options * cargo fmt
1 parent 21a46c7 commit 475ce00

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

crates/test-rust-wasm/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ test = false
4444
name = "results"
4545
test = false
4646

47+
[[bin]]
48+
name = "options"
49+
test = false
50+
4751
[[bin]]
4852
name = "owning"
4953
test = false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include!("../../../../tests/runtime/options/wasm.rs");
2+
3+
fn main() {}

tests/runtime/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod flavorful;
1616
mod lists;
1717
mod many_arguments;
1818
mod numbers;
19+
mod options;
1920
mod other_dependencies;
2021
mod ownership;
2122
mod records;

tests/runtime/options.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

tests/runtime/options/wasm.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
wit_bindgen::generate!({
2+
path: "../../tests/runtime/options",
3+
exports: {
4+
world: Component,
5+
"test:options/test": Component
6+
},
7+
});
8+
9+
struct Component;
10+
11+
impl Guest for Component {
12+
fn test_imports() {
13+
use test::options::test::*;
14+
15+
option_none_param(None);
16+
option_some_param(Some("foo"));
17+
assert!(option_none_result().is_none());
18+
assert_eq!(option_some_result(), Some("foo".to_string()));
19+
assert_eq!(option_roundtrip(Some("foo")), Some("foo".to_string()));
20+
}
21+
}
22+
23+
impl exports::test::options::test::Guest for Component {
24+
fn option_none_param(a: Option<String>) {
25+
assert!(a.is_none());
26+
}
27+
28+
fn option_none_result() -> Option<String> {
29+
None
30+
}
31+
32+
fn option_some_param(a: Option<String>) {
33+
assert_eq!(a, Some("foo".to_string()));
34+
}
35+
36+
fn option_some_result() -> Option<String> {
37+
Some("foo".to_string())
38+
}
39+
40+
fn option_roundtrip(a: Option<String>) -> Option<String> {
41+
a
42+
}
43+
}

tests/runtime/options/world.wit

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test:options;
2+
3+
interface test {
4+
option-none-param: func(a: option<string>);
5+
option-some-param: func(a: option<string>);
6+
option-none-result: func() -> option<string>;
7+
option-some-result: func() -> option<string>;
8+
9+
option-roundtrip: func(a: option<string>) -> option<string>;
10+
}
11+
12+
world options {
13+
import test;
14+
export test;
15+
16+
export test-imports: func();
17+
}

0 commit comments

Comments
 (0)