From d1a6ac3a283ac4ce35104f329ac63a260fb52f97 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 14 May 2019 17:40:17 +1000 Subject: [PATCH] Make tests generic across an EthSpec --- .../types/src/attestation_data_and_custody_bit.rs | 15 +++++++++++++-- tests/ef_tests/src/test_case_result.rs | 3 ++- tests/ef_tests/src/test_doc.rs | 13 +++++++------ tests/ef_tests/src/test_doc_cases/ssz_generic.rs | 3 ++- tests/ef_tests/src/test_doc_cases/ssz_static.rs | 8 +++----- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index f1437cb54..43436938b 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -2,14 +2,25 @@ use super::AttestationData; use crate::test_utils::TestRandom; use rand::RngCore; -use serde_derive::Serialize; +use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; use tree_hash_derive::{CachedTreeHash, TreeHash}; /// Used for pairing an attestation with a proof-of-custody. /// /// Spec v0.5.1 -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash, CachedTreeHash)] +#[derive( + Debug, + Clone, + PartialEq, + Default, + Serialize, + Deserialize, + Encode, + Decode, + TreeHash, + CachedTreeHash, +)] pub struct AttestationDataAndCustodyBit { pub data: AttestationData, pub custody_bit: bool, diff --git a/tests/ef_tests/src/test_case_result.rs b/tests/ef_tests/src/test_case_result.rs index f16036922..9e3a97613 100644 --- a/tests/ef_tests/src/test_case_result.rs +++ b/tests/ef_tests/src/test_case_result.rs @@ -1,4 +1,5 @@ use super::*; +use types::EthSpec; #[derive(Debug, PartialEq, Clone)] pub struct TestCaseResult { @@ -18,7 +19,7 @@ impl TestCaseResult { } pub trait Test { - fn test(&self) -> Vec; + fn test(&self) -> Vec; } /// Compares `result` with `expected`. diff --git a/tests/ef_tests/src/test_doc.rs b/tests/ef_tests/src/test_doc.rs index 46769d29b..87382b989 100644 --- a/tests/ef_tests/src/test_doc.rs +++ b/tests/ef_tests/src/test_doc.rs @@ -1,5 +1,6 @@ use super::*; use std::{fs::File, io::prelude::*, path::PathBuf}; +use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec}; #[derive(Debug, Deserialize)] pub struct TestDoc { @@ -26,8 +27,8 @@ impl TestDoc { header.handler.as_ref(), header.config.as_ref(), ) { - ("ssz", "uint", _) => run_test::(&doc.yaml), - ("ssz", "static", "minimal") => run_test::(&doc.yaml), + ("ssz", "uint", _) => run_test::(&doc.yaml), + ("ssz", "static", "minimal") => run_test::(&doc.yaml), (runner, handler, config) => panic!( "No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"", runner, handler, config @@ -41,16 +42,16 @@ impl TestDoc { let failures: Vec<&TestCaseResult> = results.iter().filter(|r| r.result.is_err()).collect(); if !failures.is_empty() { - for f in failures { + for f in &failures { dbg!(&f.case_index); dbg!(&f.result); } - panic!() + panic!("{}/{} tests failed.", failures.len(), results.len()) } } } -pub fn run_test(test_doc_yaml: &String) -> Vec +pub fn run_test(test_doc_yaml: &String) -> Vec where TestDocCases: Test + serde::de::DeserializeOwned + TestDecode, { @@ -59,5 +60,5 @@ where let test_cases: TestDocCases = TestDocCases::test_decode(&test_cases_yaml.to_string()).unwrap(); - test_cases.test() + test_cases.test::() } diff --git a/tests/ef_tests/src/test_doc_cases/ssz_generic.rs b/tests/ef_tests/src/test_doc_cases/ssz_generic.rs index da932df53..48fdd0b58 100644 --- a/tests/ef_tests/src/test_doc_cases/ssz_generic.rs +++ b/tests/ef_tests/src/test_doc_cases/ssz_generic.rs @@ -1,4 +1,5 @@ use super::*; +use types::EthSpec; #[derive(Debug, Clone, Deserialize)] pub struct SszGeneric { @@ -16,7 +17,7 @@ impl TestDecode for SszGeneric { } impl Test for TestDocCases { - fn test(&self) -> Vec { + fn test(&self) -> Vec { self.test_cases .iter() .enumerate() diff --git a/tests/ef_tests/src/test_doc_cases/ssz_static.rs b/tests/ef_tests/src/test_doc_cases/ssz_static.rs index 49e1a9208..5948fdb25 100644 --- a/tests/ef_tests/src/test_doc_cases/ssz_static.rs +++ b/tests/ef_tests/src/test_doc_cases/ssz_static.rs @@ -42,7 +42,7 @@ impl SszStatic { } impl Test for TestDocCases { - fn test(&self) -> Vec { + fn test(&self) -> Vec { self.test_cases .iter() .enumerate() @@ -52,17 +52,15 @@ impl Test for TestDocCases { "Crosslink" => ssz_static_test::(tc), "Eth1Data" => ssz_static_test::(tc), "AttestationData" => ssz_static_test::(tc), - /* "AttestationDataAndCustodyBit" => { ssz_static_test::(tc) } - */ "IndexedAttestation" => ssz_static_test::(tc), "DepositData" => ssz_static_test::(tc), "BeaconBlockHeader" => ssz_static_test::(tc), "Validator" => ssz_static_test::(tc), "PendingAttestation" => ssz_static_test::(tc), - // "HistoricalBatch" => ssz_static_test::(tc), + "HistoricalBatch" => ssz_static_test::>(tc), "ProposerSlashing" => ssz_static_test::(tc), "AttesterSlashing" => ssz_static_test::(tc), "Attestation" => ssz_static_test::(tc), @@ -71,7 +69,7 @@ impl Test for TestDocCases { "Transfer" => ssz_static_test::(tc), "BeaconBlockBody" => ssz_static_test::(tc), "BeaconBlock" => ssz_static_test::(tc), - // "BeaconState" => ssz_static_test::(tc), + "BeaconState" => ssz_static_test::>(tc), _ => Err(Error::FailedToParseTest(format!( "Unknown type: {}", tc.type_name