Work on tests

This commit is contained in:
Conrad Irwin 2024-01-21 21:59:41 -07:00
parent 9d261cf859
commit 4143d3a36e
10 changed files with 181 additions and 35 deletions

View file

@ -73,9 +73,9 @@ pub(crate) struct Up {
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Down {
pub(crate) struct Down {
#[serde(default)]
display_lines: bool,
pub(crate) display_lines: bool,
}
#[derive(Clone, Deserialize, PartialEq)]

View file

@ -3,8 +3,11 @@ mod neovim_backed_test_context;
mod neovim_connection;
mod vim_test_context;
use std::time::Duration;
use command_palette::CommandPalette;
use editor::DisplayPoint;
use gpui::{Action, KeyBinding};
pub use neovim_backed_binding_test_context::*;
pub use neovim_backed_test_context::*;
pub use vim_test_context::*;
@ -12,7 +15,7 @@ pub use vim_test_context::*;
use indoc::indoc;
use search::BufferSearchBar;
use crate::{state::Mode, ModeIndicator};
use crate::{insert::NormalBefore, motion, normal::InsertLineBelow, state::Mode, ModeIndicator};
#[gpui::test]
async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
@ -774,3 +777,73 @@ async fn test_select_all_issue_2170(cx: &mut gpui::TestAppContext) {
Mode::Visual,
);
}
#[gpui::test]
async fn test_jk(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.update(|cx| {
cx.bind_keys([KeyBinding::new(
"j k",
NormalBefore,
Some("vim_mode == insert"),
)])
});
cx.neovim.exec("imap jk <esc>").await;
cx.set_shared_state("ˇhello").await;
cx.simulate_shared_keystrokes(["i", "j", "o", "j", "k"])
.await;
cx.assert_shared_state("jˇohello").await;
}
#[gpui::test]
async fn test_jk_delay(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.update(|cx| {
cx.bind_keys([KeyBinding::new(
"j k",
NormalBefore,
Some("vim_mode == insert"),
)])
});
cx.set_state("ˇhello", Mode::Normal);
cx.simulate_keystrokes(["i", "j"]);
cx.executor().advance_clock(Duration::from_millis(500));
cx.run_until_parked();
cx.assert_state("ˇhello", Mode::Insert);
cx.executor().advance_clock(Duration::from_millis(500));
cx.run_until_parked();
cx.assert_state("jˇhello", Mode::Insert);
cx.simulate_keystrokes(["k", "j", "k"]);
cx.assert_state("jˇkhello", Mode::Normal);
}
#[gpui::test]
async fn test_comma_w(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.update(|cx| {
cx.bind_keys([KeyBinding::new(
", w",
motion::Down {
display_lines: false,
},
Some("vim_mode == normal"),
)])
});
cx.neovim.exec("map ,w j").await;
cx.set_shared_state("ˇhello hello\nhello hello").await;
cx.simulate_shared_keystrokes(["f", "o", ";", ",", "w"])
.await;
cx.assert_shared_state("hello hello\nhello hellˇo").await;
cx.set_shared_state("ˇhello hello\nhello hello").await;
cx.simulate_shared_keystrokes(["f", "o", ";", ",", "i"])
.await;
cx.assert_shared_state("hellˇo hello\nhello hello").await;
cx.assert_shared_mode(Mode::Insert).await;
}

View file

@ -52,7 +52,7 @@ pub struct NeovimBackedTestContext {
// Lookup for exempted assertions. Keyed by the insertion text, and with a value indicating which
// bindings are exempted. If None, all bindings are ignored for that insertion text.
exemptions: HashMap<String, Option<HashSet<String>>>,
neovim: NeovimConnection,
pub(crate) neovim: NeovimConnection,
last_set_state: Option<String>,
recent_keystrokes: Vec<String>,

View file

@ -42,6 +42,7 @@ pub enum NeovimData {
Key(String),
Get { state: String, mode: Option<Mode> },
ReadRegister { name: char, value: String },
Exec { command: String },
SetOption { value: String },
}
@ -269,6 +270,32 @@ impl NeovimConnection {
);
}
#[cfg(feature = "neovim")]
pub async fn exec(&mut self, value: &str) {
self.nvim
.command_output(format!("{}", value).as_str())
.await
.unwrap();
self.data.push_back(NeovimData::Exec {
command: value.to_string(),
})
}
#[cfg(not(feature = "neovim"))]
pub async fn exec(&mut self, value: &str) {
if let Some(NeovimData::Get { .. }) = self.data.front() {
self.data.pop_front();
};
assert_eq!(
self.data.pop_front(),
Some(NeovimData::Exec {
command: value.to_string(),
}),
"operation does not match recorded script. re-record with --features=neovim"
);
}
#[cfg(not(feature = "neovim"))]
pub async fn read_register(&mut self, register: char) -> String {
if let Some(NeovimData::Get { .. }) = self.data.front() {

View file

@ -0,0 +1,15 @@
{"Exec":{"command":"map ,w j"}}
{"Put":{"state":"ˇhello hello\nhello hello"}}
{"Key":"f"}
{"Key":"o"}
{"Key":";"}
{"Key":","}
{"Key":"w"}
{"Get":{"state":"hello hello\nhello hellˇo","mode":"Normal"}}
{"Put":{"state":"ˇhello hello\nhello hello"}}
{"Key":"f"}
{"Key":"o"}
{"Key":";"}
{"Key":","}
{"Key":"i"}
{"Get":{"state":"hellˇo hello\nhello hello","mode":"Insert"}}

View file

@ -0,0 +1,8 @@
{"Exec":{"command":"imap jk <esc>"}}
{"Put":{"state":"ˇhello"}}
{"Key":"i"}
{"Key":"j"}
{"Key":"o"}
{"Key":"j"}
{"Key":"k"}
{"Get":{"state":"jˇohello","mode":"Normal"}}