1use std::sync::Arc;
4
5use miette::Diagnostic;
6use miette::NamedSource;
7use thiserror::Error;
8
9use super::TestVerbCreator;
10use crate::error::TestErrorCase;
11
12pub 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")]
26pub 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 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}