Skip to content
Snippets Groups Projects
Commit d944108a authored by gwenn's avatar gwenn
Browse files

Tests: assert both line and cursor

parent ea6c3538
No related branches found
No related tags found
No related merge requests found
......@@ -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"),
);
}
......@@ -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]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment