litcheck_lit/
format.rs

1use std::fmt;
2
3use litcheck::diagnostics::DiagResult;
4use serde::Deserialize;
5
6use crate::{
7    test::{DefaultTestRegistry, Test, TestRegistry, TestResult},
8    Config,
9};
10
11pub trait TestFormat: fmt::Debug {
12    /// Returns the proper name of this test format
13    fn name(&self) -> &'static str;
14
15    /// Get the test registry for this format
16    fn registry(&self) -> &dyn TestRegistry;
17
18    /// Executes `test` using this test format, and the provided `config`.
19    fn execute(&self, test: &Test, config: &Config) -> DiagResult<TestResult>;
20}
21
22#[derive(Default, Clone, Deserialize)]
23pub enum SelectedTestFormat {
24    #[default]
25    Default,
26    /// Parses each test file for a `RUN:` directive, which is expected to contain
27    /// a shell command to run, the exit status of which will determine the result
28    /// of that particular test.
29    #[serde(rename = "shtest", alias = "sh")]
30    ShTest(crate::formats::ShTest),
31    /// Runs a test file as an executable with no inputs, and expects the test to
32    /// exit with a zero status if successful, or non-zero if failed.
33    #[serde(rename = "executable", alias = "exe")]
34    Executable(crate::formats::ExecutableTest),
35}
36impl fmt::Debug for SelectedTestFormat {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        match self {
39            Self::Default => f.write_str("Default"),
40            Self::ShTest(ref format) => write!(f, "{format:#?}"),
41            Self::Executable(ref format) => write!(f, "{format:#?}"),
42        }
43    }
44}
45impl TestFormat for SelectedTestFormat {
46    fn name(&self) -> &'static str {
47        match self {
48            Self::Default => "shtest",
49            Self::ShTest(ref format) => format.name(),
50            Self::Executable(ref format) => format.name(),
51        }
52    }
53
54    fn registry(&self) -> &dyn TestRegistry {
55        match self {
56            Self::Default => &DefaultTestRegistry,
57            Self::ShTest(ref format) => format.registry(),
58            Self::Executable(ref format) => format.registry(),
59        }
60    }
61
62    fn execute(&self, test: &Test, config: &Config) -> DiagResult<TestResult> {
63        match self {
64            Self::Default => {
65                let format = crate::formats::ShTest::default();
66                format.execute(test, config)
67            }
68            Self::ShTest(ref format) => format.execute(test, config),
69            Self::Executable(ref format) => format.execute(test, config),
70        }
71    }
72}