expectations_matcher/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Provides test expectations matching based on json5 expectations files.
6
7/// Calculates the expected outcome for a test named `name` for `expectation`
8/// given the current set of `cases_to_run`.
9fn expected_single_outcome(
10    name: &str,
11    expectation: &ser::Expectation,
12    cases_to_run: &ser::CasesToRun,
13) -> Option<Outcome> {
14    let (ser::Matchers { matchers }, outcome) = match expectation {
15        ser::Expectation::Skip(a) => (a, Outcome::Skip),
16        ser::Expectation::ExpectFailure(a) => match cases_to_run {
17            ser::CasesToRun::WithErrLogs => (a, Outcome::Skip),
18            _ => (a, Outcome::Fail),
19        },
20        ser::Expectation::ExpectPass(a) => match cases_to_run {
21            ser::CasesToRun::WithErrLogs => (a, Outcome::Skip),
22            _ => (a, Outcome::Pass),
23        },
24        ser::Expectation::ExpectFailureWithErrLogs(a) => match cases_to_run {
25            ser::CasesToRun::NoErrLogs => (a, Outcome::Skip),
26            _ => (a, Outcome::Fail),
27        },
28        ser::Expectation::ExpectPassWithErrLogs(a) => match cases_to_run {
29            ser::CasesToRun::NoErrLogs => (a, Outcome::Skip),
30            _ => (a, Outcome::Pass),
31        },
32    };
33    matchers.iter().any(|matcher| matcher.matches(name)).then_some(outcome)
34}
35
36/// Calculates the expected outcome for a test named `name` for the given set of
37/// `expectations`.
38pub fn expected_outcome(name: &str, expectations: &ser::Expectations) -> Option<Outcome> {
39    let ser::Expectations { expectations, cases_to_run } = expectations;
40    expectations
41        .iter()
42        .rev()
43        .find_map(|expectation| expected_single_outcome(name, expectation, cases_to_run))
44}
45
46/// The outcome of an expectation matching operation.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum Outcome {
49    Pass,
50    Fail,
51    Skip,
52}