gilo/editor/input_test.go

80 lines
1.6 KiB
Go
Raw Normal View History

2021-04-02 12:03:33 -04:00
package editor
2021-04-02 12:48:15 -04:00
import (
"testing"
"timshome.page/gilo/char"
2021-04-12 12:52:20 -04:00
gilo2 "timshome.page/gilo/internal/gilo"
2021-04-02 12:48:15 -04:00
)
2021-04-02 12:03:33 -04:00
type moveCursor struct {
keys []string
withFile bool
2021-04-12 12:52:20 -04:00
cursor *gilo2.Point
2021-04-02 12:03:33 -04:00
}
var cursorTests = []moveCursor{
{[]string{string(char.Esc)}, false, gilo2.DefaultPoint()},
2021-04-12 12:52:20 -04:00
{[]string{keyRight}, true, gilo2.NewPoint(1, 0)},
{[]string{keyDown, keyEnd}, true, gilo2.NewPoint(14, 1)},
{[]string{keyEnd, keyHome}, true, gilo2.DefaultPoint()},
{[]string{keyRight, keyLeft}, true, gilo2.DefaultPoint()},
{[]string{keyDown, keyUp}, true, gilo2.DefaultPoint()},
2021-04-02 14:52:44 -04:00
// {[]string{keyPageUp}, true, gilo.DefaultPoint()},
2021-04-02 12:03:33 -04:00
}
func TestMoveCursor(t *testing.T) {
for _, test := range cursorTests {
2021-04-02 15:36:43 -04:00
e := NewEditor()
2021-04-02 12:03:33 -04:00
if test.withFile {
e.Open("Editor.go")
2021-04-02 12:03:33 -04:00
}
2021-04-02 13:11:54 -04:00
for _, k := range test.keys {
e.moveCursor(k)
2021-04-02 12:03:33 -04:00
}
want := test.cursor
got := e.cursor
2021-04-02 14:52:44 -04:00
if got.X != want.X || got.Y != want.Y {
2021-04-02 12:03:33 -04:00
t.Errorf("Output %v not equal to expected %v for input %q", got, want, test.keys)
}
}
}
2021-04-02 12:48:15 -04:00
type seqTest struct {
2021-04-02 14:52:44 -04:00
input string
2021-04-02 12:48:15 -04:00
expected string
}
var seqTests = []seqTest{
{"OH", keyHome},
{"OF", keyEnd},
{"[A", keyUp},
{"[B", keyDown},
{"[C", keyRight},
{"[D", keyLeft},
{"[H", keyHome},
{"[F", keyEnd},
{"[1~", keyHome},
{"[3~", keyDelete},
{"[4~", keyEnd},
{"[5~", keyPageUp},
{"[6~", keyPageDown},
{"[7~", keyHome},
{"[8~", keyEnd},
{"OQ", string(char.Esc)},
{"XZ", string(char.Esc)},
2021-04-02 12:48:15 -04:00
}
func TestEscToKey(t *testing.T) {
for _, test := range seqTests {
got := escSeqToKey([]rune(test.input))
want := test.expected
if got != want {
t.Errorf("Got %s for input %s, expected %s", got, test.input, want)
}
}
}