-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
046261b
commit 7027baf
Showing
2 changed files
with
251 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,157 @@ | ||
use std::{error::Error, fs, path::{Path, PathBuf}}; | ||
|
||
mod run; | ||
|
||
#[test_log::test] | ||
pub fn spec_i32() { | ||
run::run_spec_test("./tests/specification/testsuite/../dummy.wast"); | ||
pub fn spec_dummy() { | ||
let path = "./tests/specification/dummy.wast"; | ||
let report = run::run_spec_test(path); | ||
println!("Report for {}:\n{}", path, report); | ||
} | ||
|
||
#[test_log::test] | ||
pub fn spec_tests() { | ||
let paths = get_wast_files(Path::new("./tests/specification/testsuite/")).expect("Failed to find testsuite"); | ||
for test_path in paths { | ||
let report = run::run_spec_test(test_path.to_str().unwrap()); | ||
println!("Report for {}:\n{}", test_path.display(), report); | ||
} | ||
} | ||
|
||
// See: https://stackoverflow.com/a/76820878 | ||
fn get_wast_files(base_path: &Path) -> Result<Vec<PathBuf>, std::io::Error> { | ||
let mut buf = vec![]; | ||
let entries = fs::read_dir(base_path)?; | ||
|
||
for entry in entries { | ||
let entry = entry?; | ||
let meta = entry.metadata()?; | ||
|
||
if meta.is_dir() { | ||
let mut subdir = get_wast_files(&entry.path())?; | ||
buf.append(&mut subdir); | ||
} | ||
|
||
if meta.is_file() && entry.path().extension().unwrap_or_default() == "wast" { | ||
buf.push(entry.path()); | ||
} | ||
} | ||
|
||
Ok(buf) | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
struct WasmErrorWrapper(wasm::Error); | ||
|
||
impl Error for WasmErrorWrapper { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
match &self.0 { | ||
wasm::Error::MalformedUtf8String(inner) => Some(inner), | ||
_ => None | ||
} | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
struct AssertEqError { | ||
left: String, | ||
right: String, | ||
} | ||
|
||
impl AssertEqError { | ||
pub fn assert_eq<T: std::fmt::Debug + PartialEq>(left: T, right: T) -> Result<(), Self> { | ||
if left != right { | ||
return Err(AssertEqError { | ||
left: format!("{:?}", left), | ||
right: format!("{:?}", right), | ||
}); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
impl Error for AssertEqError {} | ||
impl std::fmt::Display for AssertEqError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "assert_eq failed: left: {}, right: {}", self.left, self.right) | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
struct PanicError { | ||
message: String, | ||
} | ||
|
||
impl PanicError { | ||
pub fn new(message: String) -> Self { | ||
PanicError { message } | ||
} | ||
} | ||
|
||
impl Error for PanicError {} | ||
impl std::fmt::Display for PanicError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Panic: {}", self.message) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for WasmErrorWrapper { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "{}", self.0) | ||
} | ||
|
||
} | ||
|
||
type AssertReport = Vec<Result<WastSuccess, WastError>>; | ||
|
||
type WastTestReport = Result<AssertReport, WastError>; | ||
|
||
struct WastSuccess { | ||
filename: String, | ||
line_number: u32, | ||
command: String, | ||
} | ||
|
||
struct WastError { | ||
inner: Box<dyn Error>, | ||
filename: String, | ||
line_number: u32, | ||
command: String, | ||
} | ||
|
||
pub fn get_command(contents: &str, span: wast::token::Span) -> String { | ||
contents[span.offset() as usize..].lines().next().unwrap_or("<unknown>").to_string() | ||
} | ||
|
||
impl std::fmt::Display for WastError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "[ERROR] {} ({}:{})", self.command, self.filename, self.line_number)?; | ||
writeln!(f, "\t{}", self.inner)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for WastSuccess { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "[SUCCESS] {} ({}:{})", self.command, self.filename, self.line_number) | ||
} | ||
} | ||
|
||
pub fn display_report(report: WastTestReport) -> std::fmt::Result { | ||
let mut f:std::fmt::Formatter<'_> = std::fmt::Formatter::new(); | ||
match self { | ||
WastTestReport::Asserts(asserts) => { | ||
for assert in asserts { | ||
match assert { | ||
Ok(success) => write!(f, "{}", success), | ||
Err(error) => write!(f, "{}", error), | ||
}?; | ||
} | ||
}, | ||
WastTestReport::CompilationError(error) => write!(f, "{}", error)?, | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters