From 82c1d48c6f2f1ba14fcab814704a424de1ce249a Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 15 Oct 2025 15:06:04 +0100 Subject: [PATCH] add tests for 'dgg' motion --- fcore/normal-mode/make-normal-delete.sml | 6 +- test/normal-delete.sml | 112 +++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index 2995250..07e0bd0 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -72,7 +72,6 @@ struct finishAfterDeletingBuffer (app, startIdx, buffer, time, initialMsg) end - (* equivalent of vi's 'x' command **) fun removeChr (app: app_type, count, time) = let @@ -727,6 +726,11 @@ struct val initialMsg = Fn.initMsgs (0, cursorIdx, buffer) val buffer = LineGap.delete (0, cursorIdx, buffer) + val buffer = + if #textLength buffer = 0 then + LineGap.fromString "\n" + else + buffer val buffer = LineGap.goToStart buffer val initialMsg = SEARCH (buffer, dfa, time) :: initialMsg diff --git a/test/normal-delete.sml b/test/normal-delete.sml index e7925d1..0c5293b 100644 --- a/test/normal-delete.sml +++ b/test/normal-delete.sml @@ -2690,6 +2690,117 @@ struct end) ] + val dggDelete = describe "delete motion 'dgg'" + [ test "leaves newline behind when deleting from last line" (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 7) + + (* act *) + val {buffer, cursorIdx, ...} = TestUtils.updateMany (app, "dgg") + + (* assert *) + val actualString = LineGap.toString buffer + val expectedString = "\n" + val expectedCursorIdx = 0 + in + Expect.isTrue + (actualString = expectedString andalso cursorIdx = expectedCursorIdx) + end) + , test + "deletes whole line that cursor is currently on, \ + \and all lines preceding cursor" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\nagain\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 7) + + (* act *) + val {buffer, cursorIdx, ...} = TestUtils.updateMany (app, "dgg") + + (* assert *) + val expectedString = "again\n" + val actualString = LineGap.toString buffer + + val expectedCursurIdx = 0 + + val stringIsExpected = expectedString = actualString + val cursorIsExpected = expectedCursurIdx = cursorIdx + in + Expect.isTrue (stringIsExpected andalso cursorIsExpected) + end) + , test + "deletes current line and preceding lines \ + \when current line does not end with a newline" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 7) + + (* act *) + val {buffer, cursorIdx, ...} = TestUtils.updateMany (app, "dgg") + + (* assert *) + val actualString = LineGap.toString buffer + val expectedString = "\n" + val expectedCursorIdx = 0 + in + Expect.isTrue + (actualString = expectedString + andalso cursorIdx = expectedCursorIdx) + end) + , test + "deletes current line and preceding line \ + \when cursor is on a newline preceded by a newline" + (fn _ => + let + (* arrange *) + val originalString = "\n\nhello world\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 1) + + (* act *) + val {buffer, cursorIdx, ...} = TestUtils.updateMany (app, "dgg") + + (* assert *) + val actualString = LineGap.toString buffer + val expectedString = "hello world\n" + val expectedCursorIdx = 0 + in + Expect.isTrue + (actualString = expectedString + andalso cursorIdx = expectedCursorIdx) + end) + , test + "deletes current line and preceding line \ + \when cursor is on a newline, followed by another newline" + (fn _ => + let + (* arrange *) + val originalString = "\n\n\nhello world\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 1) + + (* act *) + val {buffer, cursorIdx, ...} = TestUtils.updateMany (app, "dgg") + + (* assert *) + val actualString = LineGap.toString buffer + val expectedString = "\nhello world\n" + val expectedCursorIdx = 0 + in + Expect.isTrue + (actualString = expectedString + andalso cursorIdx = expectedCursorIdx) + end) + ] + val tests = [ dhDelete , dlDelete @@ -2704,5 +2815,6 @@ struct , dBDelete , dgeDelete , dgEDelete + , dggDelete ] end