diff --git a/src/actions.rs b/src/actions.rs index 03e1202..dfcf1d4 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -159,3 +159,57 @@ pub fn insert_newline(state: &mut State) { state.cursor.line += 1; state.cursor.column = 0; } + +#[cfg(test)] +mod tests { + use super::*; + + fn state(src: &str) -> State { + State::from_str(None, src).unwrap() + } + + #[test] + fn backspace_empty() { + let mut state = state(""); + delete_char_backward(&mut state); + } + + #[test] + fn append_on_newline() { + let mut state = state("test\ntest\n"); + state.cursor.column = 4; + append_mode(&mut state); + assert_eq!(state.cursor.line, 1); + assert_eq!(state.cursor.column, 0); + } + + #[test] + fn append_at_eof() { + let mut state = state("test\n"); + state.cursor.column = 4; + append_mode(&mut state); + assert_eq!(state.cursor.line, 1); + assert_eq!(state.cursor.column, 0); + } + + #[test] + fn move_line_up_preserve_column() { + let mut state = state("test\n\ntest\n"); + state.cursor.line = 2; + state.cursor.column = 3; + move_line_up(&mut state); + move_line_up(&mut state); + assert_eq!(state.cursor.line, 0); + assert_eq!(state.cursor.column, 3); + } + + #[test] + fn move_line_down_preserve_column() { + let mut state = state("test\n\ntest\n"); + state.cursor.column = 3; + move_line_down(&mut state); + move_line_down(&mut state); + assert_eq!(state.cursor.line, 2); + assert_eq!(state.cursor.column, 3); + } +}