ocpi-tariffs 0.43.0

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 as _},
    test,
    warning::test::VerdictTestExt,
};

use super::{Warning, Weekday};

#[test]
fn should_create_from_json() {
    const JSON: &str = r#""MONDAY""#;

    test::setup();

    let elem = json::parse(JSON).unwrap();
    let day = Weekday::from_json(&elem).unwrap().unwrap();
    assert_matches!(day, Weekday::Monday);
}

#[test]
fn should_fail_on_type_from_json() {
    const JSON: &str = "[]";

    test::setup();

    let elem = json::parse(JSON).unwrap();
    let error = Weekday::from_json(&elem).unwrap_only_error();
    assert_matches!(error.into_warning(), Warning::InvalidType { .. });
}

#[test]
fn should_fail_on_value_from_json() {
    const JSON: &str = r#""MOONDAY""#;

    test::setup();

    let elem = json::parse(JSON).unwrap();
    let error = Weekday::from_json(&elem).unwrap_only_error();
    assert_matches!(error.into_warning(), Warning::InvalidDay);
}

#[test]
fn should_warn_about_case_from_json() {
    const JSON: &str = r#""sunday""#;

    test::setup();

    let elem = json::parse(JSON).unwrap();
    let (day, warnings) = Weekday::from_json(&elem).unwrap().into_parts();
    let warnings = warnings.into_path_as_str_map();
    let warnings = &*warnings["$"];

    assert_matches!(day, Weekday::Sunday);
    assert_matches!(warnings, [Warning::PreferUpperCase]);
}