Debian Codegraph @

crates/wit-encoder/tests/parse-to-encoder.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use pretty_assertions::assert_eq;
2 use wit_encoder::packages_from_parsed;
3
4 const WIT: &str = r#"package foo:functions;
5
6 interface foo-interface {
7 flags %flags {
8 flag-a,
9 flag-b,
10 }
11 }
12
13 interface bar-interface {
14 use foo-interface.{ %flags as external-type };
15 enum some-enum {
16 first-case,
17 second-case,
18 }
19 }
20
21 interface %interface {
22 use bar-interface.{ some-enum, external-type };
23 resource other-resource {
24 constructor(values: list<u32>);
25 static-method: func(arg: tuple<u32, u32>);
26 }
27 resource %resource {
28 constructor();
29 method: func(arg: list<u32>) -> char;
30 static-method: static func(arg: tuple<u32, u32>) -> list<u32>;
31 }
32 record some-record {
33 optinal: option<string>,
34 %own: %resource,
35 %borrow: borrow<%resource>,
36 result-a: result,
37 result-b: result<f64>,
38 result-c: result<_, f64>,
39 result-d: result<f64, f32>,
40 result-e: result<string, list<u8>>,
41 }
42 type type-handle-borrow = borrow<%resource>;
43 type type-handle-own = %resource;
44 type type-list = list<f64>;
45 type type-option = option<f64>;
46 type type-result-a = result;
47 type type-result-b = result<f64>;
48 type type-result-c = result<_, f64>;
49 type type-result-d = result<f64, f32>;
50 type type-result-e = result<string, list<u8>>;
51 type type-tuple = tuple<u32, list<u32>, borrow<%resource>>;
52 variant %variant {
53 empty-case,
54 valued-case(u32),
55 }
56 statndalone-func: func(a: u32, b: s32) -> f32;
57 }
58
59 world my-world {
60 import inline-import: interface {
61 do-something: func(a: string) -> string;
62 }
63 export inline-export: interface {
64 do-nothing: func();
65 }
66 import bar-interface;
67 import foo-interface;
68 import %interface;
69 export %interface;
70 import import-func: func();
71 export export-func: func(a: u32, b: s32) -> f32;
72 }
73 "#;
74
75 #[test]
76 fn round_trip() {
77 // First round trip
78 let mut resolve = wit_parser::Resolve::new();
79 resolve.push_str("", WIT).unwrap();
80 let packages = packages_from_parsed(&resolve);
81
82 assert_eq!(packages.len(), 1, "Should create exactly one package");
83 let package = &packages[0];
84 assert_eq!(WIT, package.to_string());
85
86 // Second round tip
87 let mut resolve = wit_parser::Resolve::new();
88 resolve.push_str("", &package.to_string()).unwrap();
89 let packages = packages_from_parsed(&resolve);
90
91 assert_eq!(packages.len(), 1, "Should create exactly one package");
92 let package = &packages[0];
93 assert_eq!(WIT, package.to_string());
94 }