ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
use super::{Element, Location, 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<Location> = items.iter().map(Element::location).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![
        Location { line: 0, col: 1 },
        Location { line: 0, col: 7 },
        Location { line: 0, col: 13 },
    ];
    for line in 1..LINES {
        expected.extend([
            Location { line, col: 0 },
            Location { line, col: 6 },
            Location { line, col: 12 },
        ]);
    }
    expected.push(Location {
        line: LINES,
        col: 0,
    });

    assert_eq!(locations, expected);
}

#[test]
fn should_not_count_a_byte_order_mark_as_a_column() {
    // An editor hides a byte order mark, so the first char after it is column 1 there.
    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(), Location { line: 0, col: 0 });
    assert_eq!(item.location(), Location { line: 0, col: 1 });
}

#[test]
fn should_display_a_location_counting_from_one() {
    // The CLI prints a location as `file:line:col`, which an editor reads counting from 1.
    let doc = parse("[\n  0]".into()).unwrap();
    let root = doc.root();
    let [item] = root.as_array().unwrap() else {
        panic!("expected exactly one item");
    };

    assert_eq!(item.location(), Location { line: 1, col: 2 });
    assert_eq!(item.location().to_string(), "2:3");
}

#[test]
fn should_display_the_largest_location_without_overflowing() {
    let location = Location {
        line: u32::MAX,
        col: u32::MAX,
    };

    assert_eq!(location.to_string(), "4294967296:4294967296");
}