ef_tests/
case.rs

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