Rename TestDecode
to YamlDecode
This commit is contained in:
parent
9f42d4d764
commit
b7a8613444
@ -3,7 +3,6 @@ use ethereum_types::{U128, U256};
|
|||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use ssz::Decode;
|
use ssz::Decode;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use test_decode::TestDecode;
|
|
||||||
|
|
||||||
pub use crate::error::*;
|
pub use crate::error::*;
|
||||||
pub use crate::eth_specs::*;
|
pub use crate::eth_specs::*;
|
||||||
@ -11,13 +10,12 @@ pub use crate::test_case_result::*;
|
|||||||
pub use crate::test_doc::*;
|
pub use crate::test_doc::*;
|
||||||
pub use crate::test_doc_cases::*;
|
pub use crate::test_doc_cases::*;
|
||||||
pub use crate::test_doc_header::*;
|
pub use crate::test_doc_header::*;
|
||||||
pub use crate::yaml_utils::*;
|
pub use yaml_decode::YamlDecode;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod eth_specs;
|
mod eth_specs;
|
||||||
mod test_case_result;
|
mod test_case_result;
|
||||||
mod test_decode;
|
|
||||||
mod test_doc;
|
mod test_doc;
|
||||||
mod test_doc_cases;
|
mod test_doc_cases;
|
||||||
mod test_doc_header;
|
mod test_doc_header;
|
||||||
mod yaml_utils;
|
mod yaml_decode;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::yaml_decode::*;
|
||||||
use std::{fs::File, io::prelude::*, path::PathBuf};
|
use std::{fs::File, io::prelude::*, path::PathBuf};
|
||||||
use types::{EthSpec, FoundationEthSpec};
|
use types::{EthSpec, FoundationEthSpec};
|
||||||
|
|
||||||
@ -53,12 +54,12 @@ impl TestDoc {
|
|||||||
|
|
||||||
pub fn run_test<T, E: EthSpec>(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 + YamlDecode,
|
||||||
{
|
{
|
||||||
let test_cases_yaml = extract_yaml_by_key(test_doc_yaml, "test_cases");
|
let test_cases_yaml = extract_yaml_by_key(test_doc_yaml, "test_cases");
|
||||||
|
|
||||||
let test_cases: TestDocCases<T> =
|
let test_cases: TestDocCases<T> =
|
||||||
TestDocCases::test_decode(&test_cases_yaml.to_string()).unwrap();
|
TestDocCases::yaml_decode(&test_cases_yaml.to_string()).unwrap();
|
||||||
|
|
||||||
test_cases.test::<E>()
|
test_cases.test::<E>()
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::yaml_decode::*;
|
||||||
use yaml_rust::YamlLoader;
|
use yaml_rust::YamlLoader;
|
||||||
|
|
||||||
mod ssz_generic;
|
mod ssz_generic;
|
||||||
@ -12,9 +13,9 @@ pub struct TestDocCases<T> {
|
|||||||
pub test_cases: Vec<T>,
|
pub test_cases: Vec<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: TestDecode> TestDecode for TestDocCases<T> {
|
impl<T: YamlDecode> YamlDecode for TestDocCases<T> {
|
||||||
/// Decodes a YAML list of test cases
|
/// Decodes a YAML list of test cases
|
||||||
fn test_decode(yaml: &String) -> Result<Self, Error> {
|
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
|
||||||
let doc = &YamlLoader::load_from_str(yaml).unwrap()[0];
|
let doc = &YamlLoader::load_from_str(yaml).unwrap()[0];
|
||||||
|
|
||||||
let mut test_cases: Vec<T> = vec![];
|
let mut test_cases: Vec<T> = vec![];
|
||||||
@ -24,7 +25,7 @@ impl<T: TestDecode> TestDecode for TestDocCases<T> {
|
|||||||
if doc[i].is_badvalue() {
|
if doc[i].is_badvalue() {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
test_cases.push(T::test_decode(&yaml_to_string(&doc[i])).unwrap())
|
test_cases.push(T::yaml_decode(&yaml_to_string(&doc[i])).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
|
@ -10,8 +10,8 @@ pub struct SszGeneric {
|
|||||||
pub ssz: Option<String>,
|
pub ssz: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestDecode for SszGeneric {
|
impl YamlDecode for SszGeneric {
|
||||||
fn test_decode(yaml: &String) -> Result<Self, Error> {
|
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
|
||||||
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
|
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ fn ssz_generic_test<T>(
|
|||||||
value: &Option<String>,
|
value: &Option<String>,
|
||||||
) -> Result<(), Error>
|
) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Decode + TestDecode + Debug + PartialEq<T>,
|
T: Decode + YamlDecode + Debug + PartialEq<T>,
|
||||||
{
|
{
|
||||||
let ssz = hex::decode(&ssz[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
let ssz = hex::decode(&ssz[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
let expected = if let Some(string) = value {
|
let expected = if let Some(string) = value {
|
||||||
Some(T::test_decode(string)?)
|
Some(T::yaml_decode(string)?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -21,8 +21,8 @@ pub struct Value<T> {
|
|||||||
value: T,
|
value: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestDecode for SszStatic {
|
impl YamlDecode for SszStatic {
|
||||||
fn test_decode(yaml: &String) -> Result<Self, Error> {
|
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
|
||||||
let mut ssz_static: SszStatic = serde_yaml::from_str(&yaml.as_str()).unwrap();
|
let mut ssz_static: SszStatic = serde_yaml::from_str(&yaml.as_str()).unwrap();
|
||||||
|
|
||||||
ssz_static.raw_yaml = yaml.clone();
|
ssz_static.raw_yaml = yaml.clone();
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use types::Fork;
|
use types::Fork;
|
||||||
|
|
||||||
pub trait TestDecode: Sized {
|
mod utils;
|
||||||
|
|
||||||
|
pub use utils::*;
|
||||||
|
|
||||||
|
pub trait YamlDecode: Sized {
|
||||||
/// Decode an object from the test specification YAML.
|
/// Decode an object from the test specification YAML.
|
||||||
fn test_decode(string: &String) -> Result<Self, Error>;
|
fn yaml_decode(string: &String) -> Result<Self, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Basic types can general be decoded with the `parse` fn if they implement `str::FromStr`.
|
/// 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 YamlDecode for $ty {
|
||||||
fn test_decode(string: &String) -> Result<Self, Error> {
|
fn yaml_decode(string: &String) -> Result<Self, Error> {
|
||||||
string
|
string
|
||||||
.parse::<Self>()
|
.parse::<Self>()
|
||||||
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
||||||
@ -28,8 +32,8 @@ impl_via_parse!(u64);
|
|||||||
/// hex, so we use `from_dec_str` instead.
|
/// 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 YamlDecode for $ty {
|
||||||
fn test_decode(string: &String) -> Result<Self, Error> {
|
fn yaml_decode(string: &String) -> Result<Self, Error> {
|
||||||
Self::from_dec_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
Self::from_dec_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,8 +46,8 @@ impl_via_from_dec_str!(U256);
|
|||||||
/// Types that already implement `serde::Deserialize` can be decoded using `serde_yaml`.
|
/// Types that already implement `serde::Deserialize` can be decoded using `serde_yaml`.
|
||||||
macro_rules! impl_via_serde_yaml {
|
macro_rules! impl_via_serde_yaml {
|
||||||
($ty: ty) => {
|
($ty: ty) => {
|
||||||
impl TestDecode for $ty {
|
impl YamlDecode for $ty {
|
||||||
fn test_decode(string: &String) -> Result<Self, Error> {
|
fn yaml_decode(string: &String) -> Result<Self, Error> {
|
||||||
serde_yaml::from_str(string)
|
serde_yaml::from_str(string)
|
||||||
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
|
||||||
}
|
}
|
@ -47,7 +47,6 @@ mod ssz_static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn minimal_nil() {
|
fn minimal_nil() {
|
||||||
TestDoc::assert_tests_pass(ssz_generic_file("core/ssz_minimal_nil.yaml"));
|
TestDoc::assert_tests_pass(ssz_generic_file("core/ssz_minimal_nil.yaml"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user