From 1f6e393ff0321694290ebb82d730109fe067c96e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 14 May 2019 09:27:27 +1000 Subject: [PATCH] Tidy up `ef_tests` --- tests/ef_tests/src/lib.rs | 66 +++++++++++++++++++++++------------ tests/ef_tests/tests/tests.rs | 3 +- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/tests/ef_tests/src/lib.rs b/tests/ef_tests/src/lib.rs index 0166a04ecf..a4093cf2ca 100644 --- a/tests/ef_tests/src/lib.rs +++ b/tests/ef_tests/src/lib.rs @@ -20,7 +20,7 @@ pub struct TestDoc { pub test_cases: Vec, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct SszGenericCase { #[serde(alias = "type")] pub type_name: String, @@ -30,17 +30,18 @@ pub struct SszGenericCase { } #[derive(Debug, PartialEq, Clone)] -pub struct TestCaseResult { - pub description: String, +pub struct TestCaseResult { + pub case_index: usize, + pub case: T, pub result: Result<(), Error>, } -pub trait Test { - fn test(&self) -> Vec; +pub trait Test { + fn test(&self) -> Vec>; } -impl Test for TestDoc { - fn test(&self) -> Vec { +impl Test for TestDoc { + fn test(&self) -> Vec> { self.test_cases .iter() .enumerate() @@ -66,7 +67,8 @@ impl Test for TestDoc { }; TestCaseResult { - description: format!("Case {}: {:?}", i, tc), + case_index: i, + case: tc.clone(), result, } }) @@ -74,28 +76,48 @@ impl Test for TestDoc { } } -fn compare_decoding(should_pass: bool, ssz: &String, value: &String) -> Result<(), Error> +fn compare_decoding(should_be_ok: bool, ssz: &String, value: &String) -> Result<(), Error> where T: Decode + TestDecode + Debug + PartialEq, { 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); - if should_pass { - let decoded = decoded.map_err(|e| Error::NotEqual(format!("{:?}", e)))?; + compare_result(decoded, expected) +} - if decoded != expected { - Err(Error::NotEqual(format!("{:?} != {:?}", decoded, expected))) - } else { - Ok(()) - } - } else { - if let Ok(decoded) = decoded { - Err(Error::DidntFail(format!("Decoded as {:?}", decoded))) - } else { - Ok(()) +fn compare_result(result: Result, expected: Option) -> Result<(), Error> +where + T: PartialEq + Debug, + E: Debug, +{ + match (result, expected) { + // Pass: The should have failed and did fail. + (Err(_), None) => Ok(()), + // Fail: The test failed when it should have produced a result (fail). + (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 + ))) + } } } } diff --git a/tests/ef_tests/tests/tests.rs b/tests/ef_tests/tests/tests.rs index 13d204c4c5..3f954da082 100644 --- a/tests/ef_tests/tests/tests.rs +++ b/tests/ef_tests/tests/tests.rs @@ -23,7 +23,8 @@ fn ssz() { let results = doc.test(); - let failures: Vec<&TestCaseResult> = results.iter().filter(|r| r.result.is_err()).collect(); + let failures: Vec<&TestCaseResult> = + results.iter().filter(|r| r.result.is_err()).collect(); if !failures.is_empty() { panic!("{:?}", failures);