add tests for 'yk' motion (next: implement 'yj' motion and test it)

This commit is contained in:
2026-01-14 04:49:41 +00:00
parent 66f1fb0b5e
commit e1077b7780
5 changed files with 190 additions and 6 deletions

View File

@@ -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

View File

@@ -1,3 +1,3 @@
hello world
test line
zzz
hello
world
again

View File

@@ -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

View File

@@ -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

View File

@@ -1,3 +1,2 @@
# To-do list
- Add tests for 'yk' motion
- Implement 'yj' motion and add tests for it