use super::{Element, Location, parse};
#[test]
fn should_count_columns_in_chars_after_multibyte_chars() {
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();
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() {
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() {
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");
}