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