Add some test for vertical movement

This commit is contained in:
Kovid Goyal 2022-10-17 12:50:02 +05:30
parent 00ef9c1955
commit d260d2f480
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 46 additions and 1 deletions

View File

@ -183,7 +183,7 @@ func (self *Readline) move_cursor_vertically(amt int) (ans int) {
break
}
}
target_line_num := (cursor_line_num + amt + len(screen_lines)) % len(screen_lines)
target_line_num := utils.Min(utils.Max(0, cursor_line_num+amt), len(screen_lines)-1)
ans = target_line_num - cursor_line_num
if ans != 0 {
self.move_cursor_to_target_line(screen_lines[cursor_line_num], screen_lines[target_line_num])

View File

@ -176,6 +176,31 @@ func TestCursorMovement(t *testing.T) {
dt("àb", func(rl *Readline) {
right(rl, 1, 1, false)
}, "à", "b")
lp, _ := loop.New()
rl := New(lp, RlInit{Prompt: "$$ "})
rl.screen_width = 10
vert := func(amt int, moved_amt int, text_upto_cursor_pos string, initials ...Position) {
initial := Position{}
if len(initials) > 0 {
initial = initials[0]
}
rl.cursor = initial
actual := rl.move_cursor_vertically(amt)
if actual != moved_amt {
t.Fatalf("Failed to move cursor by %#v for: %#v \nactual != expected: %#v != %#v", amt, rl.AllText(), actual, moved_amt)
}
if diff := cmp.Diff(text_upto_cursor_pos, rl.text_upto_cursor_pos()); diff != "" {
t.Fatalf("Did not get expected screen lines for: %#v and cursor: %+v\n%s", rl.AllText(), initial, diff)
}
}
rl.ResetText()
rl.add_text("1234567xy\nabcd\n123")
vert(-1, -1, "1234567xy\nabc", Position{X: 3, Y: 2})
vert(-2, -2, "1234567xy", Position{X: 3, Y: 2})
vert(-30, -3, "123", Position{X: 3, Y: 2})
}
func TestEraseChars(t *testing.T) {

View File

@ -53,3 +53,23 @@ func StableSortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T
sort.SliceStable(s, func(i, j int) bool { return mem[i] < mem[j] })
return s
}
func Max[T constraints.Ordered](a T, items ...T) (ans T) {
ans = a
for _, q := range items {
if q > ans {
ans = q
}
}
return ans
}
func Min[T constraints.Ordered](a T, items ...T) (ans T) {
ans = a
for _, q := range items {
if q < ans {
ans = q
}
}
return ans
}