use crate::ui::input::InputEditor;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
fn press(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::empty())
}
fn type_str(editor: &mut InputEditor, s: &str) {
for c in s.chars() {
editor.handle_key(press(KeyCode::Char(c)));
}
}
#[test]
fn typing_ascii_keeps_cursor_in_sync() {
let mut editor = InputEditor::new();
type_str(&mut editor, "hello");
assert_eq!(editor.buffer.as_str(), "hello");
assert_eq!(editor.cursor, 5);
}
#[test]
fn typing_multibyte_chars_does_not_panic() {
let mut editor = InputEditor::new();
type_str(&mut editor, "på "); assert_eq!(editor.buffer.as_str(), "på ");
assert_eq!(editor.cursor, editor.buffer.len()); }
#[test]
fn typing_mixed_ascii_and_multibyte() {
let mut editor = InputEditor::new();
type_str(&mut editor, "hei på deg så fin dag æøå");
assert_eq!(editor.buffer.as_str(), "hei på deg så fin dag æøå");
assert_eq!(editor.cursor, editor.buffer.len());
}
#[test]
fn backspace_after_multibyte_does_not_panic() {
let mut editor = InputEditor::new();
type_str(&mut editor, "å");
editor.handle_key(press(KeyCode::Backspace));
assert_eq!(editor.buffer.as_str(), "");
assert_eq!(editor.cursor, 0);
}
#[test]
fn left_arrow_steps_one_char_not_one_byte() {
let mut editor = InputEditor::new();
type_str(&mut editor, "aåb");
assert_eq!(editor.cursor, 4);
editor.handle_key(press(KeyCode::Left));
assert_eq!(editor.cursor, 3);
editor.handle_key(press(KeyCode::Left));
assert_eq!(editor.cursor, 1);
}
#[test]
fn right_arrow_steps_one_char_not_one_byte() {
let mut editor = InputEditor::new();
type_str(&mut editor, "aåb");
editor.cursor = 0;
editor.handle_key(press(KeyCode::Right));
assert_eq!(editor.cursor, 1); editor.handle_key(press(KeyCode::Right));
assert_eq!(editor.cursor, 3); }
#[test]
fn enter_returns_buffer_and_resets() {
let mut editor = InputEditor::new();
type_str(&mut editor, "hei på");
let out = editor.handle_key(press(KeyCode::Enter)).unwrap();
assert_eq!(out.as_str(), "hei på");
assert_eq!(editor.cursor, 0);
assert_eq!(editor.buffer.as_str(), "");
}