cat / 012
clone

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

main
012 / internal / ui / shortcuts / shortcuts_test.go
shortcuts_test.go 2.3K
1package shortcuts23import (4 "strconv"5 "strings"6 "testing"78 tea "charm.land/bubbletea/v2"9 "github.com/charmbracelet/x/ansi"1011 "src.rest/cat/012/internal/ui/overlay"12 "src.rest/cat/012/internal/ui/theme"13)1415// fakeHost is a screen with forty rows of shortcuts in four groups.16type fakeHost struct {17 th theme.Theme18 w, h int19 closed int20}2122func (f *fakeHost) Theme() *theme.Theme { return &f.th }23func (f *fakeHost) Size() (int, int) { return f.w, f.h }24func (f *fakeHost) Close() { f.closed++ }25func (f *fakeHost) Shortcuts() []Row {26 var rows []Row27 for g := range 4 {28 rows = append(rows, Row{Heading: "Group " + strconv.Itoa(g)})29 for i := range 9 {30 rows = append(rows, Row{Keys: []string{"Ctrl+" + strconv.Itoa(i)}, Action: "Action " + strconv.Itoa(g*10+i) + " does something at some length"})31 }32 }33 return rows34}3536func text(v *View) string {37 return ansi.Strip(strings.Join(v.Layout()[0].Lines, "\n"))38}3940func TestScrollsAndCloses(t *testing.T) {41 h := &fakeHost{th: theme.New(true), w: 80, h: 24}42 v := New(h)43 out := text(v)44 for _, want := range []string{"Keyboard shortcuts", "Group 0", "Action 0", "Ctrl+0", "1-19 of 44"} {45 if !strings.Contains(out, want) {46 t.Errorf("view lacks %q:\n%s", want, out)47 }48 }49 v.Key(tea.KeyPressMsg{Code: tea.KeyDown})50 v.Key(tea.KeyPressMsg{Code: tea.KeyDown})51 if v.Top() != 2 {52 t.Errorf("down twice scrolled to %d", v.Top())53 }54 v.Key(tea.KeyPressMsg{Code: tea.KeyEnd})55 if v.Top() != 44-19 {56 t.Errorf("End scrolled to %d", v.Top())57 }58 v.Mouse(overlay.MouseEvent{Kind: overlay.MouseWheel, Button: tea.MouseWheelUp})59 if v.Top() != 44-19-3 {60 t.Errorf("the wheel scrolled to %d", v.Top())61 }62 v.Mouse(overlay.MouseEvent{Kind: overlay.MousePress, Box: ID})63 if h.closed != 0 {64 t.Error("a click in the box closed it")65 }66 v.Mouse(overlay.MouseEvent{Kind: overlay.MousePress})67 v.Key(tea.KeyPressMsg{Code: tea.KeyEscape})68 if h.closed != 2 {69 t.Errorf("a click outside and Esc closed it %d times", h.closed)70 }71}7273func TestTwoColumnsWhenWide(t *testing.T) {74 h := &fakeHost{th: theme.New(true), w: 200, h: 50}75 lines, w := New(h).Lines()76 if len(lines) >= 40 || w < 40 {77 t.Fatalf("%d lines %d wide", len(lines), w)78 }79 if first := ansi.Strip(lines[0]); !strings.Contains(first, "Group 0") || !strings.Contains(first, "Group 2") {80 t.Errorf("the columns split elsewhere: %q", first)81 }82}