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 / traceview_test.go
traceview_test.go 3.5K
1package ui23import (4 "strings"5 "testing"67 "src.rest/cat/012/internal/sheet"8)910func TestTraceView(t *testing.T) {11 m := traceModel(t)12 m.sheet.Set(addr("A40"), "=B1")13 m.cur = addr("B1")14 press(t, m, "<alt+;>")15 if m.tview == nil {16 t.Fatal("tracing isn't on")17 }18 for a, want := range map[string]string{"A1": "precedent", "A3": "precedent", "C1": "dependent", "C2": "dependent", "A4": "", "D1": ""} {19 got := ""20 switch m.traceRole(addr(a)) {21 case &m.th.Precedent:22 got = "precedent"23 case &m.th.Dependent:24 got = "dependent"25 }26 if got != want {27 t.Errorf("%s: %q, want %q", a, got, want)28 }29 }30 // Off-screen dependents are listed with an arrow toward them.31 if l := line(m, contextLine); !strings.HasPrefix(l, "B1 reads A1, A2:A3; read by C1, C2, A40↓") || !strings.Contains(l, "Alt+' list") {32 t.Errorf("context %q", l)33 }34 // It follows the pointer.35 press(t, m, "<right>")36 if l := line(m, contextLine); !strings.HasPrefix(l, "C1 reads B1") || m.traceRole(addr("B1")) != &m.th.Precedent {37 t.Errorf("after moving: %q", l)38 }39 press(t, m, "<left>", "<left>")40 if l := line(m, contextLine); !strings.HasPrefix(l, "A1 is read by B1") {41 t.Errorf("a number: %q", l)42 }43 press(t, m, "<down>", "<down>", "<down>")44 if l := line(m, contextLine); !strings.Contains(l, "A4 reads no cells and no formula reads it") {45 t.Errorf("nothing: %q", l)46 }47 // Stepping through a trace shows that trace, and Esc comes back.48 m.cur = addr("B1")49 press(t, m, "<alt+.>")50 if m.cur != addr("C1") || !strings.HasPrefix(line(m, contextLine), "3 dependents of B1") {51 t.Errorf("stepping: %v %q", m.cur, line(m, contextLine))52 }53 press(t, m, "<esc>", "<alt+;>")54 if m.tview != nil || m.traceRole(addr("A1")) != nil {55 t.Error("tracing stayed on")56 }57}5859func TestTraceViewEdits(t *testing.T) {60 m := traceModel(t)61 m.cur = addr("A1")62 run(m, m.runCommand("view.trace"))63 m.cur = addr("D1")64 press(t, m, "=A1*3", "<enter>", "<up>")65 if m.traceRole(addr("A1")) != &m.th.Precedent {66 t.Error("a new formula's precedent isn't marked")67 }68 m.cur = addr("A1")69 press(t, m, "<right>", "<left>")70 if m.traceRole(addr("D1")) != &m.th.Dependent {71 t.Error("a new dependent isn't marked")72 }73}7475func TestTraceList(t *testing.T) {76 m := traceModel(t)77 other, _ := m.book().AddSheet("Data", 1)78 other.Set(addr("A1"), "=Sheet1!B1")79 m.sheet.DefineName("Pair", rectOf("A2:A3"))80 m.cur = addr("B1")81 press(t, m, "<alt+'>")82 scr := screen(m)83 for _, want := range []string{"Precedents and dependents of B1", "A1", "Pair", "C2", "Data!A1", "dependent, on Data"} {84 if !strings.Contains(scr, want) {85 t.Errorf("missing %q in\n%s", want, scr)86 }87 }88 press(t, m, "Data", "<enter>")89 if m.sheet != other || m.cur != addr("A1") {90 t.Errorf("went to %s!%v", m.sheet.Name(), m.cur)91 }92 m.cur = addr("B5")93 press(t, m, "<alt+'>")94 if !strings.Contains(line(m, contextLine), "B5 reads no cells") {95 t.Errorf("nothing to list: %q", line(m, contextLine))96 }97}9899func TestTraceViewSpill(t *testing.T) {100 m := newModel()101 m.sheet.Set(addr("A1"), "=SEQUENCE(3)")102 m.sheet.Set(addr("B1"), "=SUM(A2:A3)")103 m.cur = addr("A2")104 run(m, m.runCommand("view.trace"))105 if l := line(m, contextLine); !strings.HasPrefix(l, "A2 reads A1; read by B1") {106 t.Errorf("a spilled cell: %q", l)107 }108 m.cur = addr("A1")109 send(m, teaSize(100, 30))110 if m.traceRole(addr("A3")) != &m.th.Dependent || m.traceRole(addr("B1")) != &m.th.Dependent {111 t.Error("the anchor's spill and what reads it aren't dependents")112 }113 if got := m.sheet.Value(addr("A3")); got.Kind != sheet.Number {114 t.Errorf("A3 = %v", got)115 }116}