litcheck_filecheck/test/
mod.rs

1mod result;
2#[cfg(test)]
3pub(crate) mod testing;
4
5pub(crate) use self::result::TestInputType;
6pub use self::result::TestResult;
7#[cfg(test)]
8pub use self::testing::TestContext;
9
10use crate::{check::Checker, common::*, parse};
11
12/// This struct represents a single FileCheck test which
13/// can be run against one or more input files for verification.
14///
15/// This is the primary entrypoint for running FileCheck tests.
16pub struct Test<'a> {
17    config: &'a Config,
18    strings: StringInterner,
19    match_file: ArcSource,
20}
21impl<'a> Test<'a> {
22    /// Create a new test from the given match file (containing CHECKs) and configuration
23    ///
24    /// This function does not compile the actual test file until verification is requested.
25    pub fn new<S>(match_file: S, config: &'a Config) -> Self
26    where
27        ArcSource: From<S>,
28    {
29        Self {
30            config,
31            match_file: ArcSource::from(match_file),
32            strings: StringInterner::new(),
33        }
34    }
35
36    /// Verify the given input file passes this test.
37    ///
38    /// First, it parses the check file for rules, post-processes them, and then
39    /// applies the parsed rules to the input file, to determine if the file matches
40    /// or fails to match.
41    pub fn verify<'input, S>(&mut self, input_file: S) -> DiagResult<Vec<MatchInfo<'static>>>
42    where
43        ArcSource: From<S> + 'input,
44    {
45        // Parse the check file
46        let mut parser = parse::CheckFileParser::new(&self.config, &mut self.strings);
47
48        let match_file = parser
49            .parse(&self.match_file)
50            .map_err(|err| Report::new(err).with_source_code(self.match_file.clone()))?;
51
52        // Compile the check rules, and raise an error if any are invalid
53        let program = match_file.compile(self.config, &mut self.strings)?;
54
55        // Apply the checks
56        let mut checker = Checker::new(
57            self.config,
58            &mut self.strings,
59            program,
60            self.match_file.clone(),
61        );
62        checker
63            .check_input(ArcSource::from(input_file))
64            .into_result()
65            .map_err(Report::new)
66    }
67}