Add some tests for yanking

This commit is contained in:
Kovid Goyal 2022-11-01 14:26:54 +05:30
parent f127523ae9
commit c8e8cb5ad5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 59 additions and 0 deletions

View File

@ -3,6 +3,7 @@
package readline package readline
import ( import (
"container/list"
"fmt" "fmt"
"kitty/tools/tui/loop" "kitty/tools/tui/loop"
"testing" "testing"
@ -253,6 +254,60 @@ func TestCursorMovement(t *testing.T) {
} }
func TestYanking(t *testing.T) {
lp, _ := loop.New()
rl := New(lp, RlInit{Prompt: "$$ "})
as_slice := func(l *list.List) []string {
ans := make([]string, 0, l.Len())
for e := l.Front(); e != nil; e = e.Next() {
ans = append(ans, e.Value.(string))
}
return ans
}
assert_items := func(expected ...string) {
if diff := cmp.Diff(expected, as_slice(rl.kill_ring.items)); diff != "" {
t.Fatalf("kill ring items not as expected\n%s", diff)
}
}
assert_text := func(expected string) {
if diff := cmp.Diff(expected, rl.all_text()); diff != "" {
t.Fatalf("text not as expected:\n%s", diff)
}
}
rl.add_text("1 2 3\none two three")
rl.perform_action(ActionKillToStartOfLine, 1)
assert_items("one two three")
rl.perform_action(ActionCursorUp, 1)
rl.perform_action(ActionKillToEndOfLine, 1)
assert_items("1 2 3", "one two three")
rl.perform_action(ActionYank, 1)
assert_text("1 2 3\n")
rl.perform_action(ActionYank, 1)
assert_text("1 2 31 2 3\n")
rl.perform_action(ActionPopYank, 1)
assert_text("1 2 3one two three\n")
rl.perform_action(ActionPopYank, 1)
assert_text("1 2 31 2 3\n")
rl.ResetText()
rl.kill_ring.clear()
rl.add_text("one two three")
rl.perform_action(ActionMoveToStartOfLine, 1)
rl.perform_action(ActionKillNextWord, 1)
assert_items("one")
assert_text(" two three")
rl.perform_action(ActionKillNextWord, 1)
assert_items("one two")
assert_text(" three")
rl.perform_action(ActionCursorRight, 1)
rl.perform_action(ActionKillNextWord, 1)
assert_items("three", "one two")
assert_text(" ")
}
func TestEraseChars(t *testing.T) { func TestEraseChars(t *testing.T) {
dt := test_func(t) dt := test_func(t)

View File

@ -106,6 +106,10 @@ func (self *kill_ring) pop_yank() string {
return self.yank() return self.yank()
} }
func (self *kill_ring) clear() {
self.items = self.items.Init()
}
type Readline struct { type Readline struct {
prompt string prompt string
prompt_len int prompt_len int