add additional tests for 'j' motion

This commit is contained in:
2025-09-20 08:09:35 +01:00
parent ec091b56a3
commit a81d45b3b3
2 changed files with 117 additions and 53 deletions

View File

@@ -144,79 +144,126 @@ struct
] ]
val jMove = describe "move motion 'j'" val jMove = describe "move motion 'j'"
[ test "moves cursur down one column in contiguous string when column = 0" [ test "moves cursur down one column when column = 0" (fn _ =>
let
(* arrange *)
(* "world" at end of string is intentionally misspelled as "qorld"
* since "world" appears twice and it is useful to differentiate them
* *)
val app = TestUtils.init "hello \nworld \ngoodbye \nqorld \n"
(* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *)
val c1 = getChr app1 = #"w"
val c2 = getChr app2 = #"g"
val c3 = getChr app3 = #"q"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test "moves cursur down one column when column = 1" (fn _ =>
let
(* arrange *)
val app = TestUtils.init "hello \nworld \nbye \nfriends \n"
val app = AppWith.idx (app, 1)
(* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *)
val c1 = getChr app1 = #"o"
val c2 = getChr app2 = #"y"
val c3 = getChr app3 = #"r"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test "moves cursur down one column when column = 2" (fn _ =>
let
(* arrange *)
val app = TestUtils.init "hello \nworld \nbye \nfriends \n"
val app = AppWith.idx (app, 2)
(* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *)
val c1 = getChr app1 = #"r"
val c2 = getChr app2 = #"e"
val c3 = getChr app3 = #"i"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test
"moves to last char on below column \
\when cursor is on a column that is greater than \
\the number of columns on the next line"
(fn _ => (fn _ =>
let let
(* arrange *) (* arrange *)
(* "world" at end of string is intentionally misspelled as "qorld" val str =
* since "world" appears twice and it is useful to differentiate them "hello world!\n\
* *) \bye!\n"
val app = TestUtils.init "hello \nworld \ngoodbye \nqorld \n" val app = TestUtils.init str
val app = AppWith.idx (app, 7)
(* act *) (* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j") val app = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *) (* assert *)
val c1 = getChr app1 = #"w" val c1 = getChr app = #"!"
val c2 = getChr app2 = #"g"
val c3 = getChr app3 = #"q"
in in
Expect.isTrue (c1 andalso c2 andalso c3) Expect.isTrue c1
end) end)
, test "moves cursur down one column in contiguous string when column = 1" , test
"goes to first newline when there are two consecutive newlines\
\ahead of the cursor"
(fn _ => (fn _ =>
let let
(* arrange *) (* arrange *)
val app = TestUtils.init "hello \nworld \nbye \nfriends \n" val app = TestUtils.init "hello\n\nworld\n"
val app = AppWith.idx (app, 1)
(* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *)
val c1 = getChr app1 = #"o"
val c2 = getChr app2 = #"y"
val c3 = getChr app3 = #"r"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test "moves cursur down one column in contiguous string when column = 2"
(fn _ =>
let
(* arrange *)
val app = TestUtils.init "hello \nworld \nbye \nfriends \n"
val app = AppWith.idx (app, 2)
(* act *)
val app1 = TestUtils.update (app, CHAR_EVENT #"j")
val app2 = TestUtils.update (app1, CHAR_EVENT #"j")
val app3 = TestUtils.update (app2, CHAR_EVENT #"j")
(* assert *)
val c1 = getChr app1 = #"r"
val c2 = getChr app2 = #"e"
val c3 = getChr app3 = #"i"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test "skips '\\n' when cursor is on non-\\n and is followed by two '\\n's"
(fn _ =>
let
(* arrange *)
val app = TestUtils.init "hello\n\n nworld\n"
(* act *) (* act *)
val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"j") val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"j")
(* assert *) (* assert *)
val isSkipped = cursorIdx = 6 val isSkipped = cursorIdx = 5
in in
Expect.isTrue isSkipped Expect.isTrue isSkipped
end) end)
, test "moves to same column on last line after a count" (fn _ =>
let
(* arrange *)
val str =
"let\n\
\hello\n\
\in\n\
\0\n\
\end\n"
val app = TestUtils.init str
val app1 = AppWith.idx (app, 0)
val app2 = AppWith.idx (app, 1)
val app3 = AppWith.idx (app, 2)
(* act *)
val newApp1 = TestUtils.updateMany (app1, "33j")
val newApp2 = TestUtils.updateMany (app2, "33j")
val newApp3 = TestUtils.updateMany (app3, "33j")
(* assert *)
val c1 = getChr newApp1 = #"e"
val c2 = getChr newApp2 = #"n"
val c3 = getChr newApp3 = #"d"
in
Expect.isTrue (c1 andalso c2 andalso c3)
end)
, test "moves to end of buffer when on last line" (fn _ => , test "moves to end of buffer when on last line" (fn _ =>
let let
(* arrange *) (* arrange *)

View File

@@ -16,4 +16,21 @@ struct
fun update (app, cmd) = fun update (app, cmd) =
AppUpdate.update (app, cmd, Time.now ()) AppUpdate.update (app, cmd, Time.now ())
fun updateMany (app, str) =
let
fun loop (pos, app) =
if pos = String.size str then
app
else
let
val chr = String.sub (str, pos)
val chr = InputMsg.CHAR_EVENT chr
val app = update (app, chr)
in
loop (pos + 1, app)
end
in
loop (0, app)
end
end end