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 / chartaxes_test.go
chartaxes_test.go 4.7K
1package ui23import (4 "strings"5 "testing"67 "github.com/charmbracelet/x/ansi"89 "src.rest/cat/012/internal/sheet"10)1112func TestChartEditorStackingAndTrend(t *testing.T) {13 m := wideModel()14 chartTable(t, m)15 m.runCommand("insert.chart")16 if _, keys := m.overlay.Status(); !strings.Contains(line(m, contextLine), "Not stacked") || !strings.Contains(keys, "stacking") {17 t.Errorf("no stacking chip: %q / %q", line(m, contextLine), keys)18 }19 press(t, m, "k")20 if c := m.sheet.Charts()[0]; c.Stack != sheet.StackNormal || !strings.Contains(line(m, contextLine), "Stacked") {21 t.Errorf("stack %v: %q", c.Stack, line(m, contextLine))22 }23 press(t, m, "k")24 if c := m.sheet.Charts()[0]; c.Stack != sheet.StackPercent || !strings.Contains(screen(m), "100% ┤") {25 t.Errorf("100%% stacking %v:\n%s", c.Stack, screen(m))26 }27 // Scatter has a trend line instead; K does nothing there.28 press(t, m, "6")29 if !strings.Contains(line(m, contextLine), "Trend line") || strings.Contains(line(m, contextLine), "stacked") {30 t.Errorf("scatter bar: %q", line(m, contextLine))31 }32 press(t, m, "e", "k")33 if c := m.sheet.Charts()[0]; c.Type != sheet.ChartScatter || !c.Trend || c.Stack != sheet.StackPercent {34 t.Errorf("after E and K: %+v", c)35 }36 // Esc undoes every change, removing the new chart.37 press(t, m, "<esc>")38 if n := len(m.sheet.Charts()); n != 0 {39 t.Errorf("%d charts after Esc", n)40 }41}4243func TestChartEditorAxes(t *testing.T) {44 m := wideModel()45 chartTable(t, m)46 m.runCommand("insert.chart")47 press(t, m, "a")48 ctx := line(m, contextLine)49 for _, want := range []string{"Min auto", "Max auto", "Log", "Gridlines", "Legend bottom"} {50 if !strings.Contains(ctx, want) {51 t.Errorf("axis bar lacks %q: %q", want, ctx)52 }53 }54 // The minimum is typed as a cell takes a number.55 press(t, m, "n")56 if m.mode != modePrompt {57 t.Fatalf("minimum prompt not open, mode %v", m.mode)58 }59 m.line.Buf, m.line.Pos = []rune("$500"), 460 press(t, m, "<enter>")61 if c := m.sheet.Charts()[0]; !c.HasMin || c.Min != 500 || !strings.Contains(line(m, contextLine), "Min 500") {62 t.Errorf("min %v %v: %q", c.HasMin, c.Min, line(m, contextLine))63 }64 // Not a number: asked again, and Esc leaves the axis as it was.65 press(t, m, "x")66 m.line.Buf, m.line.Pos = []rune("lots"), 467 press(t, m, "<enter>")68 if m.mode != modePrompt || !strings.Contains(line(m, contextLine), "Not a number. Axis maximum") {69 t.Errorf("not asked again: %q", line(m, contextLine))70 }71 press(t, m, "<esc>")72 if c := m.sheet.Charts()[0]; c.HasMax || !strings.Contains(line(m, contextLine), "Max auto") {73 t.Errorf("max %v %v: %q", c.HasMax, c.Max, line(m, contextLine))74 }75 press(t, m, "l", "g", "p")76 c := m.sheet.Charts()[0]77 if !c.Log || !c.NoGrid || c.Legend != sheet.LegendRight {78 t.Errorf("toggles: %+v", c.ChartOptions)79 }80 if !strings.Contains(screen(m), "■ Rent") {81 t.Errorf("right legend missing:\n%s", screen(m))82 }83 press(t, m, "p")84 if c := m.sheet.Charts()[0]; c.Legend != sheet.LegendNone || strings.Contains(screen(m), "■ Rent") {85 t.Errorf("legend none: %v", c.Legend)86 }87 // A blank minimum is automatic again.88 press(t, m, "n")89 m.line.Buf, m.line.Pos = nil, 090 press(t, m, "<enter>")91 if c := m.sheet.Charts()[0]; c.HasMin {92 t.Errorf("min still set: %v", c.Min)93 }94 // Esc goes back to the main bar, keeping the changes.95 press(t, m, "<esc>")96 if _, ok := m.overlay.(*chartEditor); !ok || !strings.Contains(line(m, contextLine), "Column") {97 t.Fatalf("not back on the main bar: %T %q", m.overlay, line(m, contextLine))98 }99 press(t, m, "<enter>")100 if c := m.sheet.Charts()[0]; !c.Log || c.Legend != sheet.LegendNone {101 t.Errorf("changes lost: %+v", c.ChartOptions)102 }103 // Undo takes back the last change.104 press(t, m, "<esc>", "<ctrl+z>")105 if c := m.sheet.Charts()[0]; !c.HasMin {106 t.Errorf("undo: %+v", c.ChartOptions)107 }108}109110func TestChartEditorFitsNarrow(t *testing.T) {111 m := wideModel()112 chartTable(t, m)113 m.runCommand("insert.chart")114 press(t, m, "a")115 m.Update(teaSize(60, 16))116 if ctx := line(m, contextLine); !strings.Contains(ctx, "Legend bottom") {117 t.Errorf("axis bar clipped at 60 columns: %q", ctx)118 }119 if _, keys := m.overlay.Status(); !strings.Contains(keys, "grid") || !strings.Contains(keys, "legend") || ansi.StringWidth(keys) > 60 {120 t.Errorf("axis keys at 60 columns: %q", ansi.Strip(keys))121 }122 press(t, m, "<esc>")123 for _, want := range []string{"◂ Column ▸", "Series in columns"} {124 if ctx := line(m, contextLine); !strings.Contains(ctx, want) {125 t.Errorf("main bar lacks %q at 60 columns: %q", want, ctx)126 }127 }128 // Clicking the one type chip moves on to the next type.129 leftClick(m, 3, contextLine)130 if c := m.sheet.Charts()[0]; c.Type != sheet.ChartBar || !strings.Contains(line(m, contextLine), "◂ Bar ▸") {131 t.Errorf("after a click: %v %q", c.Type, line(m, contextLine))132 }133}