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

58 lines
2.1 KiB
Rust

use prometeu_analysis::TextIndex;
#[test]
fn text_index_ascii_roundtrip() {
let text = "hello\nworld\nthis is ascii";
let idx = TextIndex::new(text);
// Verifica round-trip em todas as fronteiras de char
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 falhou para byte {} -> (l={},c16={})", b, line, col16);
}
// Alguns checks diretos
// início: (0,0)
assert_eq!(idx.byte_to_lsp(0), (0, 0));
// após "hello" (5), antes do '\n': linha 0, col=5
assert_eq!(idx.byte_to_lsp(5), (0, 5));
// após '\n' (6): linha 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);
// fronteiras de char + fim
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 unicode falhou para byte {} -> (l={},c16={})", b, line, col16);
}
// Checagens de colunas esperadas na linha 0
// bytes: [0:'a'][1..2:'é'][3..6:'🙂'][7:'b'][8:end]
assert_eq!(idx.byte_to_lsp(0), (0, 0)); // antes de 'a'
assert_eq!(idx.byte_to_lsp(1), (0, 1)); // após 'a'
assert_eq!(idx.byte_to_lsp(3), (0, 2)); // após 'a' + 'é' (1+1 utf16)
assert_eq!(idx.byte_to_lsp(7), (0, 4)); // após '🙂' (2 utf16) => 1+1+2=4
assert_eq!(idx.byte_to_lsp(8), (0, 5)); // após 'b'
// e inverso, colunas específicas
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);
}