1mod neovim_backed_binding_test_context;
2mod neovim_backed_test_context;
3mod neovim_connection;
4mod vim_binding_test_context;
5mod vim_test_context;
6
7pub use neovim_backed_binding_test_context::*;
8pub use neovim_backed_test_context::*;
9pub use vim_binding_test_context::*;
10pub use vim_test_context::*;
11
12use indoc::indoc;
13use search::BufferSearchBar;
14
15use crate::state::Mode;
16
17#[gpui::test]
18async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
19 let mut cx = VimTestContext::new(cx, false).await;
20 cx.simulate_keystrokes(["h", "j", "k", "l"]);
21 cx.assert_editor_state("hjklˇ");
22}
23
24#[gpui::test]
25async fn test_neovim(cx: &mut gpui::TestAppContext) {
26 let mut cx = NeovimBackedTestContext::new(cx).await;
27
28 cx.simulate_shared_keystroke("i").await;
29 cx.assert_state_matches().await;
30 cx.simulate_shared_keystrokes([
31 "shift-T", "e", "s", "t", " ", "t", "e", "s", "t", "escape", "0", "d", "w",
32 ])
33 .await;
34 cx.assert_state_matches().await;
35 cx.assert_editor_state("ˇtest");
36}
37
38#[gpui::test]
39async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
40 let mut cx = VimTestContext::new(cx, true).await;
41
42 cx.simulate_keystroke("i");
43 assert_eq!(cx.mode(), Mode::Insert);
44
45 // Editor acts as though vim is disabled
46 cx.disable_vim();
47 cx.simulate_keystrokes(["h", "j", "k", "l"]);
48 cx.assert_editor_state("hjklˇ");
49
50 // Selections aren't changed if editor is blurred but vim-mode is still disabled.
51 cx.set_state("«hjklˇ»", Mode::Normal);
52 cx.assert_editor_state("«hjklˇ»");
53 cx.update_editor(|_, cx| cx.blur());
54 cx.assert_editor_state("«hjklˇ»");
55 cx.update_editor(|_, cx| cx.focus_self());
56 cx.assert_editor_state("«hjklˇ»");
57
58 // Enabling dynamically sets vim mode again and restores normal mode
59 cx.enable_vim();
60 assert_eq!(cx.mode(), Mode::Normal);
61 cx.simulate_keystrokes(["h", "h", "h", "l"]);
62 assert_eq!(cx.buffer_text(), "hjkl".to_owned());
63 cx.assert_editor_state("hˇjkl");
64 cx.simulate_keystrokes(["i", "T", "e", "s", "t"]);
65 cx.assert_editor_state("hTestˇjkl");
66
67 // Disabling and enabling resets to normal mode
68 assert_eq!(cx.mode(), Mode::Insert);
69 cx.disable_vim();
70 cx.enable_vim();
71 assert_eq!(cx.mode(), Mode::Normal);
72}
73
74#[gpui::test]
75async fn test_buffer_search(cx: &mut gpui::TestAppContext) {
76 let mut cx = VimTestContext::new(cx, true).await;
77
78 cx.set_state(
79 indoc! {"
80 The quick brown
81 fox juˇmps over
82 the lazy dog"},
83 Mode::Normal,
84 );
85 cx.simulate_keystroke("/");
86
87 let search_bar = cx.workspace(|workspace, cx| {
88 workspace
89 .active_pane()
90 .read(cx)
91 .toolbar()
92 .read(cx)
93 .item_of_type::<BufferSearchBar>()
94 .expect("Buffer search bar should be deployed")
95 });
96
97 search_bar.read_with(cx.cx, |bar, cx| {
98 assert_eq!(bar.query_editor.read(cx).text(cx), "jumps");
99 })
100}
101
102#[gpui::test]
103async fn test_count_down(cx: &mut gpui::TestAppContext) {
104 let mut cx = VimTestContext::new(cx, true).await;
105
106 cx.set_state(indoc! {"aˇa\nbb\ncc\ndd\nee"}, Mode::Normal);
107 cx.simulate_keystrokes(["2", "down"]);
108 cx.assert_editor_state("aa\nbb\ncˇc\ndd\nee");
109 cx.simulate_keystrokes(["9", "down"]);
110 cx.assert_editor_state("aa\nbb\ncc\ndd\neˇe");
111}
112
113#[gpui::test]
114async fn test_end_of_document_710(cx: &mut gpui::TestAppContext) {
115 let mut cx = VimTestContext::new(cx, true).await;
116
117 // goes to end by default
118 cx.set_state(indoc! {"aˇa\nbb\ncc"}, Mode::Normal);
119 cx.simulate_keystrokes(["shift-g"]);
120 cx.assert_editor_state("aa\nbb\ncˇc");
121
122 // can go to line 1 (https://github.com/zed-industries/community/issues/710)
123 cx.simulate_keystrokes(["1", "shift-g"]);
124 cx.assert_editor_state("aˇa\nbb\ncc");
125}