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 / bars_test.go
bars_test.go 2.3K
1package ui23import (4 "strings"5 "testing"67 "src.rest/cat/012/internal/sheet"8)910// mustFormat adds a conditional format written as a line of the file.11func mustFormat(t *testing.T, m *Model, line string) {12 t.Helper()13 f, err := sheet.ParseCondFormat(line)14 if err != nil {15 t.Fatal(err)16 }17 if err := m.sheet.AddCondFormat(f); err != nil {18 t.Fatal(err)19 }20}2122// Data bars draw in eighth blocks as long as their numbers, icons at23// the cells' left, and a dropdown shown as a chip holds its value24// between rounded ends; each reads in characters.25func TestBarsIconsAndChips(t *testing.T) {26 m := newModel()27 for i, v := range []string{"25", "50", "100"} {28 m.sheet.Set(sheet.Addr{Row: i}, v)29 m.sheet.Set(sheet.Addr{Col: 1, Row: i}, v)30 }31 m.sheet.Set(addr("C1"), "Ann")32 mustFormat(t, m, `{"ranges":"A1:A3","dataBar":{"color":"blue","min":{"type":"min"},"max":{"type":"max"}}}`)33 mustFormat(t, m, `{"ranges":"B1:B3","iconSet":{"icons":"arrows","points":[{"type":"percent","value":"33"},{"type":"percent","value":"67"}]}}`)34 mustValidate(t, m, `{"ranges":"C1:C3","criteria":"list","items":["Ann","Bo"],"display":"chip"}`)35 m.cur = addr("E5")36 g := gridLines(m)37 colOf := func(line string, col int) string {38 r := []rune(line)39 from := m.hdrW() + col*sheet.DefaultWidth40 if from >= len(r) {41 return ""42 }43 return string(r[from:min(from+sheet.DefaultWidth, len(r))])44 }45 // A1 is a quarter of the way: two of its nine columns and a quarter.46 if a1 := colOf(g[0], 0); !strings.HasPrefix(a1, "██▎") || !strings.HasSuffix(strings.TrimRight(a1, " "), "25") {47 t.Errorf("A1's bar %q", a1)48 }49 if a3 := colOf(g[2], 0); strings.Count(a3, "█") < 5 || !strings.Contains(a3, "100") {50 t.Errorf("A3's bar %q", a3)51 }52 for row, icon := range []string{"↓", "→", "↑"} {53 if b := colOf(g[row], 1); !strings.HasPrefix(b, " "+icon) {54 t.Errorf("B%d's icon %q", row+1, b)55 }56 }57 if c1 := colOf(g[0], 2); !strings.HasPrefix(c1, " ▐Ann ▾▌") {58 t.Errorf("C1's chip %q", c1)59 }60 if c2 := colOf(g[1], 2); !strings.Contains(c2, "▾") {61 t.Errorf("a blank chip cell shows its ▾: %q", c2)62 }63 // Text under a bar is in reverse video, the bar's color as its64 // background; without color that still reads.65 m.sheet.SetColWidth(0, 4)66 m.sheet.Set(addr("A3"), "999")67 if cells := cellAt(m, addr("A3")); !cells[1].reverse {68 t.Errorf("text under the bar: %+v", cells)69 }70}