cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / lineedit / area_test.go
area_test.go 2.2K
1package lineedit23import (4 "strings"5 "testing"67 tea "charm.land/bubbletea/v2"8)910func rowsText(buf []rune, rows []Row) []string {11 var out []string12 for _, r := range rows {13 s := string(buf[r.Start:r.End])14 if r.Cont {15 s = strings.Repeat(" ", Indent) + s16 }17 out = append(out, s)18 }19 return out20}2122func TestWrapAtPipes(t *testing.T) {23 buf := []rune("ls | where size > 1kb | sort-by size | first 3\n$x")24 got := rowsText(buf, Wrap(buf, 24))25 want := []string{"ls | where size > 1kb ", " | sort-by size ", " | first 3", "$x"}26 if strings.Join(got, "/") != strings.Join(want, "/") {27 t.Errorf("rows %q", got)28 }29 // Every character is on a row, once.30 long := []rune(strings.Repeat("abcdefghij", 20))31 n := 032 for _, r := range Wrap(long, 30) {33 n += r.End - r.Start34 if w := RowWidth(long, r.Start, r.End); w > 30 {35 t.Errorf("a row %d wide", w)36 }37 }38 if n != len(long) {39 t.Errorf("%d of %d characters on rows", n, len(long))40 }41}4243func key(s string) tea.KeyPressMsg {44 switch s {45 case "up":46 return tea.KeyPressMsg{Code: tea.KeyUp}47 case "down":48 return tea.KeyPressMsg{Code: tea.KeyDown}49 case "home":50 return tea.KeyPressMsg{Code: tea.KeyHome}51 case "end":52 return tea.KeyPressMsg{Code: tea.KeyEnd}53 case "enter":54 return tea.KeyPressMsg{Code: tea.KeyEnter}55 }56 return tea.KeyPressMsg{Code: rune(s[0]), Text: s}57}5859func TestAreaMovesByRows(t *testing.T) {60 var a Area61 a.SetText("ls | where size > 1kb | sort-by size")62 const w = 2463 a.Key(key("home"), w)64 if a.Pos != 22 {65 t.Errorf("Home on the second row: %d", a.Pos)66 }67 a.Key(key("up"), w)68 if row, _ := Caret(Wrap(a.Buf, w), a.Buf, a.Pos); row != 0 {69 t.Errorf("Up: row %d", row)70 }71 a.Key(key("end"), w)72 if a.Pos != 21 {73 t.Errorf("End on a wrapped row stays on it: %d", a.Pos)74 }75 a.Key(key("down"), w)76 a.Key(key("end"), w)77 if a.Pos != len(a.Buf) {78 t.Errorf("End on the last row: %d", a.Pos)79 }80 a.SetText(" $x")81 a.Key(key("enter"), w)82 a.Key(key("y"), w)83 if a.Text() != " $x\n y" {84 t.Errorf("a new line keeps the indent: %q", a.Text())85 }86 if row, col := Caret(Wrap(a.Buf, w), a.Buf, a.Pos); row != 1 || col != 3 {87 t.Errorf("caret %d,%d", row, col)88 }89 a.InsertText("\ta\x01")90 if a.Text() != " $x\n y a" || a.Offset() != len(" $x\n y a") {91 t.Errorf("inserted %q", a.Text())92 }93}