Tidy up ef_tests
This commit is contained in:
parent
e53abe3f0b
commit
1f6e393ff0
@ -20,7 +20,7 @@ pub struct TestDoc<T> {
|
|||||||
pub test_cases: Vec<T>,
|
pub test_cases: Vec<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct SszGenericCase {
|
pub struct SszGenericCase {
|
||||||
#[serde(alias = "type")]
|
#[serde(alias = "type")]
|
||||||
pub type_name: String,
|
pub type_name: String,
|
||||||
@ -30,17 +30,18 @@ pub struct SszGenericCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct TestCaseResult {
|
pub struct TestCaseResult<T> {
|
||||||
pub description: String,
|
pub case_index: usize,
|
||||||
|
pub case: T,
|
||||||
pub result: Result<(), Error>,
|
pub result: Result<(), Error>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Test {
|
pub trait Test<T> {
|
||||||
fn test(&self) -> Vec<TestCaseResult>;
|
fn test(&self) -> Vec<TestCaseResult<T>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Test for TestDoc<SszGenericCase> {
|
impl Test<SszGenericCase> for TestDoc<SszGenericCase> {
|
||||||
fn test(&self) -> Vec<TestCaseResult> {
|
fn test(&self) -> Vec<TestCaseResult<SszGenericCase>> {
|
||||||
self.test_cases
|
self.test_cases
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -66,7 +67,8 @@ impl Test for TestDoc<SszGenericCase> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TestCaseResult {
|
TestCaseResult {
|
||||||
description: format!("Case {}: {:?}", i, tc),
|
case_index: i,
|
||||||
|
case: tc.clone(),
|
||||||
result,
|
result,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -74,28 +76,48 @@ impl Test for TestDoc<SszGenericCase> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compare_decoding<T>(should_pass: bool, ssz: &String, value: &String) -> Result<(), Error>
|
fn compare_decoding<T>(should_be_ok: bool, ssz: &String, value: &String) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Decode + TestDecode + Debug + PartialEq<T>,
|
T: Decode + TestDecode + Debug + PartialEq<T>,
|
||||||
{
|
{
|
||||||
let ssz = hex::decode(&ssz[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
let ssz = hex::decode(&ssz[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
||||||
let expected = T::test_decode(value)?;
|
|
||||||
|
let expected = if should_be_ok {
|
||||||
|
Some(T::test_decode(value)?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let decoded = T::from_ssz_bytes(&ssz);
|
let decoded = T::from_ssz_bytes(&ssz);
|
||||||
|
|
||||||
if should_pass {
|
compare_result(decoded, expected)
|
||||||
let decoded = decoded.map_err(|e| Error::NotEqual(format!("{:?}", e)))?;
|
}
|
||||||
|
|
||||||
if decoded != expected {
|
fn compare_result<T, E>(result: Result<T, E>, expected: Option<T>) -> Result<(), Error>
|
||||||
Err(Error::NotEqual(format!("{:?} != {:?}", decoded, expected)))
|
where
|
||||||
} else {
|
T: PartialEq<T> + Debug,
|
||||||
Ok(())
|
E: Debug,
|
||||||
}
|
{
|
||||||
} else {
|
match (result, expected) {
|
||||||
if let Ok(decoded) = decoded {
|
// Pass: The should have failed and did fail.
|
||||||
Err(Error::DidntFail(format!("Decoded as {:?}", decoded)))
|
(Err(_), None) => Ok(()),
|
||||||
} else {
|
// Fail: The test failed when it should have produced a result (fail).
|
||||||
Ok(())
|
(Err(e), Some(expected)) => Err(Error::NotEqual(format!(
|
||||||
|
"Got {:?} expected {:?}",
|
||||||
|
e, expected
|
||||||
|
))),
|
||||||
|
// Fail: The test produced a result when it should have failed (fail).
|
||||||
|
(Ok(result), None) => Err(Error::DidntFail(format!("Got {:?}", result))),
|
||||||
|
// Potential Pass: The test should have produced a result, and it did.
|
||||||
|
(Ok(result), Some(expected)) => {
|
||||||
|
if result == expected {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::NotEqual(format!(
|
||||||
|
"Got {:?} expected {:?}",
|
||||||
|
result, expected
|
||||||
|
)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,8 @@ fn ssz() {
|
|||||||
|
|
||||||
let results = doc.test();
|
let results = doc.test();
|
||||||
|
|
||||||
let failures: Vec<&TestCaseResult> = results.iter().filter(|r| r.result.is_err()).collect();
|
let failures: Vec<&TestCaseResult<SszGenericCase>> =
|
||||||
|
results.iter().filter(|r| r.result.is_err()).collect();
|
||||||
|
|
||||||
if !failures.is_empty() {
|
if !failures.is_empty() {
|
||||||
panic!("{:?}", failures);
|
panic!("{:?}", failures);
|
||||||
|
Loading…
Reference in New Issue
Block a user