2019-05-15 01:41:25 +00:00
|
|
|
use crate::case_result::CaseResult;
|
|
|
|
use crate::cases::*;
|
|
|
|
use crate::doc_header::DocHeader;
|
|
|
|
use crate::eth_specs::MinimalEthSpec;
|
|
|
|
use crate::yaml_decode::{extract_yaml_by_key, YamlDecode};
|
|
|
|
use crate::EfTest;
|
|
|
|
use serde_derive::Deserialize;
|
2019-05-14 01:13:28 +00:00
|
|
|
use std::{fs::File, io::prelude::*, path::PathBuf};
|
2019-05-14 23:50:05 +00:00
|
|
|
use types::{EthSpec, FoundationEthSpec};
|
2019-05-14 01:13:28 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2019-05-15 01:15:34 +00:00
|
|
|
pub struct Doc {
|
2019-05-14 01:13:28 +00:00
|
|
|
pub yaml: String,
|
2019-05-15 02:30:42 +00:00
|
|
|
pub path: PathBuf,
|
2019-05-14 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 01:15:34 +00:00
|
|
|
impl Doc {
|
2019-05-14 05:08:42 +00:00
|
|
|
fn from_path(path: PathBuf) -> Self {
|
2019-05-15 02:30:42 +00:00
|
|
|
let mut file = File::open(path.clone()).unwrap();
|
2019-05-14 01:13:28 +00:00
|
|
|
|
|
|
|
let mut yaml = String::new();
|
|
|
|
file.read_to_string(&mut yaml).unwrap();
|
|
|
|
|
2019-05-15 02:30:42 +00:00
|
|
|
Self { yaml, path }
|
2019-05-14 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 02:30:42 +00:00
|
|
|
pub fn test_results(&self) -> Vec<CaseResult> {
|
|
|
|
let header: DocHeader = serde_yaml::from_str(&self.yaml.as_str()).unwrap();
|
2019-05-14 01:13:28 +00:00
|
|
|
|
2019-05-14 05:08:42 +00:00
|
|
|
match (
|
|
|
|
header.runner.as_ref(),
|
|
|
|
header.handler.as_ref(),
|
|
|
|
header.config.as_ref(),
|
|
|
|
) {
|
2019-05-15 02:30:42 +00:00
|
|
|
("ssz", "uint", _) => run_test::<SszGeneric, FoundationEthSpec>(&self.yaml),
|
|
|
|
("ssz", "static", "minimal") => run_test::<SszStatic, MinimalEthSpec>(&self.yaml),
|
2019-05-14 05:08:42 +00:00
|
|
|
(runner, handler, config) => panic!(
|
|
|
|
"No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"",
|
|
|
|
runner, handler, config
|
2019-05-14 01:13:28 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_tests_pass(path: PathBuf) {
|
2019-05-15 02:30:42 +00:00
|
|
|
let doc = Self::from_path(path);
|
|
|
|
let results = doc.test_results();
|
2019-05-14 01:13:28 +00:00
|
|
|
|
2019-05-15 02:30:42 +00:00
|
|
|
if results.iter().any(|r| r.result.is_err()) {
|
|
|
|
print_failures(&doc, &results);
|
|
|
|
panic!("Tests failed (see above)");
|
2019-05-14 01:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:27:22 +00:00
|
|
|
pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<CaseResult>
|
2019-05-14 01:13:28 +00:00
|
|
|
where
|
2019-05-15 01:45:50 +00:00
|
|
|
Cases<T>: EfTest + YamlDecode,
|
2019-05-14 01:13:28 +00:00
|
|
|
{
|
2019-05-15 01:45:50 +00:00
|
|
|
// Extract only the "test_cases" YAML as a stand-alone string.
|
2019-05-14 05:08:42 +00:00
|
|
|
let test_cases_yaml = extract_yaml_by_key(test_doc_yaml, "test_cases");
|
2019-05-14 01:13:28 +00:00
|
|
|
|
2019-05-15 01:45:50 +00:00
|
|
|
// Pass only the "test_cases" YAML string to `yaml_decode`.
|
2019-05-15 01:17:32 +00:00
|
|
|
let test_cases: Cases<T> = Cases::yaml_decode(&test_cases_yaml.to_string()).unwrap();
|
2019-05-14 05:08:42 +00:00
|
|
|
|
2019-05-15 01:27:22 +00:00
|
|
|
test_cases.test_results::<E>()
|
2019-05-14 01:13:28 +00:00
|
|
|
}
|
2019-05-15 02:30:42 +00:00
|
|
|
|
|
|
|
pub fn print_failures(doc: &Doc, results: &[CaseResult]) {
|
|
|
|
let header: DocHeader = serde_yaml::from_str(&doc.yaml).unwrap();
|
|
|
|
let failures: Vec<&CaseResult> = results.iter().filter(|r| r.result.is_err()).collect();
|
|
|
|
|
|
|
|
println!("--------------------------------------------------");
|
|
|
|
println!("Test Failure");
|
|
|
|
println!("Title: {}", header.title);
|
|
|
|
println!("File: {:?}", doc.path);
|
|
|
|
println!("");
|
|
|
|
println!(
|
|
|
|
"{} tests, {} failures, {} passes.",
|
|
|
|
results.len(),
|
|
|
|
failures.len(),
|
|
|
|
results.len() - failures.len()
|
|
|
|
);
|
|
|
|
println!("");
|
|
|
|
|
|
|
|
for failure in failures {
|
|
|
|
println!("-------");
|
2019-05-15 03:23:52 +00:00
|
|
|
println!("case[{}].result:", failure.case_index);
|
|
|
|
println!("{:#?}", failure.result);
|
2019-05-15 02:30:42 +00:00
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|