Catch panicking tests

This commit is contained in:
Brandon Fish 2019-08-02 11:36:38 -06:00
parent 8098b7e44d
commit 21ea2465c0

View File

@ -105,6 +105,10 @@ mod tests {
use wasmer_runtime_core::Instance; use wasmer_runtime_core::Instance;
fn parse_and_run(path: &PathBuf) -> Result<(), String> { fn parse_and_run(path: &PathBuf) -> Result<(), String> {
// TODO Collect results
// TODO Add more assertions
// TODO
// TODO Allow running WAST &str directly
let source = fs::read(&path).unwrap(); let source = fs::read(&path).unwrap();
let filename = path.file_name().unwrap().to_str().unwrap(); let filename = path.file_name().unwrap().to_str().unwrap();
let source = fs::read(&path).unwrap(); let source = fs::read(&path).unwrap();
@ -115,7 +119,7 @@ mod tests {
.expect(&format!("Failed to parse script {}", &filename)); .expect(&format!("Failed to parse script {}", &filename));
let parse_result = parser.next(); let parse_result = parser.next();
use std::panic;
while let Some(Command { kind, line }) = while let Some(Command { kind, line }) =
parser.next().map_err(|e| format!("Parse err: {:?}", e))? parser.next().map_err(|e| format!("Parse err: {:?}", e))?
{ {
@ -123,14 +127,25 @@ mod tests {
println!("line: {:?}", line); println!("line: {:?}", line);
match kind { match kind {
CommandKind::Module { module, name } => { CommandKind::Module { module, name } => {
println!("in module"); println!("Module");
let module = let result = panic::catch_unwind(|| {
wasmer_runtime_core::compile_with(&module.into_vec(), &get_compiler()) let module =
.expect("WASM can't be compiled"); wasmer_runtime_core::compile_with(&module.into_vec(), &get_compiler())
let i = module .expect("WASM can't be compiled");
.instantiate(&ImportObject::new()) let i = module
.expect("WASM can't be instantiated"); .instantiate(&ImportObject::new())
instance = Some(i); .expect("WASM can't be instantiated");
i
});
match result {
Err(e) => {
println!("instantiate failed {:?}", e);
instance = None;
}
Ok(i) => {
instance = Some(i);
}
}
} }
CommandKind::AssertReturn { action, expected } => { CommandKind::AssertReturn { action, expected } => {
println!("in assert return"); println!("in assert return");
@ -144,18 +159,28 @@ mod tests {
CommandKind::AssertTrap { action, message } => println!("AssertTrap"), CommandKind::AssertTrap { action, message } => println!("AssertTrap"),
CommandKind::AssertInvalid { module, message } => println!("AssertInvalid"), CommandKind::AssertInvalid { module, message } => println!("AssertInvalid"),
CommandKind::AssertMalformed { module, message } => { CommandKind::AssertMalformed { module, message } => {
let module = println!("AssertMalformed");
wasmer_runtime_core::compile_with(&module.into_vec(), &get_compiler());
if let Err(CompileError::InternalError { msg }) = module { let result = panic::catch_unwind(|| {
println!("expected: {:?}", message); wasmer_runtime_core::compile_with(&module.into_vec(), &get_compiler())
println!("actual: {:?}", msg); });
} else if let Err(CompileError::ValidationError { msg }) = module {
println!("expected: {:?}", message); match result {
println!("actual: {:?}", msg); Ok(module) => {
} else { if let Err(CompileError::InternalError { msg }) = module {
println!("Should be malformed") println!("expected: {:?}", message);
println!("actual: {:?}", msg);
} else if let Err(CompileError::ValidationError { msg }) = module {
println!("expected: {:?}", message);
println!("actual: {:?}", msg);
} else {
println!("Should be malformed");
}
}
Err(p) => {
println!("caught panic {:?}", p);
}
} }
println!("AssertMalformed")
} }
CommandKind::AssertUninstantiable { module, message } => { CommandKind::AssertUninstantiable { module, message } => {
println!("AssertUninstantiable") println!("AssertUninstantiable")