ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
use super::parse;

#[test]
fn should_count_columns_in_chars_after_multibyte_chars() {
    // Each line holds three items, so the columns of the second and third items depend on how
    // the parser counts the `é` and `😀` before them. The `\r\n` line breaks check that a `\r`
    // doesn't start a line of its own.
    const LINES: u32 = 3;
    const LINE: &str = "\"é😀\", \"é😀\", \"é😀\",\r\n";

    let mut source = String::from("[");
    for _ in 0..LINES {
        source.push_str(LINE);
    }
    source.push_str("0]");

    let doc = parse(source.as_str().into()).unwrap();
    let items = doc.root().as_array().unwrap();
    let locations: Vec<(u32, u32)> = items.iter().map(|item| item.location().into()).collect();

    // `"é😀"` is 4 chars and 8 bytes, and the columns below count chars. The `[` shifts the
    // first line one column right, and the closing `0` sits alone on the last line.
    let mut expected = vec![(0, 1), (0, 7), (0, 13)];
    for line in 1..LINES {
        expected.extend([(line, 0), (line, 6), (line, 12)]);
    }
    expected.push((LINES, 0));

    assert_eq!(locations, expected);
}

#[test]
fn should_count_a_byte_order_mark_as_one_column() {
    // A byte order mark counts as one char of the first line.
    let doc = parse("\u{feff}[0]".into()).unwrap();
    let root = doc.root();
    let [item] = root.as_array().unwrap() else {
        panic!("expected exactly one item");
    };

    assert_eq!(root.location(), (0, 1));
    assert_eq!(item.location(), (0, 2));
}