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 / context_test.go
context_test.go 3.4K
1package ui23import (4 "strings"5 "testing"67 tea "charm.land/bubbletea/v2"89 "src.rest/cat/012/internal/sheet"10)1112func rightClick(m *Model, x, y int) {13 mouseAt(m, tea.MouseClickMsg{X: x, Y: y, Button: tea.MouseRight})14 mouseAt(m, tea.MouseReleaseMsg{X: x, Y: y, Button: tea.MouseRight})15}1617func menuLabels(t *testing.T, m *Model) string {18 t.Helper()19 var got []string20 for _, it := range openMenu(t, m).top().items {21 if it.sep {22 got = append(got, "-")23 } else {24 got = append(got, it.label())25 }26 }27 return strings.Join(got, ",")28}2930// tallModel has room for full-height context menus below the click.31func tallModel() *Model {32 m := newModel()33 m.Update(tea.WindowSizeMsg{Width: 80, Height: 40})34 return m35}3637func TestRightClickCellMenu(t *testing.T) {38 copied := fakeCommand(t, "edit.copy", "Copy", nil)39 m := tallModel()40 press(t, m, "<shift+down>", "<shift+right>") // A1:B241 // Inside the selection, the selection stays.42 rightClick(m, cellX(1), gridTop+1)43 if m.selection().String() != "A1:B2" {44 t.Errorf("right-click inside moved the selection to %s", m.selection())45 }46 if got := menuLabels(t, m); got != "Cut,Copy,Paste,Paste values only,-,Insert row above,Insert row below,Insert column left,Insert column right,-,Delete row,Delete column,-,Clear,-,Insert note,Delete notes,Protect range" {47 t.Errorf("cell menu %s", got)48 }49 o := openMenu(t, m)50 if b := o.Layout()[0]; b.X != cellX(1) || b.Y != gridTop+2 {51 t.Errorf("menu at %d,%d", b.X, b.Y)52 }53 press(t, m, "<down>", "<enter>")54 if *copied != 1 || m.overlay != nil {55 t.Errorf("copy ran %d times", *copied)56 }57 // Outside the selection, the active cell moves there first.58 rightClick(m, cellX(3), gridTop+5)59 if m.cur != addr("D6") || m.hasRange() {60 t.Errorf("right-click outside: cur %v range %v", m.cur, m.hasRange())61 }62 // Right-clicking elsewhere with a menu open moves the menu.63 rightClick(m, cellX(0), gridTop)64 if m.cur != addr("A1") || m.overlay == nil {65 t.Errorf("second right-click: cur %v overlay %T", m.cur, m.overlay)66 }67 press(t, m, "<esc>")68 if m.overlay != nil || m.mode != modeReady {69 t.Error("Esc did not close the context menu")70 }71}7273func TestRightClickHeaders(t *testing.T) {74 m := tallModel()75 rightClick(m, cellX(2), headerLine)76 if r := m.selection(); r.From != addr("C1") || r.To.Row != sheet.MaxRows-1 || r.To.Col != 2 {77 t.Errorf("column header selected %s", r)78 }79 if got := menuLabels(t, m); got != "Cut,Copy,Paste,-,Insert column left,Insert column right,-,Delete column,Clear,-,Resize column,Reset column width,-,Sort sheet A to Z,Sort sheet Z to A,-,Create a filter,Remove filter" {80 t.Errorf("column menu %s", got)81 }82 press(t, m, "r")83 if highlighted(t, m) != "Resize column" {84 t.Errorf("r highlighted %q", highlighted(t, m))85 }86 press(t, m, "<enter>")87 if m.mode != modePrompt || m.prompt.kind != promptWidth {88 t.Errorf("Resize column: mode %v", m.mode)89 }90 press(t, m, "<esc>")91 rightClick(m, 1, gridTop+3)92 if r := m.selection(); r.From != addr("A4") || r.To.Col != sheet.MaxCols-1 {93 t.Errorf("row header selected %s", r)94 }95 if got := menuLabels(t, m); got != "Cut,Copy,Paste,-,Insert row above,Insert row below,-,Delete row,Clear,-,Resize row,Fit rows to data" {96 t.Errorf("row menu %s", got)97 }98}99100func TestShiftF10OpensCellMenu(t *testing.T) {101 m := tallModel()102 press(t, m, "<right>", "<down>", "<shift+f10>")103 b := openMenu(t, m).Layout()[0]104 if b.X != cellX(1)-2 || b.Y != gridTop+2 {105 t.Errorf("menu at %d,%d", b.X, b.Y)106 }107}