Skip to content

Commit

Permalink
read arguments from json, walk folder to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaeff committed Feb 26, 2018
1 parent 276bfec commit c060077
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ clap = "2.26.2"
# serialization and deserialization
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
bincode = "0.8.0"
regex = "0.2"

Expand Down
6 changes: 0 additions & 6 deletions tests/code/add.code

This file was deleted.

1 change: 1 addition & 0 deletions tests/code/simple_add.arguments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2]
71 changes: 39 additions & 32 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,81 @@
extern crate assert_cli;
extern crate serde_json;

#[cfg(test)]
mod integration {
use assert_cli;
use std::fs::{File};
use std::path::Path;
use std::io;
use std::io::prelude::*;
use std::fs::{self};

// one possible implementation of walking a directory only visiting files
fn visit_dirs(dir: &Path) -> io::Result<()> {
use serde_json;
use serde_json::Value;

#[test]
fn test_compile_and_witness_dir() {
let dir = Path::new("./tests/code");
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
visit_dirs(&path)?;
} else {
assert_eq!(path.into_os_string().into_string().unwrap(), "abc".to_string());
if path.extension().unwrap() == "witness" {
let base = Path::new(Path::new(path.file_stem().unwrap()).file_stem().unwrap());
let prog = dir.join(base).with_extension("code");
let flat = dir.join(base).with_extension("expected.out.code");
let witness = dir.join(base).with_extension("expected.witness");
let args = dir.join(base).with_extension("arguments.json");
test_compile_and_witness(&prog, &flat, &args, &witness);
}
}
}
Ok(())
}

#[test]
fn simple_add() {
test_compile_and_witness("./tests/code/simple_add.code", "./tests/code/simple_add.expected.out.code", "-a 1 2", "./tests/code/simple_add.expected.witness")
}

#[test]
fn t() {
visit_dirs(Path::new("./tests/code"));
}

fn test_compile_and_witness(program_path: &str, expected_flattened_code_path: &str, arguments: &str, expected_witness_path: &str) {
let flattened_path = "./tests/tmp/out";
let flattened_code_path = "./tests/tmp/out.code";
let witness_path = "./tests/tmp/witness";
fn test_compile_and_witness(program_path: &Path, expected_flattened_code_path: &Path, arguments_path: &Path, expected_witness_path: &Path) {
let flattened_path = Path::new("./tests/tmp/out");
let flattened_code_path = Path::new("./tests/tmp/out.code");
let witness_path = Path::new("./tests/tmp/witness");

// compile
assert_cli::Assert::command(&["cargo", "run", "--", "compile", "-i", program_path, "-o", flattened_path])
assert_cli::Assert::command(&["cargo", "run", "--", "compile", "-i", program_path.to_str().unwrap(), "-o", flattened_path.to_str().unwrap()])
.succeeds()
.unwrap();

// compile
assert_cli::Assert::command(&["cargo", "run", "--", "compute-witness", "-i", flattened_path, "-o", witness_path, "-a", "1", "2"])
// compute
let arguments: Value = serde_json::from_reader(File::open(arguments_path).unwrap()).unwrap();

let arguments_str_list: Vec<String> = arguments.as_array().unwrap().iter().map(|i| match *i {
Value::Number(ref n) => n.to_string(),
_ => panic!(format!("Cannot read arguments. Check {}", arguments_path.to_str().unwrap()))
}).collect();

let mut compute = vec!["cargo", "run", "--", "compute-witness", "-i", flattened_path.to_str().unwrap(), "-o", witness_path.to_str().unwrap(), "-a"];

for arg in arguments_str_list.iter() {
compute.push(arg);
}
assert_cli::Assert::command(&compute)
.succeeds()
.unwrap();

// load the expected result
let mut expected_flattened_code_file = File::open(expected_flattened_code_path).unwrap();
let mut expected_flattened_code = String::new();
expected_flattened_code_file.read_to_string(&mut expected_flattened_code);
expected_flattened_code_file.read_to_string(&mut expected_flattened_code).unwrap();

// load the expected witness
let mut expected_witness_file = File::open(expected_witness_path).unwrap();
let mut expected_witness = String::new();
expected_witness_file.read_to_string(&mut expected_witness);
expected_witness_file.read_to_string(&mut expected_witness).unwrap();

// load the actual result
let mut flattened_code_file = File::open(flattened_code_path).unwrap();
let mut flattened_code = String::new();
flattened_code_file.read_to_string(&mut flattened_code);
flattened_code_file.read_to_string(&mut flattened_code).unwrap();

// load the actual witness
let mut witness_file = File::open(witness_path).unwrap();
let mut witness = String::new();
witness_file.read_to_string(&mut witness);
witness_file.read_to_string(&mut witness).unwrap();

// check equality
assert_eq!(flattened_code, expected_flattened_code);
Expand Down

0 comments on commit c060077

Please sign in to comment.