Skip to content

Commit

Permalink
vim test redux (#11709)
Browse files Browse the repository at this point in the history
This cleans up the neovim-backed vim tests:
- removed exempted tests (we'll rely on bug reports to find missing edge
cases)
- moved all assertions into non-async fn's so that failures are
reporting on the right file/line
- removed the NeovimBackedBindingTestContext
- renamed a few things to make them clearer
- reduced the number of permutations tested in some cases to reduce
slowest test from 60s to 5s

Release Notes:

- N/A
  • Loading branch information
ConradIrwin committed May 11, 2024
1 parent 48cba32 commit f550f23
Show file tree
Hide file tree
Showing 38 changed files with 2,926 additions and 7,120 deletions.
27 changes: 3 additions & 24 deletions crates/editor/src/test/editor_test_context.rs
Expand Up @@ -193,32 +193,11 @@ impl EditorTestContext {
self.assertion_cx.context()
}

pub fn simulate_keystroke(&mut self, keystroke_text: &str) -> ContextHandle {
let keystroke_under_test_handle =
self.add_assertion_context(format!("Simulated Keystroke: {:?}", keystroke_text));
// unlike cx.simulate_keystrokes(), this does not run_until_parked
// so you can use it to test detailed timing
pub fn simulate_keystroke(&mut self, keystroke_text: &str) {
let keystroke = Keystroke::parse(keystroke_text).unwrap();

self.cx.dispatch_keystroke(self.window, keystroke);

keystroke_under_test_handle
}

pub fn simulate_keystrokes<const COUNT: usize>(
&mut self,
keystroke_texts: [&str; COUNT],
) -> ContextHandle {
let keystrokes_under_test_handle =
self.add_assertion_context(format!("Simulated Keystrokes: {:?}", keystroke_texts));
for keystroke_text in keystroke_texts.into_iter() {
self.simulate_keystroke(keystroke_text);
}
// it is common for keyboard shortcuts to kick off async actions, so this ensures that they are complete
// before returning.
// NOTE: we don't do this in simulate_keystroke() because a possible cause of bugs is that typing too
// quickly races with async actions.
self.cx.background_executor.run_until_parked();

keystrokes_under_test_handle
}

pub fn run_until_parked(&mut self) {
Expand Down
67 changes: 27 additions & 40 deletions crates/vim/src/change_list.rs
Expand Up @@ -102,20 +102,16 @@ mod test {

cx.set_shared_state("ˇ").await;

cx.simulate_shared_keystrokes([
"i", "1", "1", "escape", "shift-o", "2", "2", "escape", "shift-g", "o", "3", "3",
"escape",
])
.await;
cx.simulate_shared_keystrokes("i 1 1 escape shift-o 2 2 escape shift-g o 3 3 escape")
.await;

cx.assert_shared_state(indoc! {
cx.shared_state().await.assert_eq(indoc! {
"22
11
3ˇ3"
})
.await;
});

cx.simulate_shared_keystrokes(["g", ";"]).await;
cx.simulate_shared_keystrokes("g ;").await;
// NOTE: this matches nvim when I type it into it
// but in tests, nvim always reports the column as 0...
cx.assert_state(
Expand All @@ -126,7 +122,7 @@ mod test {
},
Mode::Normal,
);
cx.simulate_shared_keystrokes(["g", ";"]).await;
cx.simulate_shared_keystrokes("g ;").await;
cx.assert_state(
indoc! {
"2ˇ2
Expand All @@ -135,7 +131,7 @@ mod test {
},
Mode::Normal,
);
cx.simulate_shared_keystrokes(["g", ";"]).await;
cx.simulate_shared_keystrokes("g ;").await;
cx.assert_state(
indoc! {
"22
Expand All @@ -144,7 +140,7 @@ mod test {
},
Mode::Normal,
);
cx.simulate_shared_keystrokes(["g", ","]).await;
cx.simulate_shared_keystrokes("g ,").await;
cx.assert_state(
indoc! {
"2ˇ2
Expand All @@ -153,9 +149,8 @@ mod test {
},
Mode::Normal,
);
cx.simulate_shared_keystrokes(["shift-g", "i", "4", "4", "escape"])
.await;
cx.simulate_shared_keystrokes(["g", ";"]).await;
cx.simulate_shared_keystrokes("shift-g i 4 4 escape").await;
cx.simulate_shared_keystrokes("g ;").await;
cx.assert_state(
indoc! {
"22
Expand All @@ -164,7 +159,7 @@ mod test {
},
Mode::Normal,
);
cx.simulate_shared_keystrokes(["g", ";"]).await;
cx.simulate_shared_keystrokes("g ;").await;
cx.assert_state(
indoc! {
"2ˇ2
Expand All @@ -182,22 +177,18 @@ mod test {
"one two
three fˇour"})
.await;
cx.simulate_shared_keystrokes(["x", "k", "d", "i", "w", "^", "x"])
.await;
cx.assert_shared_state(indoc! {
cx.simulate_shared_keystrokes("x k d i w ^ x").await;
cx.shared_state().await.assert_eq(indoc! {
"ˇne•
three fur"})
.await;
cx.simulate_shared_keystrokes(["2", "g", ";"]).await;
cx.assert_shared_state(indoc! {
three fur"});
cx.simulate_shared_keystrokes("2 g ;").await;
cx.shared_state().await.assert_eq(indoc! {
"ne•
three fˇur"})
.await;
cx.simulate_shared_keystrokes(["g", ","]).await;
cx.assert_shared_state(indoc! {
three fˇur"});
cx.simulate_shared_keystrokes("g ,").await;
cx.shared_state().await.assert_eq(indoc! {
"ˇne•
three fur"})
.await;
three fur"});
}

#[gpui::test]
Expand All @@ -207,13 +198,11 @@ mod test {
"one two
three fˇr"})
.await;
cx.simulate_shared_keystrokes(["i", "o", "escape", "k", "g", "i"])
.await;
cx.simulate_shared_keystrokes(["u", "escape"]).await;
cx.assert_shared_state(indoc! {
cx.simulate_shared_keystrokes("i o escape k g i").await;
cx.simulate_shared_keystrokes("u escape").await;
cx.shared_state().await.assert_eq(indoc! {
"one two
three foˇur"})
.await;
three foˇur"});
}

#[gpui::test]
Expand All @@ -223,11 +212,9 @@ mod test {
"one two
three fˇr"})
.await;
cx.simulate_shared_keystrokes(["i", "o", "escape", "k", "`", "."])
.await;
cx.assert_shared_state(indoc! {
cx.simulate_shared_keystrokes("i o escape k ` .").await;
cx.shared_state().await.assert_eq(indoc! {
"one two
three fˇor"})
.await;
three fˇor"});
}
}
67 changes: 28 additions & 39 deletions crates/vim/src/command.rs
Expand Up @@ -404,15 +404,14 @@ mod test {
c"})
.await;

cx.simulate_shared_keystrokes([":", "j", "enter"]).await;
cx.simulate_shared_keystrokes(": j enter").await;

// hack: our cursor positionining after a join command is wrong
cx.simulate_shared_keystrokes(["^"]).await;
cx.assert_shared_state(indoc! {
cx.simulate_shared_keystrokes("^").await;
cx.shared_state().await.assert_eq(indoc! {
"ˇa b
c"
})
.await;
});
}

#[gpui::test]
Expand All @@ -424,12 +423,11 @@ mod test {
b
c"})
.await;
cx.simulate_shared_keystrokes([":", "3", "enter"]).await;
cx.assert_shared_state(indoc! {"
cx.simulate_shared_keystrokes(": 3 enter").await;
cx.shared_state().await.assert_eq(indoc! {"
a
b
ˇc"})
.await;
ˇc"});
}

#[gpui::test]
Expand All @@ -441,22 +439,17 @@ mod test {
b
c"})
.await;
cx.simulate_shared_keystrokes([":", "%", "s", "/", "b", "/", "d", "enter"])
.await;
cx.assert_shared_state(indoc! {"
cx.simulate_shared_keystrokes(": % s / b / d enter").await;
cx.shared_state().await.assert_eq(indoc! {"
a
ˇd
c"})
c"});
cx.simulate_shared_keystrokes(": % s : . : \\ 0 \\ 0 enter")
.await;
cx.simulate_shared_keystrokes([
":", "%", "s", ":", ".", ":", "\\", "0", "\\", "0", "enter",
])
.await;
cx.assert_shared_state(indoc! {"
cx.shared_state().await.assert_eq(indoc! {"
aa
dd
ˇcc"})
.await;
ˇcc"});
}

#[gpui::test]
Expand All @@ -469,22 +462,18 @@ mod test {
a
c"})
.await;
cx.simulate_shared_keystrokes([":", "/", "b", "enter"])
.await;
cx.assert_shared_state(indoc! {"
cx.simulate_shared_keystrokes(": / b enter").await;
cx.shared_state().await.assert_eq(indoc! {"
a
ˇb
a
c"})
.await;
cx.simulate_shared_keystrokes([":", "?", "a", "enter"])
.await;
cx.assert_shared_state(indoc! {"
c"});
cx.simulate_shared_keystrokes(": ? a enter").await;
cx.shared_state().await.assert_eq(indoc! {"
ˇa
b
a
c"})
.await;
c"});
}

#[gpui::test]
Expand All @@ -493,23 +482,23 @@ mod test {
let path = Path::new("/root/dir/file.rs");
let fs = cx.workspace(|workspace, cx| workspace.project().read(cx).fs().clone());

cx.simulate_keystrokes(["i", "@", "escape"]);
cx.simulate_keystrokes([":", "w", "enter"]);
cx.simulate_keystrokes("i @ escape");
cx.simulate_keystrokes(": w enter");

assert_eq!(fs.load(&path).await.unwrap(), "@\n");

fs.as_fake().insert_file(path, b"oops\n".to_vec()).await;

// conflict!
cx.simulate_keystrokes(["i", "@", "escape"]);
cx.simulate_keystrokes([":", "w", "enter"]);
cx.simulate_keystrokes("i @ escape");
cx.simulate_keystrokes(": w enter");
assert!(cx.has_pending_prompt());
// "Cancel"
cx.simulate_prompt_answer(0);
assert_eq!(fs.load(&path).await.unwrap(), "oops\n");
assert!(!cx.has_pending_prompt());
// force overwrite
cx.simulate_keystrokes([":", "w", "!", "enter"]);
cx.simulate_keystrokes(": w ! enter");
assert!(!cx.has_pending_prompt());
assert_eq!(fs.load(&path).await.unwrap(), "@@\n");
}
Expand All @@ -518,13 +507,13 @@ mod test {
async fn test_command_quit(cx: &mut TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;

cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
cx.simulate_keystrokes(": n e w enter");
cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
cx.simulate_keystrokes([":", "q", "enter"]);
cx.simulate_keystrokes(": q enter");
cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 1));
cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
cx.simulate_keystrokes(": n e w enter");
cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
cx.simulate_keystrokes([":", "q", "a", "enter"]);
cx.simulate_keystrokes(": q a enter");
cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 0));
}
}

0 comments on commit f550f23

Please sign in to comment.