From dbea9f20eafb5b1a837b5b82afe6165b3c475cce Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 3 Jun 2017 21:20:52 +0300 Subject: [PATCH] ints running --- spec/src/run.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++-- spec/src/test.rs | 10 +++--- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/spec/src/run.rs b/spec/src/run.rs index 8a1f166..c4ad053 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -1,12 +1,52 @@ #![cfg(test)] -use std::env; +use std::{self, env}; use std::path::PathBuf; use std::process::Command; use std::fs::File; +use std::sync::Arc; use serde_json; 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) { + 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 { + test_vals.iter().map(runtime_value).collect::>() +} + +fn run_action(module: &ModuleInstance, action: &test::Action) + -> Result, InterpreterError> +{ + match *action { + test::Action::Invoke { ref field, ref args} => { + module.execute_export(field, runtime_values(args).into()) + } + } +} #[test] fn i32_tests() { @@ -31,5 +71,42 @@ fn i32_tests() { .expect("Failed to execute process"); 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::>(); + 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"); + } + } + } } \ No newline at end of file diff --git a/spec/src/test.rs b/spec/src/test.rs index 686022f..123a215 100644 --- a/spec/src/test.rs +++ b/spec/src/test.rs @@ -3,8 +3,8 @@ #[derive(Deserialize)] pub struct RuntimeValue { #[serde(rename = "type")] - value_type: String, - value: String, + pub value_type: String, + pub value: String, } #[derive(Deserialize)] @@ -34,7 +34,7 @@ pub enum Command { } #[derive(Deserialize)] -pub struct Commands { - source_filename: String, - commands: Vec, +pub struct Spec { + pub source_filename: String, + pub commands: Vec, } \ No newline at end of file