58 lines
2.0 KiB
Rust

use prometeu_analysis::TextIndex;
#[test]
fn text_index_ascii_roundtrip() {
let text = "hello\nworld\nthis is ascii";
let idx = TextIndex::new(text);
// Verify round-trip on all character boundaries
let mut boundaries: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
boundaries.push(text.len());
for &b in &boundaries {
let (line, col16) = idx.byte_to_lsp(b as u32);
let b2 = idx.lsp_to_byte(line, col16);
assert_eq!(b2, b as u32, "roundtrip failed for byte {} -> (l={},c16={})", b, line, col16);
}
// Some direct checks
// start: (0,0)
assert_eq!(idx.byte_to_lsp(0), (0, 0));
// after "hello" (5), before '\n': line 0, col=5
assert_eq!(idx.byte_to_lsp(5), (0, 5));
// after '\n' (6): line 1, col=0
assert_eq!(idx.byte_to_lsp(6), (1, 0));
}
#[test]
fn text_index_unicode_roundtrip_utf16() {
// "a" (1B, 1u16), "é" (2B, 1u16), "🙂" (4B, 2u16), "b" (1B, 1u16)
let text = "aé🙂b";
let idx = TextIndex::new(text);
// character boundaries + end
let mut boundaries: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
boundaries.push(text.len());
for &b in &boundaries {
let (line, col16) = idx.byte_to_lsp(b as u32);
let b2 = idx.lsp_to_byte(line, col16);
assert_eq!(b2, b as u32, "unicode roundtrip failed for byte {} -> (l={},c16={})", b, line, col16);
}
// Expected columns on line 0
// bytes: [0:'a'][1..2:'é'][3..6:'🙂'][7:'b'][8:end]
assert_eq!(idx.byte_to_lsp(0), (0, 0)); // before 'a'
assert_eq!(idx.byte_to_lsp(1), (0, 1)); // after 'a'
assert_eq!(idx.byte_to_lsp(3), (0, 2)); // after 'a' + 'é' (1+1 utf16)
assert_eq!(idx.byte_to_lsp(7), (0, 4)); // after '🙂' (2 utf16) => 1+1+2=4
assert_eq!(idx.byte_to_lsp(8), (0, 5)); // after 'b'
// and inverse, specific columns
assert_eq!(idx.lsp_to_byte(0, 0), 0);
assert_eq!(idx.lsp_to_byte(0, 1), 1);
assert_eq!(idx.lsp_to_byte(0, 2), 3);
assert_eq!(idx.lsp_to_byte(0, 4), 7);
assert_eq!(idx.lsp_to_byte(0, 5), 8);
}