ocpi-tariffs 0.43.0

OCPI tariff calculations
Documentation
#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed panic")]

use std::{collections::BTreeMap, path::Path};

use assert_matches::assert_matches;

use crate::{
    guess, tariff,
    test::{self, ExpectFile, Expectation},
    warning, Version, Versioned,
};

use super::Report;

pub fn lint_tariff(tariff_json: &str, path: &Path, expected_version: Version) {
    const LINT_FEATURE: &str = "lint";
    const PARSE_FEATURE: &str = "parse";

    test::setup();
    let expect_json = test::read_expect_json(path, PARSE_FEATURE);
    let parse_expect = test::parse_expect_json(expect_json.as_deref());
    let report = tariff::parse_and_report(tariff_json).unwrap();

    let tariff = {
        let version = tariff::test::assert_parse_guess_report(report, parse_expect);
        let tariff = assert_matches!(version, guess::Version::Certain(tariff) => tariff);
        assert_eq!(tariff.version(), expected_version);
        tariff
    };

    let expect_json = test::read_expect_json(path, LINT_FEATURE);
    let lint_expect = test::parse_expect_json(expect_json.as_deref());

    let report = super::lint(&tariff);

    assert_report(report, lint_expect);
}

/// Expectations for the result of calling `tariff::lint`.
#[derive(serde::Deserialize)]
pub struct Expect {
    #[serde(default)]
    warnings: Expectation<BTreeMap<String, Vec<String>>>,
}

#[track_caller]
pub(crate) fn assert_report(report: Report, expect: ExpectFile<Expect>) {
    let Report { warnings } = report;

    // If there are warnings reported and there is no `expect` file
    // then panic printing the fields of the expect JSON object that would silence these warnings.
    // These can be copied into an `output_lint__*.json` file.
    let ExpectFile {
        value: expect,
        expect_file_name,
    } = expect;

    let Some(expect) = expect else {
        assert!(
            warnings.is_empty(),
            "There is no expectation file at `{expect_file_name}` but the tariff has warnings;\n{:#?}",
            warnings.path_id_map()
        );
        return;
    };
    warning::test::assert_warnings(&expect_file_name, &warnings, expect.warnings);
}