test_dsl/
test_case.rs

1//! Individual testcases
2
3use std::sync::Arc;
4
5use miette::Diagnostic;
6use miette::NamedSource;
7use thiserror::Error;
8
9use super::TestVerbCreator;
10use crate::error::TestErrorCase;
11
12/// A singular test case
13pub struct TestCase<H> {
14    pub(crate) creators: Vec<Box<dyn TestVerbCreator<H>>>,
15    pub(crate) source_code: NamedSource<Arc<str>>,
16}
17
18impl<H> std::fmt::Debug for TestCase<H> {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        f.debug_struct("TestCase").finish_non_exhaustive()
21    }
22}
23
24#[derive(Error, Diagnostic, Debug)]
25#[error("Testcase did not run successfully")]
26/// An error occured while running a test
27pub struct TestCaseError {
28    #[diagnostic_source]
29    pub(crate) error: TestErrorCase,
30
31    #[source_code]
32    pub(crate) source_code: miette::NamedSource<Arc<str>>,
33}
34
35impl<H: 'static> TestCase<H> {
36    pub(crate) fn new(source_code: miette::NamedSource<Arc<str>>) -> Self {
37        TestCase {
38            creators: vec![],
39            source_code,
40        }
41    }
42
43    /// Run the given test and report on its success
44    pub fn run(&self, harness: &mut H) -> Result<(), TestCaseError> {
45        self.creators
46            .iter()
47            .flat_map(|c| {
48                c.get_test_verbs()
49                    .map(|verb| verb.run(harness))
50                    .collect::<Vec<_>>()
51            })
52            .collect::<Result<(), _>>()
53            .map_err(|error| TestCaseError {
54                error,
55                source_code: self.source_code.clone(),
56            })
57    }
58}