2026-03-24 13:40:29 +00:00

34 lines
1.2 KiB
Rust

use prometeu_analysis::TextIndex;
#[test]
fn span_to_range_uses_utf16() {
// "ação" has: a (1), ç (1 utf16, BMP), ã (1 utf16, BMP), o (1) → total 4 utf16 units
let s = "ação\n"; // newline to ensure line indexing works
let idx = TextIndex::new(s);
// Byte offsets for each char boundary
let bytes: Vec<u32> = s.char_indices().map(|(i, _)| i as u32).collect();
// last boundary is newline; the previous is end of line content
let end_of_line = bytes[bytes.len() - 1];
let (line, col) = idx.byte_to_lsp(end_of_line);
assert_eq!(line, 0);
assert_eq!(col, 4, "UTF-16 column should be 4 for 'ação'");
}
#[test]
fn position_to_byte_roundtrip() {
let s = "😀a\n"; // emoji is surrogate pair in UTF-16
let idx = TextIndex::new(s);
// emoji (😀) occupies 2 utf16 code units; 'a' adds 1
let emoji_end_byte = "😀".len() as u32; // byte length of emoji
// From bytes→(line,col)
let (line, col) = idx.byte_to_lsp(emoji_end_byte);
assert_eq!(line, 0);
assert_eq!(col, 2, "emoji should count as 2 UTF-16 units");
// Back from (line,col)→bytes should land at the same boundary
let back = idx.lsp_to_byte(line, col);
assert_eq!(back, emoji_end_byte);
}