Make tests generic across an EthSpec

This commit is contained in:
Paul Hauner 2019-05-14 17:40:17 +10:00
parent 2755621a9b
commit d1a6ac3a28
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
5 changed files with 27 additions and 15 deletions

View File

@ -2,14 +2,25 @@ use super::AttestationData;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use tree_hash_derive::{CachedTreeHash, TreeHash}; use tree_hash_derive::{CachedTreeHash, TreeHash};
/// Used for pairing an attestation with a proof-of-custody. /// Used for pairing an attestation with a proof-of-custody.
/// ///
/// Spec v0.5.1 /// 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 struct AttestationDataAndCustodyBit {
pub data: AttestationData, pub data: AttestationData,
pub custody_bit: bool, pub custody_bit: bool,

View File

@ -1,4 +1,5 @@
use super::*; use super::*;
use types::EthSpec;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct TestCaseResult { pub struct TestCaseResult {
@ -18,7 +19,7 @@ impl TestCaseResult {
} }
pub trait Test { pub trait Test {
fn test(&self) -> Vec<TestCaseResult>; fn test<E: EthSpec>(&self) -> Vec<TestCaseResult>;
} }
/// Compares `result` with `expected`. /// Compares `result` with `expected`.

View File

@ -1,5 +1,6 @@
use super::*; use super::*;
use std::{fs::File, io::prelude::*, path::PathBuf}; use std::{fs::File, io::prelude::*, path::PathBuf};
use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct TestDoc { pub struct TestDoc {
@ -26,8 +27,8 @@ impl TestDoc {
header.handler.as_ref(), header.handler.as_ref(),
header.config.as_ref(), header.config.as_ref(),
) { ) {
("ssz", "uint", _) => run_test::<SszGeneric>(&doc.yaml), ("ssz", "uint", _) => run_test::<SszGeneric, FoundationEthSpec>(&doc.yaml),
("ssz", "static", "minimal") => run_test::<SszStatic>(&doc.yaml), ("ssz", "static", "minimal") => run_test::<SszStatic, FewValidatorsEthSpec>(&doc.yaml),
(runner, handler, config) => panic!( (runner, handler, config) => panic!(
"No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"", "No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"",
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(); let failures: Vec<&TestCaseResult> = 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 {
dbg!(&f.case_index); dbg!(&f.case_index);
dbg!(&f.result); dbg!(&f.result);
} }
panic!() panic!("{}/{} tests failed.", failures.len(), results.len())
} }
} }
} }
pub fn run_test<T>(test_doc_yaml: &String) -> Vec<TestCaseResult> pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<TestCaseResult>
where where
TestDocCases<T>: Test + serde::de::DeserializeOwned + TestDecode, TestDocCases<T>: Test + serde::de::DeserializeOwned + TestDecode,
{ {
@ -59,5 +60,5 @@ where
let test_cases: TestDocCases<T> = let test_cases: TestDocCases<T> =
TestDocCases::test_decode(&test_cases_yaml.to_string()).unwrap(); TestDocCases::test_decode(&test_cases_yaml.to_string()).unwrap();
test_cases.test() test_cases.test::<E>()
} }

View File

@ -1,4 +1,5 @@
use super::*; use super::*;
use types::EthSpec;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct SszGeneric { pub struct SszGeneric {
@ -16,7 +17,7 @@ impl TestDecode for SszGeneric {
} }
impl Test for TestDocCases<SszGeneric> { impl Test for TestDocCases<SszGeneric> {
fn test(&self) -> Vec<TestCaseResult> { fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> {
self.test_cases self.test_cases
.iter() .iter()
.enumerate() .enumerate()

View File

@ -42,7 +42,7 @@ impl SszStatic {
} }
impl Test for TestDocCases<SszStatic> { impl Test for TestDocCases<SszStatic> {
fn test(&self) -> Vec<TestCaseResult> { fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> {
self.test_cases self.test_cases
.iter() .iter()
.enumerate() .enumerate()
@ -52,17 +52,15 @@ impl Test for TestDocCases<SszStatic> {
"Crosslink" => ssz_static_test::<Crosslink>(tc), "Crosslink" => ssz_static_test::<Crosslink>(tc),
"Eth1Data" => ssz_static_test::<Eth1Data>(tc), "Eth1Data" => ssz_static_test::<Eth1Data>(tc),
"AttestationData" => ssz_static_test::<AttestationData>(tc), "AttestationData" => ssz_static_test::<AttestationData>(tc),
/*
"AttestationDataAndCustodyBit" => { "AttestationDataAndCustodyBit" => {
ssz_static_test::<AttestationDataAndCustodyBit>(tc) ssz_static_test::<AttestationDataAndCustodyBit>(tc)
} }
*/
"IndexedAttestation" => ssz_static_test::<IndexedAttestation>(tc), "IndexedAttestation" => ssz_static_test::<IndexedAttestation>(tc),
"DepositData" => ssz_static_test::<DepositData>(tc), "DepositData" => ssz_static_test::<DepositData>(tc),
"BeaconBlockHeader" => ssz_static_test::<BeaconBlockHeader>(tc), "BeaconBlockHeader" => ssz_static_test::<BeaconBlockHeader>(tc),
"Validator" => ssz_static_test::<Validator>(tc), "Validator" => ssz_static_test::<Validator>(tc),
"PendingAttestation" => ssz_static_test::<PendingAttestation>(tc), "PendingAttestation" => ssz_static_test::<PendingAttestation>(tc),
// "HistoricalBatch" => ssz_static_test::<HistoricalBatch>(tc), "HistoricalBatch" => ssz_static_test::<HistoricalBatch<E>>(tc),
"ProposerSlashing" => ssz_static_test::<ProposerSlashing>(tc), "ProposerSlashing" => ssz_static_test::<ProposerSlashing>(tc),
"AttesterSlashing" => ssz_static_test::<AttesterSlashing>(tc), "AttesterSlashing" => ssz_static_test::<AttesterSlashing>(tc),
"Attestation" => ssz_static_test::<Attestation>(tc), "Attestation" => ssz_static_test::<Attestation>(tc),
@ -71,7 +69,7 @@ impl Test for TestDocCases<SszStatic> {
"Transfer" => ssz_static_test::<Transfer>(tc), "Transfer" => ssz_static_test::<Transfer>(tc),
"BeaconBlockBody" => ssz_static_test::<BeaconBlockBody>(tc), "BeaconBlockBody" => ssz_static_test::<BeaconBlockBody>(tc),
"BeaconBlock" => ssz_static_test::<BeaconBlock>(tc), "BeaconBlock" => ssz_static_test::<BeaconBlock>(tc),
// "BeaconState" => ssz_static_test::<DepositData>(tc), "BeaconState" => ssz_static_test::<BeaconState<E>>(tc),
_ => Err(Error::FailedToParseTest(format!( _ => Err(Error::FailedToParseTest(format!(
"Unknown type: {}", "Unknown type: {}",
tc.type_name tc.type_name