diff options
author | Hubert Hirtz <hubert@hirtzfr.eu> | 2020-08-02 15:59:27 +0200 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtzfr.eu> | 2020-08-02 15:59:27 +0200 |
commit | 22f5b8292d83be4c1bf744cf51bd5e44980cb29b (patch) | |
tree | b3a9c419f68d0bb45a6a73efabd26a9256bfe97e /ui/editor_test.go | |
parent | Show incoming NOTICEs (diff) |
Improve line editing
Correctly support moving around the text and placing glyphs at any
position.
Diffstat (limited to 'ui/editor_test.go')
-rw-r--r-- | ui/editor_test.go | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/ui/editor_test.go b/ui/editor_test.go new file mode 100644 index 0000000..9f4e131 --- /dev/null +++ b/ui/editor_test.go @@ -0,0 +1,107 @@ +package ui + +import "testing" + +var hell editor = editor{ + text: []rune{'h', 'e', 'l', 'l'}, + textWidth: []int{0, 1, 2, 3, 4}, + cursorIdx: 4, + offsetIdx: 0, + width: 5, +} + +func assertEditorEq(t *testing.T, actual, expected editor) { + if len(actual.text) != len(expected.text) { + t.Errorf("expected text len to be %d, got %d\n", len(expected.text), len(actual.text)) + } else { + for i := 0; i < len(actual.text); i++ { + a := actual.text[i] + e := expected.text[i] + + if a != e { + t.Errorf("expected rune #%d to be '%c', got '%c'\n", i, e, a) + } + } + } + + if len(actual.textWidth) != len(expected.textWidth) { + t.Errorf("expected textWidth len to be %d, got %d\n", len(expected.textWidth), len(actual.textWidth)) + } else { + for i := 0; i < len(actual.textWidth); i++ { + a := actual.textWidth[i] + e := expected.textWidth[i] + + if a != e { + t.Errorf("expected width #%d to be %d, got %d\n", i, e, a) + } + } + } + + if actual.cursorIdx != expected.cursorIdx { + t.Errorf("expected cursorIdx to be %d, got %d\n", expected.cursorIdx, actual.cursorIdx) + } + + if actual.offsetIdx != expected.offsetIdx { + t.Errorf("expected offsetIdx to be %d, got %d\n", expected.offsetIdx, actual.offsetIdx) + } + + if actual.width != expected.width { + t.Errorf("expected width to be %d, got %d\n", expected.width, actual.width) + } +} + +func TestOneLetter(t *testing.T) { + e := newEditor(5) + e.PutRune('h') + assertEditorEq(t, e, editor{ + text: []rune{'h'}, + textWidth: []int{0, 1}, + cursorIdx: 1, + offsetIdx: 0, + width: 5, + }) +} + +func TestFourLetters(t *testing.T) { + e := newEditor(5) + e.PutRune('h') + e.PutRune('e') + e.PutRune('l') + e.PutRune('l') + assertEditorEq(t, e, hell) +} + +func TestOneLeft(t *testing.T) { + e := newEditor(5) + e.PutRune('h') + e.PutRune('l') + e.Left() + e.PutRune('e') + e.PutRune('l') + e.Right() + assertEditorEq(t, e, hell) +} + +func TestOneRem(t *testing.T) { + e := newEditor(5) + e.PutRune('h') + e.PutRune('l') + e.RemRune() + e.PutRune('e') + e.PutRune('l') + e.PutRune('l') + assertEditorEq(t, e, hell) +} + +func TestLeftAndRem(t *testing.T) { + e := newEditor(5) + e.PutRune('h') + e.PutRune('l') + e.PutRune('e') + e.Left() + e.RemRune() + e.Right() + e.PutRune('l') + e.PutRune('l') + assertEditorEq(t, e, hell) +} |