lighthouse/tests/ef_tests/src/test_doc.rs

66 lines
1.9 KiB
Rust
Raw Normal View History

2019-05-14 01:13:28 +00:00
use super::*;
2019-05-15 01:12:49 +00:00
use crate::yaml_decode::*;
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)]
pub struct TestDoc {
pub yaml: String,
}
impl TestDoc {
2019-05-14 05:08:42 +00:00
fn from_path(path: PathBuf) -> Self {
2019-05-14 01:13:28 +00:00
let mut file = File::open(path).unwrap();
let mut yaml = String::new();
file.read_to_string(&mut yaml).unwrap();
Self { yaml }
}
pub fn get_test_results(path: PathBuf) -> Vec<TestCaseResult> {
2019-05-14 05:08:42 +00:00
let doc = Self::from_path(path);
2019-05-14 01:13:28 +00:00
let header: TestDocHeader = serde_yaml::from_str(&doc.yaml.as_str()).unwrap();
2019-05-14 05:08:42 +00:00
match (
header.runner.as_ref(),
header.handler.as_ref(),
header.config.as_ref(),
) {
2019-05-14 07:40:17 +00:00
("ssz", "uint", _) => run_test::<SszGeneric, FoundationEthSpec>(&doc.yaml),
2019-05-14 23:50:05 +00:00
("ssz", "static", "minimal") => run_test::<SszStatic, MinimalEthSpec>(&doc.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) {
let results = Self::get_test_results(path);
let failures: Vec<&TestCaseResult> = results.iter().filter(|r| r.result.is_err()).collect();
if !failures.is_empty() {
2019-05-14 07:40:17 +00:00
for f in &failures {
2019-05-14 05:08:42 +00:00
dbg!(&f.case_index);
dbg!(&f.result);
}
2019-05-14 07:40:17 +00:00
panic!("{}/{} tests failed.", failures.len(), results.len())
2019-05-14 01:13:28 +00:00
}
}
}
2019-05-14 07:40:17 +00:00
pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<TestCaseResult>
2019-05-14 01:13:28 +00:00
where
2019-05-15 01:12:49 +00:00
TestDocCases<T>: Test + serde::de::DeserializeOwned + YamlDecode,
2019-05-14 01:13:28 +00:00
{
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-14 05:08:42 +00:00
let test_cases: TestDocCases<T> =
2019-05-15 01:12:49 +00:00
TestDocCases::yaml_decode(&test_cases_yaml.to_string()).unwrap();
2019-05-14 05:08:42 +00:00
2019-05-14 07:40:17 +00:00
test_cases.test::<E>()
2019-05-14 01:13:28 +00:00
}