From 3baf125f4a2800f98990eb59e086eb8125f2bfa0 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 11 Dec 2023 15:13:11 -0700 Subject: [PATCH] All the easy tests? --- crates/vim2/src/insert.rs | 184 +++--- crates/vim2/src/motion.rs | 297 +++++---- crates/vim2/src/normal/case.rs | 68 +- crates/vim2/src/visual.rs | 1133 ++++++++++++++++---------------- 4 files changed, 843 insertions(+), 839 deletions(-) diff --git a/crates/vim2/src/insert.rs b/crates/vim2/src/insert.rs index 81aac70d02107a850c02a2c4a00f929616a2dbb5..7c272318287bf323e693121d0e0400cedaa83e75 100644 --- a/crates/vim2/src/insert.rs +++ b/crates/vim2/src/insert.rs @@ -36,100 +36,90 @@ fn normal_before(_: &mut Workspace, action: &NormalBefore, cx: &mut ViewContext< } } -// #[cfg(test)] -// mod test { -// use std::sync::Arc; - -// use gpui::executor::Deterministic; - -// use crate::{ -// state::Mode, -// test::{NeovimBackedTestContext, VimTestContext}, -// }; - -// #[gpui::test] -// async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) { -// let mut cx = VimTestContext::new(cx, true).await; -// cx.simulate_keystroke("i"); -// assert_eq!(cx.mode(), Mode::Insert); -// cx.simulate_keystrokes(["T", "e", "s", "t"]); -// cx.assert_editor_state("Testˇ"); -// cx.simulate_keystroke("escape"); -// assert_eq!(cx.mode(), Mode::Normal); -// cx.assert_editor_state("Tesˇt"); -// } - -// #[gpui::test] -// async fn test_insert_with_counts( -// deterministic: Arc, -// cx: &mut gpui::TestAppContext, -// ) { -// let mut cx = NeovimBackedTestContext::new(cx).await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["5", "i", "-", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("----ˇ-hello\n").await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["5", "a", "-", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("h----ˇ-ello\n").await; - -// cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("---ˇ-h-----ello\n").await; - -// cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("----h-----ello--ˇ-\n").await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("hello\noi\noi\noˇi\n").await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("oi\noi\noˇi\nhello\n").await; -// } - -// #[gpui::test] -// async fn test_insert_with_repeat( -// deterministic: Arc, -// cx: &mut gpui::TestAppContext, -// ) { -// let mut cx = NeovimBackedTestContext::new(cx).await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["3", "i", "-", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("--ˇ-hello\n").await; -// cx.simulate_shared_keystrokes(["."]).await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("----ˇ--hello\n").await; -// cx.simulate_shared_keystrokes(["2", "."]).await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("-----ˇ---hello\n").await; - -// cx.set_shared_state("ˇhello\n").await; -// cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"]) -// .await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("hello\nkk\nkˇk\n").await; -// cx.simulate_shared_keystrokes(["."]).await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await; -// cx.simulate_shared_keystrokes(["1", "."]).await; -// deterministic.run_until_parked(); -// cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await; -// } -// } +#[cfg(test)] +mod test { + use crate::{ + state::Mode, + test::{NeovimBackedTestContext, VimTestContext}, + }; + + #[gpui::test] + async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + cx.simulate_keystroke("i"); + assert_eq!(cx.mode(), Mode::Insert); + cx.simulate_keystrokes(["T", "e", "s", "t"]); + cx.assert_editor_state("Testˇ"); + cx.simulate_keystroke("escape"); + assert_eq!(cx.mode(), Mode::Normal); + cx.assert_editor_state("Tesˇt"); + } + + #[gpui::test] + async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["5", "i", "-", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("----ˇ-hello\n").await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["5", "a", "-", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("h----ˇ-ello\n").await; + + cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("---ˇ-h-----ello\n").await; + + cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("----h-----ello--ˇ-\n").await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("hello\noi\noi\noˇi\n").await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("oi\noi\noˇi\nhello\n").await; + } + + #[gpui::test] + async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["3", "i", "-", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("--ˇ-hello\n").await; + cx.simulate_shared_keystrokes(["."]).await; + cx.run_until_parked(); + cx.assert_shared_state("----ˇ--hello\n").await; + cx.simulate_shared_keystrokes(["2", "."]).await; + cx.run_until_parked(); + cx.assert_shared_state("-----ˇ---hello\n").await; + + cx.set_shared_state("ˇhello\n").await; + cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"]) + .await; + cx.run_until_parked(); + cx.assert_shared_state("hello\nkk\nkˇk\n").await; + cx.simulate_shared_keystrokes(["."]).await; + cx.run_until_parked(); + cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await; + cx.simulate_shared_keystrokes(["1", "."]).await; + cx.run_until_parked(); + cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await; + } +} diff --git a/crates/vim2/src/motion.rs b/crates/vim2/src/motion.rs index 4e258bc283d25b74da30daebb6ccfaaa88b27dd4..73ba70d5add11f33d55e001e2637c05bcca0afe5 100644 --- a/crates/vim2/src/motion.rs +++ b/crates/vim2/src/motion.rs @@ -952,147 +952,156 @@ pub(crate) fn next_line_end( end_of_line(map, false, point) } -// #[cfg(test)] -// mod test { - -// use crate::test::NeovimBackedTestContext; -// use indoc::indoc; - -// #[gpui::test] -// async fn test_start_end_of_paragraph(cx: &mut gpui::TestAppContext) { -// let mut cx = NeovimBackedTestContext::new(cx).await; - -// let initial_state = indoc! {r"ˇabc -// def - -// paragraph -// the second - -// third and -// final"}; - -// // goes down once -// cx.set_shared_state(initial_state).await; -// cx.simulate_shared_keystrokes(["}"]).await; -// cx.assert_shared_state(indoc! {r"abc -// def -// ˇ -// paragraph -// the second - -// third and -// final"}) -// .await; - -// // goes up once -// cx.simulate_shared_keystrokes(["{"]).await; -// cx.assert_shared_state(initial_state).await; - -// // goes down twice -// cx.simulate_shared_keystrokes(["2", "}"]).await; -// cx.assert_shared_state(indoc! {r"abc -// def - -// paragraph -// the second -// ˇ - -// third and -// final"}) -// .await; - -// // goes down over multiple blanks -// cx.simulate_shared_keystrokes(["}"]).await; -// cx.assert_shared_state(indoc! {r"abc -// def - -// paragraph -// the second - -// third and -// finaˇl"}) -// .await; - -// // goes up twice -// cx.simulate_shared_keystrokes(["2", "{"]).await; -// cx.assert_shared_state(indoc! {r"abc -// def -// ˇ -// paragraph -// the second - -// third and -// final"}) -// .await -// } - -// #[gpui::test] -// async fn test_matching(cx: &mut gpui::TestAppContext) { -// let mut cx = NeovimBackedTestContext::new(cx).await; - -// cx.set_shared_state(indoc! {r"func ˇ(a string) { -// do(something(with.and_arrays[0, 2])) -// }"}) -// .await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state(indoc! {r"func (a stringˇ) { -// do(something(with.and_arrays[0, 2])) -// }"}) -// .await; - -// // test it works on the last character of the line -// cx.set_shared_state(indoc! {r"func (a string) ˇ{ -// do(something(with.and_arrays[0, 2])) -// }"}) -// .await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state(indoc! {r"func (a string) { -// do(something(with.and_arrays[0, 2])) -// ˇ}"}) -// .await; - -// // test it works on immediate nesting -// cx.set_shared_state("ˇ{()}").await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state("{()ˇ}").await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state("ˇ{()}").await; - -// // test it works on immediate nesting inside braces -// cx.set_shared_state("{\n ˇ{()}\n}").await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state("{\n {()ˇ}\n}").await; - -// // test it jumps to the next paren on a line -// cx.set_shared_state("func ˇboop() {\n}").await; -// cx.simulate_shared_keystrokes(["%"]).await; -// cx.assert_shared_state("func boop(ˇ) {\n}").await; -// } - -// #[gpui::test] -// async fn test_comma_semicolon(cx: &mut gpui::TestAppContext) { -// let mut cx = NeovimBackedTestContext::new(cx).await; - -// cx.set_shared_state("ˇone two three four").await; -// cx.simulate_shared_keystrokes(["f", "o"]).await; -// cx.assert_shared_state("one twˇo three four").await; -// cx.simulate_shared_keystrokes([","]).await; -// cx.assert_shared_state("ˇone two three four").await; -// cx.simulate_shared_keystrokes(["2", ";"]).await; -// cx.assert_shared_state("one two three fˇour").await; -// cx.simulate_shared_keystrokes(["shift-t", "e"]).await; -// cx.assert_shared_state("one two threeˇ four").await; -// cx.simulate_shared_keystrokes(["3", ";"]).await; -// cx.assert_shared_state("oneˇ two three four").await; -// cx.simulate_shared_keystrokes([","]).await; -// cx.assert_shared_state("one two thˇree four").await; -// } - -// #[gpui::test] -// async fn test_next_line_start(cx: &mut gpui::TestAppContext) { -// let mut cx = NeovimBackedTestContext::new(cx).await; -// cx.set_shared_state("ˇone\n two\nthree").await; -// cx.simulate_shared_keystrokes(["enter"]).await; -// cx.assert_shared_state("one\n ˇtwo\nthree").await; -// } -// } +#[cfg(test)] +mod test { + + use crate::test::NeovimBackedTestContext; + use indoc::indoc; + + #[gpui::test] + async fn test_start_end_of_paragraph(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + let initial_state = indoc! {r"ˇabc + def + + paragraph + the second + + + + third and + final"}; + + // goes down once + cx.set_shared_state(initial_state).await; + cx.simulate_shared_keystrokes(["}"]).await; + cx.assert_shared_state(indoc! {r"abc + def + ˇ + paragraph + the second + + + + third and + final"}) + .await; + + // goes up once + cx.simulate_shared_keystrokes(["{"]).await; + cx.assert_shared_state(initial_state).await; + + // goes down twice + cx.simulate_shared_keystrokes(["2", "}"]).await; + cx.assert_shared_state(indoc! {r"abc + def + + paragraph + the second + ˇ + + + third and + final"}) + .await; + + // goes down over multiple blanks + cx.simulate_shared_keystrokes(["}"]).await; + cx.assert_shared_state(indoc! {r"abc + def + + paragraph + the second + + + + third and + finaˇl"}) + .await; + + // goes up twice + cx.simulate_shared_keystrokes(["2", "{"]).await; + cx.assert_shared_state(indoc! {r"abc + def + ˇ + paragraph + the second + + + + third and + final"}) + .await + } + + #[gpui::test] + async fn test_matching(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state(indoc! {r"func ˇ(a string) { + do(something(with.and_arrays[0, 2])) + }"}) + .await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state(indoc! {r"func (a stringˇ) { + do(something(with.and_arrays[0, 2])) + }"}) + .await; + + // test it works on the last character of the line + cx.set_shared_state(indoc! {r"func (a string) ˇ{ + do(something(with.and_arrays[0, 2])) + }"}) + .await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state(indoc! {r"func (a string) { + do(something(with.and_arrays[0, 2])) + ˇ}"}) + .await; + + // test it works on immediate nesting + cx.set_shared_state("ˇ{()}").await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state("{()ˇ}").await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state("ˇ{()}").await; + + // test it works on immediate nesting inside braces + cx.set_shared_state("{\n ˇ{()}\n}").await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state("{\n {()ˇ}\n}").await; + + // test it jumps to the next paren on a line + cx.set_shared_state("func ˇboop() {\n}").await; + cx.simulate_shared_keystrokes(["%"]).await; + cx.assert_shared_state("func boop(ˇ) {\n}").await; + } + + #[gpui::test] + async fn test_comma_semicolon(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state("ˇone two three four").await; + cx.simulate_shared_keystrokes(["f", "o"]).await; + cx.assert_shared_state("one twˇo three four").await; + cx.simulate_shared_keystrokes([","]).await; + cx.assert_shared_state("ˇone two three four").await; + cx.simulate_shared_keystrokes(["2", ";"]).await; + cx.assert_shared_state("one two three fˇour").await; + cx.simulate_shared_keystrokes(["shift-t", "e"]).await; + cx.assert_shared_state("one two threeˇ four").await; + cx.simulate_shared_keystrokes(["3", ";"]).await; + cx.assert_shared_state("oneˇ two three four").await; + cx.simulate_shared_keystrokes([","]).await; + cx.assert_shared_state("one two thˇree four").await; + } + + #[gpui::test] + async fn test_next_line_start(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + cx.set_shared_state("ˇone\n two\nthree").await; + cx.simulate_shared_keystrokes(["enter"]).await; + cx.assert_shared_state("one\n ˇtwo\nthree").await; + } +} diff --git a/crates/vim2/src/normal/case.rs b/crates/vim2/src/normal/case.rs index 6f321faa7fdfd8296db406d84d6dae58b15e26d4..22d09f8359f52060a7435cef321ca0c7f71bd37c 100644 --- a/crates/vim2/src/normal/case.rs +++ b/crates/vim2/src/normal/case.rs @@ -74,43 +74,43 @@ pub fn change_case(_: &mut Workspace, _: &ChangeCase, cx: &mut ViewContext