Files
Claude e70735b708 Text terminals: menu keys checked, VT52 erasing, sheet keys, long bodies
- Menu takes only an item's key (returned as the item has it), asking
  again for any other without redrawing, as a 3270 does; Enter is still
  "" (none chosen: Back would log off from the top menu).  view.UI's
  Menu doc says so.
- VT52: ED 1 (erase up to the cursor) was sent as ESC J, erasing below;
  it is dropped now, as EL 1 is.  EL 2 erases the whole line from its
  start; ED 3 clears like ED 2.
- A sheet's key line leaves out 3270 PF-only keys instead of writing a
  NUL for them.
- The full-screen editor cuts a body longer than its line limit.
- keyLabel's unreachable branch removed; setTranslator's comment notes
  that Flush settles the old translator (it already did).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-18 22:28:08 +00:00

105 lines
2.8 KiB
Go

package textui
import (
"reflect"
"testing"
)
// typeIn types s into b, CR as Enter.
func typeIn(b *fsBuffer, s string) {
for _, r := range s {
if r == '\r' {
b.Enter()
} else {
b.Insert(r)
}
}
}
// TestBufferWrap: typing wraps at a space, a word too long breaks at the
// width, the cursor going with the text; a body given is wrapped too.
func TestBufferWrap(t *testing.T) {
b := newBuffer(nil, 10, 100)
typeIn(b, "the quick brown fox")
if got := b.Text(); !reflect.DeepEqual(got, []string{"the quick", "brown fox"}) {
t.Errorf("wrapped %q", got)
}
if b.row != 1 || b.col != 9 {
t.Errorf("cursor %d,%d", b.row, b.col)
}
// one long word breaks where it must
b = newBuffer(nil, 5, 100)
typeIn(b, "abcdefgh")
if got := b.Text(); !reflect.DeepEqual(got, []string{"abcde", "fgh"}) {
t.Errorf("long word %q", got)
}
// a body given is wrapped too
b = newBuffer([]string{"one two three"}, 8, 100)
if got := b.Text(); !reflect.DeepEqual(got, []string{"one two", "three"}) {
t.Errorf("body %q", got)
}
}
// TestBufferEditing: Backspace and Delete join lines, KillLine takes one
// out, and the cursor keys go past a line's ends to the next or the one
// before.
func TestBufferEditing(t *testing.T) {
b := newBuffer(nil, 20, 100)
typeIn(b, "hello\rworld")
b.Move(KeyHome, 0)
b.Backspace() // joins
if got := b.Text(); !reflect.DeepEqual(got, []string{"helloworld"}) || b.col != 5 {
t.Errorf("joined %q at %d", got, b.col)
}
b.Insert(' ')
b.Move(KeyEnd, 0)
b.Enter()
typeIn(b, "x")
b.Move(KeyUp, 0)
b.Move(KeyEnd, 0)
b.Delete() // joins the next line on
if got := b.Text(); !reflect.DeepEqual(got, []string{"hello worldx"}) {
t.Errorf("deleted %q", got)
}
b.KillLine()
if got := b.Text(); len(got) != 0 {
t.Errorf("killed %q", got)
}
// moving past line ends
b = newBuffer([]string{"ab", "cd"}, 20, 100)
b.Move(KeyEnd, 0)
b.Move(KeyRight, 0)
if b.row != 1 || b.col != 0 {
t.Errorf("right %d,%d", b.row, b.col)
}
b.Move(KeyLeft, 0)
if b.row != 0 || b.col != 2 {
t.Errorf("left %d,%d", b.row, b.col)
}
}
// TestBufferQuoteAndLimit: quoted lines go in at the cursor; a message has
// no more lines than its limit, by Enter or by quoting.
func TestBufferQuoteAndLimit(t *testing.T) {
b := newBuffer(nil, 20, 4)
if n := b.InsertLines([]string{"> one", "> two"}); n != 2 {
t.Errorf("quoted %d", n)
}
typeIn(b, "mine")
if got := b.Text(); !reflect.DeepEqual(got, []string{"> one", "> two", "mine"}) {
t.Errorf("quoted %q", got)
}
b.Enter()
if b.Enter() {
t.Error("a fifth line")
}
if n := b.InsertLines([]string{"> three"}); n != 0 {
t.Errorf("quoted past the limit: %d", n)
}
// a body longer than the limit is cut to it
b = newBuffer([]string{"1", "2", "3", "4", "5", "6"}, 20, 4)
if got := b.Text(); len(got) != 4 || got[3] != "4" {
t.Errorf("a long body: %q", got)
}
}