Rename TestDecode to YamlDecode

This commit is contained in:
Paul Hauner 2019-05-15 11:12:49 +10:00
parent 9f42d4d764
commit b7a8613444
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
8 changed files with 27 additions and 24 deletions

View File

@ -3,7 +3,6 @@ use ethereum_types::{U128, U256};
use serde_derive::Deserialize;
use ssz::Decode;
use std::fmt::Debug;
use test_decode::TestDecode;
pub use crate::error::*;
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_cases::*;
pub use crate::test_doc_header::*;
pub use crate::yaml_utils::*;
pub use yaml_decode::YamlDecode;
mod error;
mod eth_specs;
mod test_case_result;
mod test_decode;
mod test_doc;
mod test_doc_cases;
mod test_doc_header;
mod yaml_utils;
mod yaml_decode;

View File

@ -1,4 +1,5 @@
use super::*;
use crate::yaml_decode::*;
use std::{fs::File, io::prelude::*, path::PathBuf};
use types::{EthSpec, FoundationEthSpec};
@ -53,12 +54,12 @@ impl TestDoc {
pub fn run_test<T, E: EthSpec>(test_doc_yaml: &String) -> Vec<TestCaseResult>
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: TestDocCases<T> =
TestDocCases::test_decode(&test_cases_yaml.to_string()).unwrap();
TestDocCases::yaml_decode(&test_cases_yaml.to_string()).unwrap();
test_cases.test::<E>()
}

View File

@ -1,4 +1,5 @@
use super::*;
use crate::yaml_decode::*;
use yaml_rust::YamlLoader;
mod ssz_generic;
@ -12,9 +13,9 @@ pub struct TestDocCases<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
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 mut test_cases: Vec<T> = vec![];
@ -24,7 +25,7 @@ impl<T: TestDecode> TestDecode for TestDocCases<T> {
if doc[i].is_badvalue() {
break;
} 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;

View File

@ -10,8 +10,8 @@ pub struct SszGeneric {
pub ssz: Option<String>,
}
impl TestDecode for SszGeneric {
fn test_decode(yaml: &String) -> Result<Self, Error> {
impl YamlDecode for SszGeneric {
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
}
}
@ -55,7 +55,7 @@ fn ssz_generic_test<T>(
value: &Option<String>,
) -> Result<(), Error>
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)))?;
@ -65,7 +65,7 @@ where
}
let expected = if let Some(string) = value {
Some(T::test_decode(string)?)
Some(T::yaml_decode(string)?)
} else {
None
};

View File

@ -21,8 +21,8 @@ pub struct Value<T> {
value: T,
}
impl TestDecode for SszStatic {
fn test_decode(yaml: &String) -> Result<Self, Error> {
impl YamlDecode for SszStatic {
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
let mut ssz_static: SszStatic = serde_yaml::from_str(&yaml.as_str()).unwrap();
ssz_static.raw_yaml = yaml.clone();

View File

@ -1,16 +1,20 @@
use super::*;
use types::Fork;
pub trait TestDecode: Sized {
mod utils;
pub use utils::*;
pub trait YamlDecode: Sized {
/// 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`.
macro_rules! impl_via_parse {
($ty: ty) => {
impl TestDecode for $ty {
fn test_decode(string: &String) -> Result<Self, Error> {
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
string
.parse::<Self>()
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
@ -28,8 +32,8 @@ impl_via_parse!(u64);
/// hex, so we use `from_dec_str` instead.
macro_rules! impl_via_from_dec_str {
($ty: ty) => {
impl TestDecode for $ty {
fn test_decode(string: &String) -> Result<Self, Error> {
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
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`.
macro_rules! impl_via_serde_yaml {
($ty: ty) => {
impl TestDecode for $ty {
fn test_decode(string: &String) -> Result<Self, Error> {
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
serde_yaml::from_str(string)
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}

View File

@ -47,7 +47,6 @@ mod ssz_static {
}
#[test]
#[ignore]
fn minimal_nil() {
TestDoc::assert_tests_pass(ssz_generic_file("core/ssz_minimal_nil.yaml"));
}