ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
use super::{edit_distance, nearest_spellings, spelling_key};

fn chars(name: &str) -> Vec<char> {
    name.chars().collect()
}

#[test]
fn edit_distance_counts_each_kind_of_edit_once() {
    assert_eq!(edit_distance(&chars("maxkwh"), &chars("maxkwh")), 0);
    assert_eq!(edit_distance(&chars("maxkwh"), &chars("maxkwhh")), 1);
    assert_eq!(edit_distance(&chars("maxkwh"), &chars("maxkh")), 1);
    assert_eq!(edit_distance(&chars("maxkwh"), &chars("maxkwg")), 1);
    assert_eq!(edit_distance(&chars("maxkwh"), &chars("maxwkh")), 1);
}

/// `spelling_key` removes case and `_` before `edit_distance` counts any edit, so a camel-case name
/// is 0 edits from the snake-case name the spec writes.
#[test]
fn camel_case_matches_snake_case() {
    assert_eq!(
        edit_distance(&spelling_key("dayOfWeek"), &spelling_key("day_of_week")),
        0
    );
    assert_eq!(
        nearest_spellings("dayOfWeek", ["day_of_week", "end_date"]),
        vec!["day_of_week"]
    );
}

/// `max_kwg` is 1 edit from `max_kwh` and `VAT` is 2 edits from `FLAT`. Both are shorter than
/// `SHORT_NAME_LEN`, so the first is within `SHORT_NAME_MAX_EDITS` and the second is not.
#[test]
fn a_short_name_allows_one_edit() {
    assert_eq!(nearest_spellings("max_kwg", ["max_kwh"]), vec!["max_kwh"]);
    assert_eq!(
        nearest_spellings("VAT", ["ENERGY", "FLAT", "PARKING_TIME", "TIME"]),
        Vec::<&str>::new()
    );
}

/// `max_durtaoin` is 2 edits from `max_duration`, both swaps, and `total_vat` is 3 edits from
/// `total_cost`. Neither is shorter than `SHORT_NAME_LEN`, so the first is within
/// `LONG_NAME_MAX_EDITS` and the second is not.
#[test]
fn a_long_name_allows_two_edits() {
    assert_eq!(
        nearest_spellings("max_durtaoin", ["max_duration"]),
        vec!["max_duration"]
    );
    assert_eq!(
        nearest_spellings("total_vat", ["total_cost"]),
        Vec::<&str>::new()
    );
}

/// `man_duration` is 1 edit from both `max_duration` and `min_duration`, so nothing says which one
/// the author meant.
#[test]
fn a_tie_suggests_every_tied_candidate() {
    assert_eq!(
        nearest_spellings("man_duration", ["max_duration", "min_duration"]),
        vec!["max_duration", "min_duration"]
    );
}

/// `mex_duration` is 1 edit from `max_duration` and 2 edits from `min_duration`. Both are within
/// `LONG_NAME_MAX_EDITS`, but only the nearest are suggested.
#[test]
fn the_nearer_candidate_wins() {
    assert_eq!(
        nearest_spellings("mex_duration", ["max_duration", "min_duration"]),
        vec!["max_duration"]
    );
}

/// `max_kwh_ac` is long and `max_kwh` is short, so 1 edit is allowed. Deleting the suffix takes 2.
#[test]
fn a_long_name_is_not_a_misspelling_of_a_short_prefix() {
    assert_eq!(
        nearest_spellings("max_kwh_ac", ["max_kwh"]),
        Vec::<&str>::new()
    );
}

/// `AC` and `DC` are both shorter than `TINY_NAME_LEN`, so `nearest_spellings` doesn't allow the 1
/// edit between them. `D_C` differs from `DC` only by `_`, which `spelling_key` removes.
#[test]
fn two_tiny_names_match_only_with_equal_spelling_keys() {
    assert_eq!(
        nearest_spellings("AC", ["AC_1_PHASE", "AC_3_PHASE", "DC"]),
        Vec::<&str>::new()
    );
    assert_eq!(
        nearest_spellings("D_C", ["AC_1_PHASE", "AC_3_PHASE", "DC"]),
        vec!["DC"]
    );
}

/// `idd` is 3 characters, so it isn't tiny, and `nearest_spellings` allows the 1 edit to the tiny
/// `id`.
#[test]
fn a_tiny_candidate_allows_one_edit_from_a_longer_name() {
    assert_eq!(nearest_spellings("idd", ["id", "party_id"]), vec!["id"]);
}