mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-29 06:32:17 +00:00
ints running
This commit is contained in:
@ -1,12 +1,52 @@
|
|||||||
#![cfg(test)]
|
#![cfg(test)]
|
||||||
|
|
||||||
use std::env;
|
use std::{self, env};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use test;
|
use test;
|
||||||
|
use parity_wasm;
|
||||||
|
use parity_wasm::interpreter::{
|
||||||
|
ProgramInstance, ModuleInstance, ModuleInstanceInterface,
|
||||||
|
Error as InterpreterError,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn setup_program(base_dir: &str, test_module_path: &str) -> (ProgramInstance, Arc<ModuleInstance>) {
|
||||||
|
let mut wasm_path = PathBuf::from(base_dir.clone());
|
||||||
|
wasm_path.push(test_module_path);
|
||||||
|
let module = parity_wasm::deserialize_file(&wasm_path)
|
||||||
|
.expect(&format!("Wasm file {} failed to load", wasm_path.to_string_lossy()));
|
||||||
|
let program = ProgramInstance::new().expect("Failed creating program");
|
||||||
|
let module_instance = program.add_module("test", module).expect("Failed adding module");
|
||||||
|
(program, module_instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runtime_value(test_val: &test::RuntimeValue) -> parity_wasm::RuntimeValue {
|
||||||
|
match test_val.value_type.as_ref() {
|
||||||
|
"i32" => {
|
||||||
|
let unsigned: u32 = test_val.value.parse().expect("Literal parse error");
|
||||||
|
parity_wasm::RuntimeValue::I32(unsigned as i32)
|
||||||
|
},
|
||||||
|
_ => panic!("Unknwon runtime value type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runtime_values(test_vals: &[test::RuntimeValue]) -> Vec<parity_wasm::RuntimeValue> {
|
||||||
|
test_vals.iter().map(runtime_value).collect::<Vec<parity_wasm::RuntimeValue>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_action(module: &ModuleInstance, action: &test::Action)
|
||||||
|
-> Result<Option<parity_wasm::RuntimeValue>, InterpreterError>
|
||||||
|
{
|
||||||
|
match *action {
|
||||||
|
test::Action::Invoke { ref field, ref args} => {
|
||||||
|
module.execute_export(field, runtime_values(args).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn i32_tests() {
|
fn i32_tests() {
|
||||||
@ -31,5 +71,42 @@ fn i32_tests() {
|
|||||||
.expect("Failed to execute process");
|
.expect("Failed to execute process");
|
||||||
|
|
||||||
let mut f = File::open(&format!("{}/{}.json", outdir, spec_name)).unwrap();
|
let mut f = File::open(&format!("{}/{}.json", outdir, spec_name)).unwrap();
|
||||||
let commands: test::Commands = serde_json::from_reader(&mut f).unwrap();
|
let spec: test::Spec = serde_json::from_reader(&mut f).unwrap();
|
||||||
|
|
||||||
|
let first_command = &spec.commands[0];
|
||||||
|
let (mut program, mut module) = match first_command {
|
||||||
|
&test::Command::Module { line, ref filename } => {
|
||||||
|
setup_program(&outdir, filename)
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
panic!("First command supposed to specify module");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for command in spec.commands.iter().skip(1) {
|
||||||
|
match command {
|
||||||
|
&test::Command::Module { line, ref filename } => {
|
||||||
|
let (new_program, new_module) = setup_program(&outdir, &filename);
|
||||||
|
program = new_program;
|
||||||
|
module = new_module;
|
||||||
|
},
|
||||||
|
&test::Command::AssertReturn { line, ref action, ref expected } => {
|
||||||
|
let result = run_action(&*module, action);
|
||||||
|
match result {
|
||||||
|
Ok(result) => {
|
||||||
|
let spec_expected = runtime_values(expected);
|
||||||
|
let actual_result = result.into_iter().collect::<Vec<parity_wasm::RuntimeValue>>();
|
||||||
|
assert_eq!(actual_result, spec_expected);
|
||||||
|
println!("Action at line {} - success", line);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
panic!("Expected action to return value, got error: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
panic!("Unsupported fixture");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,8 +3,8 @@
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct RuntimeValue {
|
pub struct RuntimeValue {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
value_type: String,
|
pub value_type: String,
|
||||||
value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -34,7 +34,7 @@ pub enum Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Commands {
|
pub struct Spec {
|
||||||
source_filename: String,
|
pub source_filename: String,
|
||||||
commands: Vec<Command>,
|
pub commands: Vec<Command>,
|
||||||
}
|
}
|
Reference in New Issue
Block a user