Debian Codegraph @

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

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