ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
//! Tests for `parse_time`: the time formats accepted by the library.

#![allow(clippy::unwrap_used, reason = "test code with known-valid times")]

use chrono::NaiveTime;

use super::{TimeError, parse_time};

#[test]
fn an_hour_or_minute_without_its_leading_zero_is_read() {
    let expected = Ok(NaiveTime::from_hms_opt(9, 5, 0).unwrap());

    for text in ["9:5", "9:05", "09:5"] {
        assert_eq!(parse_time(text), expected, "for `{text}`");
    }
}

/// `parse_time` reads a space after the time, which chrono's `%H:%M` rejects.
#[test]
fn a_time_with_ascii_spaces_around_it_is_read() {
    let expected = Ok(NaiveTime::from_hms_opt(9, 5, 0).unwrap());

    for text in [" 09:05", "09:05 ", "  9:5  "] {
        assert_eq!(parse_time(text), expected, "for `{text}`");
    }
}

/// chrono's `%H:%M` reads `09: 05`, skipping the space before the minute, and `parse_time` rejects
/// it. U+00A0 is the no-break space.
#[test]
fn a_time_with_other_whitespace_is_not_read() {
    for text in ["09: 05", "09 :05", "\u{a0}09:05", "\t09:05"] {
        assert_eq!(parse_time(text), Err(TimeError::Syntax), "for `{text}`");
    }
}

#[test]
fn a_time_in_any_other_form_is_not_read() {
    let texts = [
        "+9:05", "-9:05", "009:05", "09:005", "09:05:00", "0905", "9h05", ":05", "09:", "",
    ];

    for text in texts {
        assert_eq!(parse_time(text), Err(TimeError::Syntax), "for `{text}`");
    }
}

/// OCPI writes the end of the day as `00:00`, never as `24:00`.
#[test]
fn a_time_a_day_does_not_have_is_not_read() {
    for text in ["24:00", "09:60", "99:99"] {
        assert_eq!(
            parse_time(text),
            Err(TimeError::NotATimeOfDay),
            "for `{text}`"
        );
    }
}