From d944108aa0fdf436c4a9b98964f157ceec10537a Mon Sep 17 00:00:00 2001 From: gwenn <gtreguier@gmail.com> Date: Tue, 22 May 2018 21:54:01 +0200 Subject: [PATCH] Tests: assert both line and cursor --- src/test/common.rs | 83 +++++++++++++++++++++++++++++++++------------- src/test/mod.rs | 7 ++-- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/test/common.rs b/src/test/common.rs index 1dd52adb..021828e4 100644 --- a/src/test/common.rs +++ b/src/test/common.rs @@ -4,28 +4,34 @@ use error::ReadlineError; #[test] fn home_key() { - assert_cursor(("", ""), &[KeyPress::Home, KeyPress::Enter], 0); - assert_cursor(("Hi", ""), &[KeyPress::Home, KeyPress::Enter], 0); + assert_cursor(("", ""), &[KeyPress::Home, KeyPress::Enter], ("", "")); + assert_cursor(("Hi", ""), &[KeyPress::Home, KeyPress::Enter], ("", "Hi")); } #[test] fn end_key() { - assert_cursor(("", ""), &[KeyPress::End, KeyPress::Enter], 0); - assert_cursor(("H", "i"), &[KeyPress::End, KeyPress::Enter], 2); + assert_cursor(("", ""), &[KeyPress::End, KeyPress::Enter], ("", "")); + assert_cursor(("H", "i"), &[KeyPress::End, KeyPress::Enter], ("Hi", "")); + assert_cursor(("", "Hi"), &[KeyPress::End, KeyPress::Enter], ("Hi", "")); } #[test] fn left_key() { - assert_cursor(("Hi", ""), &[KeyPress::Left, KeyPress::Enter], 1); - assert_cursor(("H", "i"), &[KeyPress::Left, KeyPress::Enter], 0); - assert_cursor(("", "Hi"), &[KeyPress::Left, KeyPress::Enter], 0); + assert_cursor(("Hi", ""), &[KeyPress::Left, KeyPress::Enter], ("H", "i")); + assert_cursor(("H", "i"), &[KeyPress::Left, KeyPress::Enter], ("", "Hi")); + assert_cursor(("", "Hi"), &[KeyPress::Left, KeyPress::Enter], ("", "Hi")); } #[test] fn right_key() { - assert_cursor(("", ""), &[KeyPress::Right, KeyPress::Enter], 0); - assert_cursor(("", "Hi"), &[KeyPress::Right, KeyPress::Enter], 1); - assert_cursor(("B", "ye"), &[KeyPress::Right, KeyPress::Enter], 2); + assert_cursor(("", ""), &[KeyPress::Right, KeyPress::Enter], ("", "")); + assert_cursor(("", "Hi"), &[KeyPress::Right, KeyPress::Enter], ("H", "i")); + assert_cursor( + ("B", "ye"), + &[KeyPress::Right, KeyPress::Enter], + ("By", "e"), + ); + assert_cursor(("H", "i"), &[KeyPress::Right, KeyPress::Enter], ("Hi", "")); } #[test] @@ -69,44 +75,75 @@ fn interrupt_key() { #[test] fn delete_key() { - assert_line_with_initial(("a", ""), &[KeyPress::Delete, KeyPress::Enter], "a"); - assert_line_with_initial(("", "a"), &[KeyPress::Delete, KeyPress::Enter], ""); + assert_cursor(("a", ""), &[KeyPress::Delete, KeyPress::Enter], ("a", "")); + assert_cursor(("", "a"), &[KeyPress::Delete, KeyPress::Enter], ("", "")); } #[test] fn ctrl_t() { - assert_line_with_initial(("a", "b"), &[KeyPress::Ctrl('T'), KeyPress::Enter], "ba"); - assert_line_with_initial( + assert_cursor( + ("a", "b"), + &[KeyPress::Ctrl('T'), KeyPress::Enter], + ("ba", ""), + ); + assert_cursor( ("ab", "cd"), &[KeyPress::Ctrl('T'), KeyPress::Enter], - "acbd", + ("acb", "d"), ); } #[test] fn ctrl_u() { - assert_line_with_initial(("a", "b"), &[KeyPress::Ctrl('U'), KeyPress::Enter], "b"); - assert_line_with_initial(("", "a"), &[KeyPress::Ctrl('U'), KeyPress::Enter], "a"); + assert_cursor( + ("a", "b"), + &[KeyPress::Ctrl('U'), KeyPress::Enter], + ("", "b"), + ); + assert_cursor( + ("", "a"), + &[KeyPress::Ctrl('U'), KeyPress::Enter], + ("", "a"), + ); } #[test] fn ctrl_v() { - assert_line( + assert_cursor( + ("", ""), &[KeyPress::Ctrl('V'), KeyPress::Char('\t'), KeyPress::Enter], - "\t", + ("\t", ""), ); } #[test] fn ctrl_w() { - assert_line_with_initial( + assert_cursor( ("Hello, ", "world"), &[KeyPress::Ctrl('W'), KeyPress::Enter], - "world", + ("", "world"), ); - assert_line_with_initial( + assert_cursor( ("Hello, world.", ""), &[KeyPress::Ctrl('W'), KeyPress::Enter], - "Hello, ", + ("Hello, ", ""), + ); +} + +#[test] +fn ctrl_y() { + assert_cursor( + ("Hello, ", "world"), + &[KeyPress::Ctrl('W'), KeyPress::Ctrl('Y'), KeyPress::Enter], + ("Hello, ", "world"), + ); +} + +#[test] +fn undo() { + assert_cursor( + ("Hello, ", "world"), + &[KeyPress::Ctrl('W'), KeyPress::Ctrl('_'), KeyPress::Enter], + ("Hello, ", "world"), ); } diff --git a/src/test/mod.rs b/src/test/mod.rs index 10c0752c..2a00d6f2 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -56,10 +56,11 @@ fn assert_line_with_initial(initial: (&str, &str), keys: &[KeyPress], expected_l let actual_line = editor.readline_with_initial(">>", initial).unwrap(); assert_eq!(expected_line, actual_line); } -fn assert_cursor(initial: (&str, &str), keys: &[KeyPress], expected_cursor: usize) { +fn assert_cursor(initial: (&str, &str), keys: &[KeyPress], expected: (&str, &str)) { let mut editor = init_editor(keys); - editor.readline_with_initial("", initial).unwrap(); - assert_eq!(expected_cursor, editor.term.cursor); + let actual_line = editor.readline_with_initial("", initial).unwrap(); + assert_eq!(expected.0.to_owned() + expected.1, actual_line); + assert_eq!(expected.0.len(), editor.term.cursor); } #[test] -- GitLab