diff --git a/editor/editor.go b/editor/editor.go index 3152204..812acf5 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -75,6 +75,10 @@ func (e *editor) ProcessKeypress() bool { return false + case key.Enter: + + case key.Backspace, key.Ctrl('h'): + case key.Esc: // Modifier keys that return ANSI escape sequences str := parseEscapeSequence() diff --git a/editor/editor_test.go b/editor/editor_test.go index c31196f..2bb2867 100644 --- a/editor/editor_test.go +++ b/editor/editor_test.go @@ -10,24 +10,52 @@ func TestNew(t *testing.T) { } } -//type moveCursor struct { -// key string -// editor *editor -//} -// -//var cursorTests = []moveCursor{ -// {"\x1b", New()}, -//} -// -//func TestMoveCursor(t *testing.T) { -// for _, test := range cursorTests { -// e := New() -// e.moveCursor(test.key) -// want := test.editor -// got := e -// -// if got != want { -// t.Errorf("Output %v not equal to expected %v for input %q", got, want, test.key) -// } -// } -//} +type moveCursor struct { + keys []string + withFile bool + cursor *point +} + +var cursorTests = []moveCursor{ + {[]string{"\x1b"}, false, &point{0,0}}, + {[]string{keyRight}, true, &point{1,0}}, + {[]string{keyEnd}, true, &point{14, 0}}, + {[]string{keyEnd, keyHome}, true, &point{0, 0}}, + {[]string{keyRight, keyLeft}, true, &point{0, 0}}, + {[]string{keyDown, keyUp}, true, &point{0, 0}}, + {[]string{keyPageUp}, true, &point{0, 0}}, +} + +func TestMoveCursor(t *testing.T) { + for _, test := range cursorTests { + e := New() + + if test.withFile { + e.Open("editor.go") + } + + for _, key := range test.keys { + e.moveCursor(key) + } + want := test.cursor + got := e.cursor + + if got.x != want.x || got.y != want.y { + t.Errorf("Output %v not equal to expected %v for input %q", got, want, test.keys) + } + } +} + +func TestInsertChar(t *testing.T) { + e := New() + e.insertChar('q') + + if e.document.rowCount() != 1 { + t.Errorf("A row was not created when the character was inserted") + } + + row := e.document.rows[0] + if row.size() != 1 { + t.Errorf("Failed to add character to row. Row: %v", row) + } +} \ No newline at end of file diff --git a/key/key.go b/key/key.go index a97be46..4456986 100644 --- a/key/key.go +++ b/key/key.go @@ -5,7 +5,9 @@ package key // ---------------------------------------------------------------------------- const ( - Esc = '\x1b' + Backspace = 0x7f + Esc = 0x1b + Enter = '\r' ) // Is this an ASCII character?