test.rs

  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.simulate_shared_keystrokes([
 30        "shift-T", "e", "s", "t", " ", "t", "e", "s", "t", "escape", "0", "d", "w",
 31    ])
 32    .await;
 33    cx.assert_state_matches().await;
 34    cx.assert_editor_state("ˇtest");
 35}
 36
 37#[gpui::test]
 38async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
 39    let mut cx = VimTestContext::new(cx, true).await;
 40
 41    cx.simulate_keystroke("i");
 42    assert_eq!(cx.mode(), Mode::Insert);
 43
 44    // Editor acts as though vim is disabled
 45    cx.disable_vim();
 46    cx.simulate_keystrokes(["h", "j", "k", "l"]);
 47    cx.assert_editor_state("hjklˇ");
 48
 49    // Selections aren't changed if editor is blurred but vim-mode is still disabled.
 50    cx.set_state("«hjklˇ»", Mode::Normal);
 51    cx.assert_editor_state("«hjklˇ»");
 52    cx.update_editor(|_, cx| cx.blur());
 53    cx.assert_editor_state("«hjklˇ»");
 54    cx.update_editor(|_, cx| cx.focus_self());
 55    cx.assert_editor_state("«hjklˇ»");
 56
 57    // Enabling dynamically sets vim mode again and restores normal mode
 58    cx.enable_vim();
 59    assert_eq!(cx.mode(), Mode::Normal);
 60    cx.simulate_keystrokes(["h", "h", "h", "l"]);
 61    assert_eq!(cx.buffer_text(), "hjkl".to_owned());
 62    cx.assert_editor_state("hˇjkl");
 63    cx.simulate_keystrokes(["i", "T", "e", "s", "t"]);
 64    cx.assert_editor_state("hTestˇjkl");
 65
 66    // Disabling and enabling resets to normal mode
 67    assert_eq!(cx.mode(), Mode::Insert);
 68    cx.disable_vim();
 69    cx.enable_vim();
 70    assert_eq!(cx.mode(), Mode::Normal);
 71}
 72
 73#[gpui::test]
 74async fn test_buffer_search(cx: &mut gpui::TestAppContext) {
 75    let mut cx = VimTestContext::new(cx, true).await;
 76
 77    cx.set_state(
 78        indoc! {"
 79            The quick brown
 80            fox juˇmps over
 81            the lazy dog"},
 82        Mode::Normal,
 83    );
 84    cx.simulate_keystroke("/");
 85
 86    // We now use a weird insert mode with selection when jumping to a single line editor
 87    assert_eq!(cx.mode(), Mode::Insert);
 88
 89    let search_bar = cx.workspace(|workspace, cx| {
 90        workspace
 91            .active_pane()
 92            .read(cx)
 93            .toolbar()
 94            .read(cx)
 95            .item_of_type::<BufferSearchBar>()
 96            .expect("Buffer search bar should be deployed")
 97    });
 98
 99    search_bar.read_with(cx.cx, |bar, cx| {
100        assert_eq!(bar.query_editor.read(cx).text(cx), "jumps");
101    })
102}