Files
barnard/uiterm/textbox_test.go

112 lines
2.9 KiB
Go

package uiterm
import "testing"
// Regression: byte-based cursor movement split UTF-8 input, producing invalid
// text when deleting or inserting beside a non-ASCII character.
func TestTextboxEditsAtRuneBoundaries(t *testing.T) {
textbox := Textbox{Text: "aé", pos: len("aé")}
textbox.uiKeyEvent(KeyArrowLeft)
if textbox.pos != 1 {
t.Fatalf("cursor = %d, want rune boundary 1", textbox.pos)
}
textbox.uiKeyEvent(KeyBackspace)
if textbox.Text != "é" || textbox.pos != 0 {
t.Fatalf("after delete: %q at %d", textbox.Text, textbox.pos)
}
textbox.uiCharacterEvent('ß')
if textbox.Text != "ßé" {
t.Fatalf("insert produced %q", textbox.Text)
}
}
func TestTextboxHistoryNavigatesSubmittedText(t *testing.T) {
t.Parallel()
textbox := Textbox{}
textbox.addHistory("first")
textbox.addHistory("second")
if !textbox.previousHistory() {
t.Fatal("expected previous history item")
}
if textbox.Text != "second" {
t.Fatalf("expected second history item, got %q", textbox.Text)
}
if !textbox.previousHistory() {
t.Fatal("expected older history item")
}
if textbox.Text != "first" {
t.Fatalf("expected first history item, got %q", textbox.Text)
}
if !textbox.nextHistory() {
t.Fatal("expected newer history item")
}
if textbox.Text != "second" {
t.Fatalf("expected second history item after next, got %q", textbox.Text)
}
}
func TestTextboxHistoryNavigatesAltArrowKeys(t *testing.T) {
t.Parallel()
textbox := Textbox{}
textbox.addHistory("first")
textbox.addHistory("second")
if redraw := textbox.handleHistoryKey(KeyAltArrowUp); !redraw {
t.Fatal("expected alt-up to recall history")
}
if textbox.Text != "second" {
t.Fatalf("expected second history item, got %q", textbox.Text)
}
if redraw := textbox.handleHistoryKey(KeyAltArrowDown); !redraw {
t.Fatal("expected alt-down to move forward through history")
}
if textbox.Text != "" {
t.Fatalf("expected alt-down to restore empty draft, got %q", textbox.Text)
}
}
func TestTextboxHistoryRestoresDraft(t *testing.T) {
t.Parallel()
textbox := Textbox{Text: "draft"}
textbox.addHistory("previous")
textbox.setText("draft")
if !textbox.previousHistory() {
t.Fatal("expected previous history item")
}
if textbox.Text != "previous" {
t.Fatalf("expected previous history item, got %q", textbox.Text)
}
if !textbox.nextHistory() {
t.Fatal("expected history next to restore draft")
}
if textbox.Text != "draft" {
t.Fatalf("expected draft to be restored, got %q", textbox.Text)
}
}
func TestTextboxHistorySkipsBlankAndConsecutiveDuplicateEntries(t *testing.T) {
t.Parallel()
textbox := Textbox{}
textbox.addHistory("")
textbox.addHistory(" ")
textbox.addHistory("repeat")
textbox.addHistory("repeat")
if len(textbox.history) != 1 {
t.Fatalf("expected one history item, got %d", len(textbox.history))
}
if textbox.history[0] != "repeat" {
t.Fatalf("expected repeat history item, got %q", textbox.history[0])
}
}