go-prompt/emacs_test.go

33 lines
741 B
Go
Raw Normal View History

2017-08-14 16:50:33 +00:00
package prompt
import "testing"
func TestEmacsKeyBindings(t *testing.T) {
buf := NewBuffer()
buf.InsertText("abcde", false, true)
2018-06-21 16:11:58 +00:00
if buf.cursorPosition != len("abcde") {
t.Errorf("Want %d, but got %d", len("abcde"), buf.cursorPosition)
2017-08-14 16:50:33 +00:00
}
// Go to the beginning of the line
applyEmacsKeyBind(buf, ControlA)
2018-06-21 16:11:58 +00:00
if buf.cursorPosition != 0 {
t.Errorf("Want %d, but got %d", 0, buf.cursorPosition)
2017-08-14 16:50:33 +00:00
}
// Go to the end of the line
applyEmacsKeyBind(buf, ControlE)
2018-06-21 16:11:58 +00:00
if buf.cursorPosition != len("abcde") {
t.Errorf("Want %d, but got %d", len("abcde"), buf.cursorPosition)
2017-08-14 16:50:33 +00:00
}
}
func applyEmacsKeyBind(buf *Buffer, key Key) {
for i := range emacsKeyBindings {
kb := emacsKeyBindings[i]
if kb.Key == key {
kb.Fn(buf)
}
}
}