ocpi-tariffs 0.46.1

OCPI tariff calculations
Documentation
#![allow(
    clippy::indexing_slicing,
    reason = "unwraps are allowed anywhere in tests"
)]

use assert_matches::assert_matches;

use crate::{
    json::{self, FromJson},
    warning,
};

use super::{CiExactLen, CiMaxLen, Warning};

const LEN: usize = 3;

#[test]
fn should_parse_max_len() {
    let input = "hel";
    let (output, warnings) = max_len(input);
    assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
    assert_eq!(output, input);
}

#[test]
fn should_fail_on_max_len() {
    let input = "hello";
    let (output, warnings) = max_len(input);
    let warnings = warnings.into_path_as_str_map();
    let warnings = &warnings["$"];
    let length = assert_matches!(
        warnings.as_slice(),
        [Warning::InvalidLengthMax { length }] => *length
    );
    assert_eq!(length, LEN);
    assert_eq!(output, input);
}

#[test]
fn should_parse_exact_len() {
    let input = "hel";
    let (output, warnings) = expect_len(input);
    assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
    assert_eq!(output, input);
}

#[test]
fn should_fail_on_exact_len() {
    let input = "hello";
    let (output, warnings) = expect_len(input);
    let warnings = warnings.into_path_as_str_map();
    let warnings = &warnings["$"];
    let length = assert_matches!(
        warnings.as_slice(),
        [Warning::InvalidLengthExact { length }] => *length
    );
    assert_eq!(length, LEN);
    assert_eq!(output, input);
}

#[track_caller]
fn max_len(s: &str) -> (String, warning::Set<Warning>) {
    let quoted_input = format!(r#""{s}""#);
    let elem = json::parse((&*quoted_input).into()).unwrap();
    let output = CiMaxLen::<'_, LEN>::from_json(&elem).unwrap();
    let (output, warnings) = output.into_parts();
    (output.to_string(), warnings)
}

#[track_caller]
fn expect_len(s: &str) -> (String, warning::Set<Warning>) {
    let quoted_input = format!(r#""{s}""#);
    let elem = json::parse((&*quoted_input).into()).unwrap();
    let output = CiExactLen::<'_, LEN>::from_json(&elem).unwrap();
    let (output, warnings) = output.into_parts();
    (output.to_string(), warnings)
}