From 8b0031ca713d1a2b53db891217a45e802d775fea Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 7 Jan 2026 06:33:10 +0000 Subject: [PATCH] add tests for 'yh' motion --- fcore/normal-mode/normal-yank.sml | 2 +- shf-tests.mlb | 1 + temp.txt | 2 +- test/normal-yank.sml | 95 +++++++++++++++++++++++++++++++ test/test-utils.sml | 30 ++++++++++ test/test.sml | 14 ++--- todo.md | 3 +- 7 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 test/normal-yank.sml diff --git a/fcore/normal-mode/normal-yank.sml b/fcore/normal-mode/normal-yank.sml index 60851f7..1521e13 100644 --- a/fcore/normal-mode/normal-yank.sml +++ b/fcore/normal-mode/normal-yank.sml @@ -10,7 +10,7 @@ struct val buffer = LineGap.goToIdx (cursorIdx, buffer) val min = Cursor.vi0 (buffer, cursorIdx) - val low = Cursor.viH (buffer, cursorIdx, 1) + val low = Cursor.viH (buffer, cursorIdx, count) val low = Int.max (min, low) val length = cursorIdx - low diff --git a/shf-tests.mlb b/shf-tests.mlb index e6f423b..82ef7eb 100644 --- a/shf-tests.mlb +++ b/shf-tests.mlb @@ -70,5 +70,6 @@ test/regex-tests.sml test/test-utils.sml test/normal-move.sml test/normal-delete.sml +test/normal-yank.sml test/regression.sml test/test.sml diff --git a/temp.txt b/temp.txt index 36fba7d..3b18e51 100644 --- a/temp.txt +++ b/temp.txt @@ -1 +1 @@ -( ( hello ) ) +hello world diff --git a/test/normal-yank.sml b/test/normal-yank.sml new file mode 100644 index 0000000..829c4eb --- /dev/null +++ b/test/normal-yank.sml @@ -0,0 +1,95 @@ +structure NormalYank = +struct + open Railroad + open Railroad.Test + open InputMsg + + val yhYank = describe "yank motion 'yh'" + [ test "yanks empty string when cursor is at index 0" (fn _ => + let + (* arrange *) + val originalString = "hello world\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 0) + + (* act *) + val app = TestUtils.updateMany (app, "yh") + + (* assert *) + val expectedString = "" + in + TestUtils.expectYank (app, expectedString) + end) + , test "yanks empty string when character before cursor is a newline" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 6) + + (* act *) + val app = TestUtils.updateMany (app, "yh") + + (* assert *) + val expectedString = "" + in + TestUtils.expectYank (app, expectedString) + end) + , test "yanks one char to the left when on a non-newline" (fn _ => + let + (* arrange *) + val originalString = "hello world\n" + val app = TestUtils.init originalString + val app = AppWith.idx (app, 5) + + (* act *) + val app = TestUtils.updateMany (app, "yh") + + (* assert *) + val expectedString = "o" + in + TestUtils.expectYank (app, expectedString) + end) + , test "yanks 3 chars when count is 3" (fn _ => + let + (* arrange *) + val originalIdx = 5 + val originalString = "hello world\n" + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "3yh") + + (* assert *) + val expectedString = "llo" + in + TestUtils.expectYank (app, expectedString) + end) + , test + "yanks from cursor position to start column when \ + \count is greater than current column" + (fn _ => + let + (* arrange *) + val originalIdx = 5 + val originalString = "hello world\n" + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val app = TestUtils.updateMany (app, "9yh") + + (* assert *) + val expectedString = "hello" + in + TestUtils.expectYank (app, expectedString) + end) + ] + + + val tests = [yhYank] +end diff --git a/test/test-utils.sml b/test/test-utils.sml index 7e84838..881601c 100644 --- a/test/test-utils.sml +++ b/test/test-utils.sml @@ -24,4 +24,34 @@ struct in loop (0, app) end + + fun expectYank (app: AppType.app_type, expectedString) = + let + open MailboxType + open DrawMsg + open Railroad + open Railroad.Test + + fun loop (hd :: tl) = + (case hd of + DRAW (YANK actualString) => + if actualString = expectedString then + Expect.isTrue (actualString = expectedString) + else + let + val () = print + ("expectedString = [" ^ expectedString ^ "]\n") + val () = print ("actualString = [" ^ actualString ^ "]\n") + val () = print "\n" + in + Expect.isTrue (actualString = expectedString) + end + | _ => loop tl) + | loop ([]) = + let val () = print "no string yanked\n" + in Expect.isTrue false + end + in + loop (#msgs app) + end end diff --git a/test/test.sml b/test/test.sml index 416569a..e4992ba 100644 --- a/test/test.sml +++ b/test/test.sml @@ -5,13 +5,13 @@ struct fun main () = let - val tests = - List.concat - [ NormalMove.tests - , NormalDelete.tests - , Regression.tests - , RegexTests.tests - ] + val tests = List.concat + [ NormalMove.tests + , NormalDelete.tests + , NormalYank.tests + , Regression.tests + , RegexTests.tests + ] val tests = concat tests in runWithConfig [Configuration.PrintPassed false] tests diff --git a/todo.md b/todo.md index dcb8b20..2cf2f67 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ # To-do list -- Implement `yh` motion. - - Implemented. Add tests for it next (based on tests for 'dh' motion). +- Implement `yl` motion and add tests for it.