Add untested ssz_static test impl

This commit is contained in:
Paul Hauner 2019-05-14 10:01:20 +10:00
parent 55ff1e0b40
commit c3b4739a11
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
5 changed files with 72 additions and 1 deletions

View File

@ -11,3 +11,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_yaml = "0.8"
ssz = { path = "../../eth2/utils/ssz" }
types = { path = "../../eth2/types" }

View File

@ -5,10 +5,12 @@ use ssz::Decode;
use std::fmt::Debug;
use test_decode::TestDecode;
pub use crate::error::*;
pub use crate::ssz_generic::*;
mod error;
mod ssz_generic;
mod ssz_static;
mod test_decode;
#[derive(Debug, Deserialize)]

View File

@ -0,0 +1,49 @@
use super::*;
use types::Fork;
#[derive(Debug, Clone, Deserialize)]
pub struct SszStatic {
pub type_name: String,
pub value: String,
pub serialized: String,
pub root: String,
}
impl Test<SszStatic> for TestDoc<SszStatic> {
fn test(&self) -> Vec<TestCaseResult<SszStatic>> {
self.test_cases
.iter()
.enumerate()
.map(|(i, tc)| {
let result = match tc.type_name.as_ref() {
"Fork" => ssz_static_test::<Fork>(&tc.value, &tc.serialized, &tc.root),
_ => Err(Error::FailedToParseTest(format!(
"Unknown type: {}",
tc.type_name
))),
};
TestCaseResult {
case_index: i,
case: tc.clone(),
result,
}
})
.collect()
}
}
/// Execute a `ssz_generic` test case.
fn ssz_static_test<T>(value: &String, serialized: &String, root: &String) -> Result<(), Error>
where
T: Decode + TestDecode + Debug + PartialEq<T>,
{
let ssz =
hex::decode(&serialized[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let expected = T::test_decode(value)?;
let decoded = T::from_ssz_bytes(&ssz);
compare_result(decoded, Some(expected))
}

View File

@ -1,9 +1,12 @@
use super::*;
use types::Fork;
pub trait TestDecode: Sized {
/// Decode an object from the test specification YAML.
fn test_decode(string: &String) -> Result<Self, Error>;
}
/// Basic types can general be decoded with the `parse` fn if they implement `str::FromStr`.
macro_rules! impl_via_parse {
($ty: ty) => {
impl TestDecode for $ty {
@ -21,6 +24,8 @@ impl_via_parse!(u16);
impl_via_parse!(u32);
impl_via_parse!(u64);
/// Some `ethereum-types` methods have a `str::FromStr` implementation that expects `0x`-prefixed
/// hex, so we use `from_dec_str` instead.
macro_rules! impl_via_from_dec_str {
($ty: ty) => {
impl TestDecode for $ty {
@ -33,3 +38,17 @@ macro_rules! impl_via_from_dec_str {
impl_via_from_dec_str!(U128);
impl_via_from_dec_str!(U256);
/// Types that already implement `serde::Deserialize` can be decoded using `serde_yaml`.
macro_rules! impl_via_serde_yaml {
($ty: ty) => {
impl TestDecode for $ty {
fn test_decode(string: &String) -> Result<Self, Error> {
serde_yaml::from_str(string)
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_serde_yaml!(Fork);

View File

@ -18,7 +18,7 @@ fn load_test_case<T: DeserializeOwned>(test_name: &str) -> TestDoc<T> {
}
#[test]
fn ssz() {
fn ssz_generic() {
let doc: TestDoc<SszGeneric> = load_test_case("ssz_generic/uint/uint_bounds.yaml");
let results = doc.test();