Fix history and add tests

This commit is contained in:
Masashi SHIBATA 2017-10-10 23:58:16 +09:00
parent eca845d01e
commit c292a2f4b4
2 changed files with 66 additions and 1 deletions

View File

@ -14,7 +14,10 @@ func (h *History) Add(input string) {
}
func (h *History) Clear() {
copy(h.tmp, h.histories)
h.tmp = make([]string, len(h.histories))
for i := range h.histories {
h.tmp[i] = h.histories[i]
}
h.tmp = append(h.tmp, "")
h.selected = len(h.tmp) - 1
}

62
history_test.go Normal file
View File

@ -0,0 +1,62 @@
package prompt
import (
"reflect"
"testing"
)
func TestClear(t *testing.T) {
h := NewHistory()
h.Add("foo")
h.Clear()
expected := &History{
histories: []string{"foo"},
tmp: []string{"foo", ""},
selected: 1,
}
if !reflect.DeepEqual(expected, h) {
t.Errorf("Should be %#v, but got %#v", expected, h)
}
}
func TestAdd(t *testing.T) {
h := NewHistory()
h.Add("echo 1")
expected := &History{
histories: []string{"echo 1"},
tmp: []string{"echo 1", ""},
selected: 1,
}
if !reflect.DeepEqual(h, expected) {
t.Errorf("Should be %v, but got %v", expected, h)
}
}
func TestOlder(t *testing.T) {
h := NewHistory()
h.Add("echo 1")
// Prepare buffer
buf := NewBuffer()
buf.InsertText("echo 2", false, true)
// [1 time] Call Older function
buf1, changed := h.Older(buf)
if !changed {
t.Error("Should be changed history but not changed.")
}
if buf1.Text() != "echo 1" {
t.Errorf("Should be %#v, but got %#v", "echo 1", buf1.Text())
}
// [2 times] Call Older function
buf = NewBuffer()
buf.InsertText("echo 1", false, true)
buf2, changed := h.Older(buf)
if changed {
t.Error("Should be not changed history but changed.")
}
if !reflect.DeepEqual("echo 1", buf2.Text()) {
t.Errorf("Should be %#v, but got %#v", "echo 1", buf2.Text())
}
}