From e1077b77801062c90aa68f924413a6324d63809f Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 14 Jan 2026 04:49:41 +0000 Subject: [PATCH] add tests for 'yk' motion (next: implement 'yj' motion and test it) --- fcore/normal-mode/normal-yank.sml | 2 +- temp.txt | 6 +- test/normal-yank.sml | 173 +++++++++++++++++++++++++++++- test/test-utils.sml | 14 +++ todo.md | 1 - 5 files changed, 190 insertions(+), 6 deletions(-) diff --git a/fcore/normal-mode/normal-yank.sml b/fcore/normal-mode/normal-yank.sml index 93815b4..89931c3 100644 --- a/fcore/normal-mode/normal-yank.sml +++ b/fcore/normal-mode/normal-yank.sml @@ -93,7 +93,7 @@ struct val length = endOfLine - lineIdx val buffer = LineGap.goToIdx (endOfLine, buffer) - val str = LineGap.substring (lineIdx, length, buffer) + val str = LineGap.substring (lineIdx + 1, length, buffer) in finish (app, buffer, str) end diff --git a/temp.txt b/temp.txt index bc52a2b..0056b4a 100644 --- a/temp.txt +++ b/temp.txt @@ -1,3 +1,3 @@ -hello world -test line -zzz +hello +world +again diff --git a/test/normal-yank.sml b/test/normal-yank.sml index f8a4255..3d17f71 100644 --- a/test/normal-yank.sml +++ b/test/normal-yank.sml @@ -206,5 +206,176 @@ struct end) ] - val tests = [yhYank, ylYank] + val ykYank = describe "yank motion 'yk'" + [ test "does not yank when cursor is on first line" (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\n" + val originalIdx = 0 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + in + (* assert *) + TestUtils.expectNoYank app + end) + , test + "yanks first two lines \ + \when there are two lines and cursor is on second line" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\n" + val originalIdx = 6 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "hello\nworld\n" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks last two lines when there are three lines in the buffer \ + \and cursor is on third line" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\nagain\n" + val originalIdx = 15 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "world\nagain\n" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks whole buffer when on last line \ + \and count is greater than number of lines" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\nagain\n" + val originalIdx = 15 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "33yk") + + (* assert *) + val expectedString = originalString + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks newline and preceding line when cursor is second line \ + \and second line contains only a newline" + (fn _ => + let + (* arrange *) + val originalString = "hello\n\nagain\n" + val originalIdx = 6 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "hello\n\n" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks just newline and line above when cursor is on third line \ + \and third line contains only a newline" + (fn _ => + let + (* arrange *) + val originalString = + "hello\n\ + \world\n\ + \\n\ + \trello\n\ + \brillo\n" + val originalIdx = 12 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "world\n\n" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks second and third lines when cursor is on \ + \last non-newline character of third line" + (fn _ => + let + (* arrange *) + val originalString = "hello\n\nagain\n" + val originalString = + "hello\n\ + \world\n\ + \trello\n\ + \brillo\n" + val originalIdx = 17 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "world\ntrello\n" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks last two lines when cursor is on last line \ + \and last line only has a newline" + (fn _ => + let + (* arrange *) + val originalString = + "hello\n\ + \world\n\ + \\n" + val originalIdx = String.size originalString - 1 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "yk") + + (* assert *) + val expectedString = "world\n\n" + in + TestUtils.expectYank (app, expectedString) + end) + ] + + val tests = [yhYank, ylYank, ykYank] end diff --git a/test/test-utils.sml b/test/test-utils.sml index 881601c..4650d51 100644 --- a/test/test-utils.sml +++ b/test/test-utils.sml @@ -54,4 +54,18 @@ struct in loop (#msgs app) end + + fun expectNoYank (app: AppType.app_type) = + let + open MailboxType + open DrawMsg + open Railroad + open Railroad.Test + + fun loop (DRAW (YANK _) :: _) = Expect.isTrue false + | loop (hd :: tl) = loop tl + | loop ([]) = Expect.isTrue true + in + loop (#msgs app) + end end diff --git a/todo.md b/todo.md index 48fdb18..e50e805 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,2 @@ # To-do list -- Add tests for 'yk' motion - Implement 'yj' motion and add tests for it