Add untested ssz_static test impl
This commit is contained in:
parent
55ff1e0b40
commit
c3b4739a11
@ -11,3 +11,4 @@ serde = "1.0"
|
|||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
serde_yaml = "0.8"
|
serde_yaml = "0.8"
|
||||||
ssz = { path = "../../eth2/utils/ssz" }
|
ssz = { path = "../../eth2/utils/ssz" }
|
||||||
|
types = { path = "../../eth2/types" }
|
||||||
|
@ -5,10 +5,12 @@ use ssz::Decode;
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use test_decode::TestDecode;
|
use test_decode::TestDecode;
|
||||||
|
|
||||||
|
pub use crate::error::*;
|
||||||
pub use crate::ssz_generic::*;
|
pub use crate::ssz_generic::*;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod ssz_generic;
|
mod ssz_generic;
|
||||||
|
mod ssz_static;
|
||||||
mod test_decode;
|
mod test_decode;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
49
tests/ef_tests/src/ssz_static.rs
Normal file
49
tests/ef_tests/src/ssz_static.rs
Normal 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))
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use types::Fork;
|
||||||
|
|
||||||
pub trait TestDecode: Sized {
|
pub trait TestDecode: Sized {
|
||||||
|
/// Decode an object from the test specification YAML.
|
||||||
fn test_decode(string: &String) -> Result<Self, Error>;
|
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 {
|
macro_rules! impl_via_parse {
|
||||||
($ty: ty) => {
|
($ty: ty) => {
|
||||||
impl TestDecode for $ty {
|
impl TestDecode for $ty {
|
||||||
@ -21,6 +24,8 @@ impl_via_parse!(u16);
|
|||||||
impl_via_parse!(u32);
|
impl_via_parse!(u32);
|
||||||
impl_via_parse!(u64);
|
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 {
|
macro_rules! impl_via_from_dec_str {
|
||||||
($ty: ty) => {
|
($ty: ty) => {
|
||||||
impl TestDecode for $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!(U128);
|
||||||
impl_via_from_dec_str!(U256);
|
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);
|
||||||
|
@ -18,7 +18,7 @@ fn load_test_case<T: DeserializeOwned>(test_name: &str) -> TestDoc<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ssz() {
|
fn ssz_generic() {
|
||||||
let doc: TestDoc<SszGeneric> = load_test_case("ssz_generic/uint/uint_bounds.yaml");
|
let doc: TestDoc<SszGeneric> = load_test_case("ssz_generic/uint/uint_bounds.yaml");
|
||||||
|
|
||||||
let results = doc.test();
|
let results = doc.test();
|
||||||
|
Loading…
Reference in New Issue
Block a user