ef_tests/
case.rs

1//! Test case definitions
2
3use crate::result::{CaseResult, Error};
4use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
5use std::{
6    fmt::Debug,
7    path::{Path, PathBuf},
8};
9
10/// A single test case, capable of loading a JSON description of itself and running it.
11///
12/// See <https://ethereum-tests.readthedocs.io/> for test specs.
13pub trait Case: Debug + Sync + Sized {
14    /// A description of the test.
15    fn description(&self) -> String {
16        "no description".to_string()
17    }
18
19    /// Load the test from the given file path.
20    ///
21    /// The file can be assumed to be a valid EF test case as described on <https://ethereum-tests.readthedocs.io/>.
22    fn load(path: &Path) -> Result<Self, Error>;
23
24    /// Run the test.
25    fn run(&self) -> Result<(), Error>;
26}
27
28/// A container for multiple test cases.
29#[derive(Debug)]
30pub struct Cases<T> {
31    /// The contained test cases and the path to each test.
32    pub test_cases: Vec<(PathBuf, T)>,
33}
34
35impl<T: Case> Cases<T> {
36    /// Run the contained test cases.
37    pub fn run(&self) -> Vec<CaseResult> {
38        self.test_cases
39            .par_iter()
40            .map(|(path, case)| CaseResult::new(path, case, case.run()))
41            .collect()
42    }
43}