Rename Test trait to EfTest

This commit is contained in:
Paul Hauner 2019-05-15 11:27:22 +10:00
parent 57040efc2a
commit 4cb23dd342
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
5 changed files with 22 additions and 19 deletions

View File

@ -1,16 +1,15 @@
use super::*; use super::*;
use types::EthSpec;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct TestCaseResult { pub struct CaseResult {
pub case_index: usize, pub case_index: usize,
pub desc: String, pub desc: String,
pub result: Result<(), Error>, pub result: Result<(), Error>,
} }
impl TestCaseResult { impl CaseResult {
pub fn new<T: Debug>(case_index: usize, case: &T, result: Result<(), Error>) -> Self { pub fn new<T: Debug>(case_index: usize, case: &T, result: Result<(), Error>) -> Self {
TestCaseResult { CaseResult {
case_index, case_index,
desc: format!("{:?}", case), desc: format!("{:?}", case),
result, result,
@ -18,10 +17,6 @@ impl TestCaseResult {
} }
} }
pub trait Test {
fn test<E: EthSpec>(&self) -> Vec<TestCaseResult>;
}
/// Compares `result` with `expected`. /// Compares `result` with `expected`.
/// ///
/// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and /// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and

View File

@ -16,8 +16,8 @@ impl YamlDecode for SszGeneric {
} }
} }
impl Test for Cases<SszGeneric> { impl EfTest for Cases<SszGeneric> {
fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> { fn test_results<E: EthSpec>(&self) -> Vec<CaseResult> {
self.test_cases self.test_cases
.iter() .iter()
.enumerate() .enumerate()
@ -42,7 +42,7 @@ impl Test for Cases<SszGeneric> {
Ok(()) Ok(())
}; };
TestCaseResult::new(i, tc, result) CaseResult::new(i, tc, result)
}) })
.collect() .collect()
} }

View File

@ -41,8 +41,8 @@ impl SszStatic {
} }
} }
impl Test for Cases<SszStatic> { impl EfTest for Cases<SszStatic> {
fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> { fn test_results<E: EthSpec>(&self) -> Vec<CaseResult> {
self.test_cases self.test_cases
.iter() .iter()
.enumerate() .enumerate()
@ -76,7 +76,7 @@ impl Test for Cases<SszStatic> {
))), ))),
}; };
TestCaseResult::new(i, tc, result) CaseResult::new(i, tc, result)
}) })
.collect() .collect()
} }

View File

@ -18,7 +18,7 @@ impl Doc {
Self { yaml } Self { yaml }
} }
pub fn get_test_results(path: PathBuf) -> Vec<TestCaseResult> { pub fn get_test_results(path: PathBuf) -> Vec<CaseResult> {
let doc = Self::from_path(path); let doc = Self::from_path(path);
let header: DocHeader = serde_yaml::from_str(&doc.yaml.as_str()).unwrap(); let header: DocHeader = serde_yaml::from_str(&doc.yaml.as_str()).unwrap();
@ -40,7 +40,7 @@ impl Doc {
pub fn assert_tests_pass(path: PathBuf) { pub fn assert_tests_pass(path: PathBuf) {
let results = Self::get_test_results(path); let results = Self::get_test_results(path);
let failures: Vec<&TestCaseResult> = results.iter().filter(|r| r.result.is_err()).collect(); let failures: Vec<&CaseResult> = results.iter().filter(|r| r.result.is_err()).collect();
if !failures.is_empty() { if !failures.is_empty() {
for f in &failures { for f in &failures {
@ -52,13 +52,13 @@ impl Doc {
} }
} }
pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<TestCaseResult> pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<CaseResult>
where where
Cases<T>: Test + serde::de::DeserializeOwned + YamlDecode, Cases<T>: EfTest + serde::de::DeserializeOwned + YamlDecode,
{ {
let test_cases_yaml = extract_yaml_by_key(test_doc_yaml, "test_cases"); let test_cases_yaml = extract_yaml_by_key(test_doc_yaml, "test_cases");
let test_cases: Cases<T> = Cases::yaml_decode(&test_cases_yaml.to_string()).unwrap(); let test_cases: Cases<T> = Cases::yaml_decode(&test_cases_yaml.to_string()).unwrap();
test_cases.test::<E>() test_cases.test_results::<E>()
} }

View File

@ -3,6 +3,7 @@ use ethereum_types::{U128, U256};
use serde_derive::Deserialize; use serde_derive::Deserialize;
use ssz::Decode; use ssz::Decode;
use std::fmt::Debug; use std::fmt::Debug;
use types::EthSpec;
pub use crate::case_result::*; pub use crate::case_result::*;
pub use crate::cases::*; pub use crate::cases::*;
@ -19,3 +20,10 @@ mod doc_header;
mod error; mod error;
mod eth_specs; mod eth_specs;
mod yaml_decode; mod yaml_decode;
/// Defined where an object can return the results of some test(s) adhering to the Ethereum
/// Foundation testing format.
pub trait EfTest {
/// Returns the results of executing one or more tests.
fn test_results<E: EthSpec>(&self) -> Vec<CaseResult>;
}