ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
//! Tests for lowering a `schema::Str` into the `mod string` types via `FromSchema`.
//!
//! A `schema::Str` is only produced by the builder, so each test drives a real `v2.1.1`
//! tariff through `build_tariff` and lowers its `id` field (a `Str` leaf). The leaf does
//! not carry the domain length bound, so each test lowers it at whatever const length it
//! wants to exercise.

#![allow(
    clippy::indexing_slicing,
    reason = "unwraps and indexing are allowed anywhere in tests"
)]

use std::assert_matches;

use super::{AsciiExactLen, AsciiPrintable, Warning};
use crate::{
    FromSchema as _, json,
    schema::{self, Integrity, v211},
    warning,
};

/// A minimal, fully valid `v2.1.1` tariff. `id` is the `Str` leaf the tests lower.
const VALID: &str = r#"{
    "currency": "EUR",
    "elements": [
        {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
    ],
    "id": "ID",
    "last_updated": "2024-01-01T00:00:00Z"
}"#;

/// Build a `v2.1.1` tariff, dropping the schema-level warnings (the tests assert on the
/// warnings produced by lowering the `id` leaf, not by the schema walk).
fn build_tariff<'buf>(doc: &json::Document<'buf>) -> v211::Tariff<'buf> {
    v211::build_tariff(doc).ignore_warnings()
}

/// Borrow the built `id` `Str` leaf out of a tariff.
fn id<'a, 'buf>(tariff: &'a v211::Tariff<'buf>) -> &'a schema::Str<'buf> {
    let Integrity::Ok(id) = &tariff.id else {
        panic!("the id should be built: {:?}", tariff.id);
    };
    id
}

/// Collect every warning across all paths into a flat list.
fn all_warnings(warnings: &warning::Set<Warning>) -> Vec<&Warning> {
    warnings.path_map().into_values().flatten().collect()
}

#[test]
fn printable_string_lowers_cleanly() {
    let doc = json::parse(VALID.into()).unwrap();
    let tariff = build_tariff(&doc);

    let (value, warnings) = AsciiPrintable::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_eq!(&*value, "ID");
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}

#[test]
fn exact_len_match_lowers_cleanly() {
    let src = VALID.replace(r#""id": "ID""#, r#""id": "abc""#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (value, warnings) = AsciiExactLen::<3>::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_eq!(&*value, "abc");
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}

#[test]
fn exact_len_mismatch_warns() {
    let src = VALID.replace(r#""id": "ID""#, r#""id": "hello""#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (value, warnings) = AsciiExactLen::<3>::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_eq!(&*value, "hello");
    assert_matches!(
        all_warnings(&warnings)[..],
        [Warning::InvalidLengthExact { length: 3 }]
    );
}

#[test]
fn escape_sequence_warns() {
    // A `\t` escape: a `CiString` should not need escapes, so the presence of one is
    // flagged. Only the escape is reported here; the decoded value is printable.
    let src = VALID.replace(r#""id": "ID""#, r#""id": "a\tb""#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (_value, warnings) = AsciiPrintable::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_matches!(all_warnings(&warnings)[..], [Warning::ContainsEscapeCodes]);
}

#[test]
fn literal_control_character_warns() {
    // A literal TAB byte (not an escape) inside the string is non-printable ASCII.
    let src = VALID.replace(r#""id": "ID""#, "\"id\": \"a\tb\"");
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (_value, warnings) = AsciiPrintable::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_matches!(
        all_warnings(&warnings)[..],
        [Warning::ContainsNonPrintableASCII]
    );
}

#[test]
fn escape_decoding_to_non_printable_warns_both() {
    // `\u0020` is an escape (flagged) that decodes to a space, which is non-printable
    // ASCII (also flagged). This exercises the on-the-fly decode in the printability
    // scan: the raw bytes look printable, only the decoded char is not.
    let src = VALID.replace(r#""id": "ID""#, r#""id": "a\u0020b""#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (_value, warnings) = AsciiPrintable::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_matches!(
        all_warnings(&warnings)[..],
        [
            Warning::ContainsEscapeCodes,
            Warning::ContainsNonPrintableASCII
        ]
    );
}

/// An escaped value is the characters it decodes to, so a correct value must not be
/// reported. Measuring the raw text counted `\u0054DR` as eight and called `TDR` the wrong length.
#[test]
fn exact_len_counts_decoded_characters() {
    let escaped = r#""id": "\u0054DR""#;
    let src = VALID.replace(r#""id": "ID""#, escaped);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (value, warnings) = AsciiExactLen::<3>::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    // The value is still held as written; only the length is measured on the decoded form.
    assert_eq!(&*value, r"\u0054DR");
    assert_matches!(all_warnings(&warnings)[..], [Warning::ContainsEscapeCodes]);
}

/// Escaped and the wrong length is a real combination, so escaping must not exempt a
/// value from the length rule.
#[test]
fn exact_len_still_warns_on_a_short_escaped_value() {
    let escaped = r#""id": "\u0054D""#;
    let src = VALID.replace(r#""id": "ID""#, escaped);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (_value, warnings) = AsciiExactLen::<3>::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_matches!(
        all_warnings(&warnings)[..],
        [
            Warning::ContainsEscapeCodes,
            Warning::InvalidLengthExact { length: 3 }
        ]
    );
}

/// An escaped value one character over the bound must report too, since escaping can only
/// ever lengthen the raw text and the old check could not tell the two apart.
#[test]
fn exact_len_warns_on_a_long_escaped_value() {
    let escaped = r#""id": "\u0054DRX""#;
    let src = VALID.replace(r#""id": "ID""#, escaped);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (_value, warnings) = AsciiExactLen::<3>::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_matches!(
        all_warnings(&warnings)[..],
        [
            Warning::ContainsEscapeCodes,
            Warning::InvalidLengthExact { length: 3 }
        ]
    );
}

/// The maximum is the schema's rule, so lowering raises nothing of its own and an over-long
/// value is reported once, by the walk.
#[test]
fn max_len_defers_to_the_schema() {
    let src = VALID.replace(r#""id": "ID""#, r#""id": "hello""#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (value, warnings) = AsciiPrintable::from_schema(id(&tariff))
        .unwrap()
        .into_parts();

    assert_eq!(&*value, "hello");
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}