Debian Codegraph @

crates/wasm-wave/tests/ui.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use anyhow::{Context, Result};
2 use std::{collections::HashMap, fmt::Write, fs, path::Path, sync::OnceLock};
3
4 use wasm_wave::{
5 parser::ParserError,
6 untyped::UntypedFuncCall,
7 value::{FuncType, Value, resolve_wit_func_type},
8 wasm::{DisplayValue, WasmFunc},
9 };
10 use wit_parser::Resolve;
11
12 fn test(path: &Path) -> Result<String> {
13 let filename = path.file_name().unwrap().to_string_lossy();
14 let inputs = std::fs::read_to_string(path)?;
15 let mut output = String::new();
16 let out = &mut output;
17 let inputs = inputs.trim_end().trim_end_matches(';');
18 for mut input in inputs.split(";\n") {
19 // Copy leading comments into the output
20 while input.starts_with("//") {
21 let Some((comment, remainder)) = input.split_once('\n') else {
22 break;
23 };
24 input = remainder;
25 writeln!(out, "{comment}")?;
26 continue;
27 }
28
29 fn parse_func_call(
30 input: &str,
31 ) -> Result<(String, &'static FuncType, Vec<Value>), ParserError> {
32 let untyped_call = UntypedFuncCall::parse(input)?;
33 let func_name = untyped_call.name().to_string();
34 let func_type = get_func_type(&func_name).unwrap_or_else(|| {
35 panic!("unknown test func {func_name:?}");
36 });
37 let param_types = func_type.params().collect::<Vec<_>>();
38 let values = untyped_call.to_wasm_params::<Value>(&param_types)?;
39 Ok((func_name, func_type, values))
40 }
41
42 match parse_func_call(input) {
43 Ok((func_name, func_type, values)) => {
44 assert!(
45 !filename.starts_with("reject-"),
46 "accepted input {input:?} in {filename}"
47 );
48 write!(out, "{func_name}(")?;
49 let mut first = true;
50 for (name, value) in func_type.param_names().zip(values) {
51 if first {
52 first = false;
53 } else {
54 write!(out, ", ")?;
55 }
56 write!(out, "{name}: {value}", value = DisplayValue(&value))?;
57 }
58 writeln!(out, ")")?;
59 }
60 Err(err) => {
61 assert!(
62 !filename.starts_with("accept-"),
63 "rejected input {input:?} in {filename}: {err:#}"
64 );
65 writeln!(out, "{err}")?;
66 }
67 }
68 }
69 Ok(output)
70 }
71
72 fn get_func_type(func_name: &str) -> Option<&'static FuncType> {
73 static FUNC_TYPES: OnceLock<HashMap<String, FuncType>> = OnceLock::new();
74 FUNC_TYPES
75 .get_or_init(|| {
76 let mut resolve = Resolve::new();
77 resolve.push_path("tests/ui/ui.wit").unwrap();
78 resolve
79 .interfaces
80 .iter()
81 .flat_map(|(_, i)| &i.functions)
82 .map(|(name, func)| (name.clone(), resolve_wit_func_type(&resolve, func).unwrap()))
83 .collect::<HashMap<_, _>>()
84 })
85 .get(func_name)
86 }
87
88 // TODO: this ought to be be migrated to use `libtest-mimic`, which other crates in this workspace
89 // use as well
90 #[test]
91 fn ui() -> Result<()> {
92 for entry in fs::read_dir("tests/ui")? {
93 let path = entry?.path();
94 if path.extension().is_none() {
95 continue;
96 }
97 if path.extension().unwrap() != "waves" {
98 continue;
99 }
100
101 println!("testing {path:?}");
102 let actual = test(&path)?;
103 let expected = path.with_extension("out");
104 if std::env::var_os("BLESS").is_some() {
105 fs::write(&expected, actual)
106 .with_context(|| format!("failed to write {expected:?}"))?;
107 } else {
108 let expected = fs::read_to_string(&expected)
109 .with_context(|| format!("failed to read {expected:?}"))?;
110 assert_eq!(
111 expected, actual,
112 "expectation `{expected}` did not match actual `{actual}`",
113 );
114 }
115 }
116
117 Ok(())
118 }