Add some tests for history

This commit is contained in:
Kovid Goyal 2022-11-05 08:42:47 +05:30
parent e1ab2383b3
commit 8ad55f7562
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 55 additions and 1 deletions

View File

@ -42,7 +42,9 @@ func (self *Readline) text_after_cursor_pos() string {
}
}
ans := buf.String()
ans = ans[:len(ans)-1]
if ans != "" {
ans = ans[:len(ans)-1]
}
return ans
}
@ -465,6 +467,9 @@ func (self *Readline) yank(repeat_count uint, pop bool) bool {
func (self *Readline) apply_history_text(text string) {
self.lines = utils.Splitlines(text)
if len(self.lines) == 0 {
self.lines = []string{""}
}
}
func (self *Readline) history_first() bool {

View File

@ -373,3 +373,51 @@ func TestEraseChars(t *testing.T) {
rl.erase_between(Position{X: 1}, Position{X: 2, Y: 2})
}, "", "oree")
}
func TestHistory(t *testing.T) {
lp, _ := loop.New()
rl := New(lp, RlInit{Prompt: "$$ "})
add_item := func(x string) {
rl.history.AddItem(x, 0)
}
add_item("a one")
add_item("a two")
add_item("b three")
add_item("b four")
test := func(ac Action, before_cursor, after_cursor string) {
rl.perform_action(ac, 1)
if diff := cmp.Diff(before_cursor, rl.text_upto_cursor_pos()); diff != "" {
t.Fatalf("The text before the cursor was not as expected for action: %#v\n%s", ac, diff)
}
if diff := cmp.Diff(after_cursor, rl.text_after_cursor_pos()); diff != "" {
t.Fatalf("The text after the cursor was not as expected for action: %#v\n%s", ac, diff)
}
}
test(ActionHistoryPrevious, "", "b four")
test(ActionHistoryPrevious, "", "b three")
test(ActionHistoryPrevious, "", "a two")
test(ActionHistoryPrevious, "", "a one")
test(ActionHistoryPrevious, "", "a one")
test(ActionHistoryNext, "", "a two")
test(ActionHistoryNext, "", "b three")
test(ActionHistoryNext, "", "b four")
test(ActionHistoryNext, "", "")
test(ActionHistoryNext, "", "")
test(ActionHistoryPrevious, "", "b four")
test(ActionHistoryPrevious, "", "b three")
test(ActionHistoryNext, "", "b four")
rl.ResetText()
rl.add_text("a")
test(ActionHistoryPrevious, "a", " two")
test(ActionHistoryPrevious, "a", " one")
test(ActionHistoryPrevious, "a", " one")
test(ActionHistoryNext, "a", " two")
test(ActionHistoryNext, "a", "")
test(ActionHistoryNext, "a", "")
}

View File

@ -159,6 +159,7 @@ func NewHistory(path string, max_items int) *History {
func (self *History) FindPrefixMatches(prefix, current_command string) *HistoryMatches {
ans := HistoryMatches{items: make([]HistoryItem, 0, len(self.items)+1), prefix: prefix}
if prefix == "" {
ans.items = ans.items[:len(self.items)]
copy(ans.items, self.items)
} else {
for _, x := range self.items {