Debian Codegraph @

crates/wit-encoder/tests/functions.rs

raw
1 use pretty_assertions::assert_eq;
2 use wit_encoder::{Interface, Package, PackageName, Params, Result_, StandaloneFunc, Type};
3
4 const PACKAGE: &str = indoc::indoc! {"
5 package foo:functions;
6
7 interface functions {
8 f1: func();
9 f2: func(a: u32);
10 f4: func() -> u32;
11 f6: func() -> tuple<u32, u32>;
12 f7: func(a: f32, b: f32) -> tuple<u32, u32>;
13 f8: func(a: option<u32>) -> result<u32, f32>;
14 f11: func() -> result<f32>;
15 f12: func() -> result<_, f32>;
16 f13: func() -> result;
17 }
18 "};
19
20 #[test]
21 fn concrete_types() {
22 let name = PackageName::new("foo", "functions", None);
23 let mut package = Package::new(name);
24
25 package.interface({
26 let mut interface = Interface::new("functions");
27 interface.function(StandaloneFunc::new("f1", false));
28 interface.function({
29 let mut func = StandaloneFunc::new("f2", false);
30 func.set_params(Params::from_iter([("a", Type::U32)]));
31 func
32 });
33 interface.function({
34 let mut func = StandaloneFunc::new("f4", false);
35 func.set_result(Some(Type::U32));
36 func
37 });
38 interface.function({
39 let mut func = StandaloneFunc::new("f6", false);
40 func.set_result(Some(Type::tuple(vec![Type::U32, Type::U32])));
41 func
42 });
43 interface.function({
44 let mut func = StandaloneFunc::new("f7", false);
45 func.set_params(Params::from_iter([("a", Type::F32), ("b", Type::F32)]));
46 func.set_result(Some(Type::tuple(vec![Type::U32, Type::U32])));
47 func
48 });
49 interface.function({
50 let mut func = StandaloneFunc::new("f8", false);
51 func.set_params(Params::from_iter([("a", Type::option(Type::U32))]));
52 func.set_result(Some(Type::result(Result_::both(Type::U32, Type::F32))));
53 func
54 });
55 interface.function({
56 let mut func = StandaloneFunc::new("f11", false);
57 func.set_result(Some(Result_::ok(Type::F32).into()));
58 func
59 });
60 interface.function({
61 let mut func = StandaloneFunc::new("f12", false);
62 func.set_result(Some(Result_::err(Type::F32).into()));
63 func
64 });
65 interface.function({
66 let mut func = StandaloneFunc::new("f13", false);
67 func.set_result(Some(Result_::empty().into()));
68 func
69 });
70 interface
71 });
72
73 assert_eq!(package.to_string(), PACKAGE);
74 }