revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
//! Autocomplete Unicode handling tests
//!
//! Tests for char-index-based cursor with multi-byte characters.
//! Verifies fix for byte/char index confusion that caused panics.

use revue::event::{Key, KeyEvent};
use revue::widget::autocomplete::Autocomplete;

#[test]
fn test_autocomplete_value_with_emoji_sets_correct_cursor() {
    let a = Autocomplete::new().value("Hello πŸŽ‰");
    assert_eq!(a.get_value(), "Hello πŸŽ‰");
}

#[test]
fn test_autocomplete_type_emoji_then_backspace_no_panic() {
    let mut a = Autocomplete::new();
    a.handle_key(KeyEvent::new(Key::Char('πŸ˜€')));
    assert_eq!(a.get_value(), "πŸ˜€");

    a.handle_key(KeyEvent::new(Key::Backspace));
    assert_eq!(a.get_value(), "");
}

#[test]
fn test_autocomplete_type_cjk_then_backspace() {
    let mut a = Autocomplete::new();
    a.handle_key(KeyEvent::new(Key::Char('ν•œ')));
    a.handle_key(KeyEvent::new(Key::Char('κΈ€')));
    assert_eq!(a.get_value(), "ν•œκΈ€");

    a.handle_key(KeyEvent::new(Key::Backspace));
    assert_eq!(a.get_value(), "ν•œ");

    a.handle_key(KeyEvent::new(Key::Backspace));
    assert_eq!(a.get_value(), "");
}

#[test]
fn test_autocomplete_delete_key_cjk() {
    let mut a = Autocomplete::new();
    a.handle_key(KeyEvent::new(Key::Char('κ°€')));
    a.handle_key(KeyEvent::new(Key::Char('λ‚˜')));
    assert_eq!(a.get_value(), "κ°€λ‚˜");

    a.handle_key(KeyEvent::new(Key::Home));
    a.handle_key(KeyEvent::new(Key::Delete));
    assert_eq!(a.get_value(), "λ‚˜");
}

#[test]
fn test_autocomplete_arrow_keys_with_cjk() {
    let mut a = Autocomplete::new();
    a.handle_key(KeyEvent::new(Key::Char('A')));
    a.handle_key(KeyEvent::new(Key::Char('ν•œ')));
    a.handle_key(KeyEvent::new(Key::Char('B')));
    assert_eq!(a.get_value(), "Aν•œB");

    a.handle_key(KeyEvent::new(Key::Left));
    a.handle_key(KeyEvent::new(Key::Left));
    a.handle_key(KeyEvent::new(Key::Char('X')));
    assert_eq!(a.get_value(), "AXν•œB");
}

#[test]
fn test_autocomplete_end_key_with_multibyte() {
    let mut a = Autocomplete::new().value("μ•ˆλ…•πŸŽ‰");
    a.handle_key(KeyEvent::new(Key::Home));
    a.handle_key(KeyEvent::new(Key::End));
    a.handle_key(KeyEvent::new(Key::Char('!')));
    assert_eq!(a.get_value(), "μ•ˆλ…•πŸŽ‰!");
}