ocpi-tariffs 0.43.0

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

use assert_matches::assert_matches;

use crate::{
    json::{self, FromJson},
    warning::test::VerdictTestExt,
    Verdict,
};

use super::{Code, CodeSet, Warning};

#[test]
fn alpha2_country_code_matches() {
    let code = Code::from_alpha_2(*b"NL").unwrap();

    assert_eq!(Code::Nl, code);
    assert_eq!("NL", code.into_alpha_2_str());
}

#[test]
fn alpha3_country_code_matches() {
    let code = Code::from_alpha_3(*b"NLD").unwrap();

    assert_eq!(Code::Nl, code);
}

#[test]
fn should_create_country3_without_issue() {
    const JSON: &str = r#"{ "country": "NLD" }"#;

    let (code_set, warnings) = code_set_from_json(JSON).unwrap().into_parts();

    let code = assert_matches!(code_set, CodeSet::Alpha3(code) => code);
    assert_eq!(code, Code::Nl);
    assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
}

#[test]
fn should_create_country2_without_issue() {
    const JSON: &str = r#"{ "country": "NL" }"#;

    let (code_set, warnings) = code_set_from_json(JSON).unwrap().into_parts();

    let code = assert_matches!(code_set, CodeSet::Alpha2(code) => code);
    assert_eq!(code, Code::Nl);
    assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
}

#[test]
fn should_raise_country_content_issue() {
    {
        const JSON: &str = r#"{ "country": "VV" }"#;

        let error = code_set_from_json(JSON).unwrap_only_error();

        assert_matches!(error.into_warning(), Warning::InvalidCode);
    }

    {
        const JSON: &str = r#"{ "country": "VVV" }"#;

        let error = code_set_from_json(JSON).unwrap_only_error();

        assert_matches!(error.into_warning(), Warning::InvalidCode);
    }
}

#[test]
fn should_parse_invalid_case() {
    {
        const JSON: &str = r#"{ "country": "nl" }"#;

        let (code_set, warnings) = code_set_from_json(JSON).unwrap().into_parts();
        let warnings = warnings.path_map();
        let warnings = &*warnings["$.country"];

        let code = assert_matches!(code_set, CodeSet::Alpha2(code) => code);
        assert_eq!(code, Code::Nl);
        assert_matches!(*warnings, [Warning::PreferUpperCase]);
    }

    {
        const JSON: &str = r#"{ "country": "nld" }"#;

        let (code_set, warnings) = code_set_from_json(JSON).unwrap().into_parts();
        let warnings = warnings.path_map();
        let warnings = &*warnings["$.country"];

        let code = assert_matches!(code_set, CodeSet::Alpha3(code) => code);
        assert_eq!(code, Code::Nl);
        assert_matches!(warnings, [Warning::PreferUpperCase]);
    }
}

#[test]
fn should_raise_country_length_issue() {
    const JSON: &str = r#"{ "country": "IRELAND" }"#;

    let error = code_set_from_json(JSON).unwrap_only_error();

    assert_matches!(error.into_warning(), Warning::InvalidLength);
}

#[track_caller]
fn code_set_from_json(json: &str) -> Verdict<CodeSet, Warning> {
    let json = json::parse(json).unwrap();
    let country_elem = json.find_field("country").unwrap();
    CodeSet::from_json(country_elem.element())
}