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 / macro_doc_test.go
macro_doc_test.go 2.9K
1package ui23import (4 "os"5 "regexp"6 "strings"7 "testing"89 "src.rest/cat/012/internal/macro"10)1112// docs/reference/macro-api.md documents the scripting API and names command ids; these13// tests keep it honest.1415func readMacroDoc(t *testing.T) string {16 t.Helper()17 data, err := os.ReadFile("../../docs/reference/macro-api.md")18 if err != nil {19 t.Fatal(err)20 }21 return string(data)22}2324// TestMacroDocCommandIDs checks every command id the doc names exists and25// can run in a script.26func TestMacroDocCommandIDs(t *testing.T) {27 doc := readMacroDoc(t)28 start := strings.Index(doc, "| Id | Command |")29 end := strings.Index(doc[start:], "\n\n")30 ids := regexp.MustCompile("`([a-z_.0-9]+)`").FindAllStringSubmatch(doc[start:start+end], -1)31 if len(ids) < 20 {32 t.Fatalf("found %d ids", len(ids))33 }34 for _, id := range ids {35 if c, ok := commands[id[1]]; !ok || c.macro == macroNever {36 t.Errorf("docs/reference/macro-api.md names %q, which scripts can't run", id[1])37 }38 }39}4041// TestMacroDocFunctions checks the doc covers every function scripts have.42func TestMacroDocFunctions(t *testing.T) {43 doc := readMacroDoc(t)44 for _, name := range macro.Functions() {45 if !strings.Contains(doc, "`"+name+"(") {46 t.Errorf("docs/reference/macro-api.md doesn't document %s", name)47 }48 }49}5051// TestMacroDocDialogs checks the doc gives the answer of every command52// that takes a dialog's choices, and runs its example of them.53func TestMacroDocDialogs(t *testing.T) {54 doc := readMacroDoc(t)55 start := strings.Index(doc, "## Dialogs")56 if start < 0 {57 t.Fatal("no Dialogs section")58 }59 section := doc[start:]60 section = section[:strings.Index(section, "\n## ")]61 for id, c := range commands {62 if c.answer != nil && !strings.Contains(section, "| `"+id+"`") && !strings.Contains(section, ", `"+id+"`") {63 t.Errorf("docs/reference/macro-api.md doesn't give the answer of %s", id)64 }65 }66 from := strings.Index(section, "```python\n") + len("```python\n")67 src := section[from : from+strings.Index(section[from:], "```")]68 m := wideSales()69 script(t, m, "run(\"data.filter\")\nrun(\"insert.chart\", answer={})\n"+src)70 if m.note != "Ran S" {71 t.Fatalf("the example: %q", m.warn)72 }73}7475// TestMacroDocExample runs the example script from the doc.76func TestMacroDocExample(t *testing.T) {77 doc := readMacroDoc(t)78 start := strings.Index(doc, "```python\n")79 end := strings.Index(doc[start:], "```\n\n")80 src := doc[start+len("```python\n") : start+end]81 m := newModel()82 press(t, m, "<right>", "<down>", "1", "<tab>", "x", "<tab>", "2", "<enter>", "3", "<tab>", "y", "<tab>", "4", "<enter>")83 press(t, m, "<up>", "<up>", "<shift+down>", "<shift+right>", "<shift+right>")84 script(t, m, src)85 if m.warn != "" {86 t.Fatal(m.warn)87 }88 if got := input(m, "B4") + " " + input(m, "C4") + " " + input(m, "D4"); got != "=SUM(B2:B3) =SUM(D2:D3)" {89 t.Errorf("totals %q", got)90 }91 if c := m.sheet.Cell(addr("B4")); c == nil || !c.Style.Bold || m.cur != addr("B4") {92 t.Errorf("B4 isn't bold and active: %v", m.cur)93 }94}