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 / spill_test.go
spill_test.go 3.3K
1package ui23import (4 "strings"5 "testing"67 "src.rest/cat/012/internal/sheet"8)910// An array formula spills: the spilled cells say where they come from,11// show its formula dimmed, and refuse edits as Sheets' array results do,12// while the formula's own cell and a range holding it stay editable.13func TestSpillInGrid(t *testing.T) {14 m := newModel()15 press(t, m, "=SEQUENCE(3, 2)", "<enter>", "<up>")16 if shows(m, "B3") != "6" {17 t.Fatalf("B3 shows %q", shows(m, "B3"))18 }19 if !strings.Contains(line(m, contextLine), "Spills into A1:B3") {20 t.Errorf("anchor's context line %q", line(m, contextLine))21 }22 press(t, m, "<down>", "<right>")23 if got := line(m, contextLine); !strings.Contains(got, "Spilled from A1") {24 t.Errorf("spilled cell's context line %q", got)25 }26 if got := bar(m); got != "=SEQUENCE(3, 2)" {27 t.Errorf("formula bar %q, want the anchor's formula", got)28 }29 for _, keys := range [][]string{{"x"}, {"<delete>"}, {"<enter>"}, {"<ctrl+d>"}} {30 press(t, m, keys...)31 if m.mode != modeReady || !strings.Contains(line(m, contextLine), "B2 shows part of the array A1 spills") {32 t.Errorf("%v: mode %v, %q", keys, m.mode, line(m, contextLine))33 }34 press(t, m, "<esc>")35 }36 // Formatting a spilled cell keeps its value.37 press(t, m, "<ctrl+b>")38 if c := m.sheet.Cell(addr("B2")); shows(m, "B2") != "4" || c == nil || !c.Style.Bold {39 t.Errorf("B2 after bold: %q %+v", shows(m, "B2"), c)40 }41 // Inserting a row through the spill recomputes it.42 press(t, m, "<ctrl+alt+=>")43 if strings.Contains(m.note, "spills") || shows(m, "B3") != "6" || shows(m, "B4") != "" {44 t.Errorf("after inserting a row: %q, B3 %q, B4 %q", m.note, shows(m, "B3"), shows(m, "B4"))45 }46 // Copying pastes values; clearing the whole spill clears the formula.47 press(t, m, "<ctrl+g>", "A1:B4", "<enter>", "<delete>")48 if shows(m, "A1") != "" || shows(m, "B3") != "" {49 t.Errorf("after clearing the spill: A1 %q B3 %q", shows(m, "A1"), shows(m, "B3"))50 }51}5253// Cells in the way of a spill make the formula #REF!, with Sheets'54// explanation, until they're cleared.55func TestSpillBlockedInGrid(t *testing.T) {56 m := newModel()57 press(t, m, "<down>", "<down>", "x", "<enter>", "<ctrl+home>", "=SEQUENCE(3)", "<enter>", "<up>")58 if shows(m, "A1") != "#REF!" {59 t.Fatalf("A1 shows %q", shows(m, "A1"))60 }61 if got := line(m, contextLine); !strings.Contains(got, "would overwrite data in A3") {62 t.Errorf("context line %q", got)63 }64 press(t, m, "<down>", "<down>", "<delete>")65 if shows(m, "A1") != "1" || shows(m, "A3") != "3" {66 t.Errorf("after clearing A3: A1 %q A3 %q", shows(m, "A1"), shows(m, "A3"))67 }68}6970// A protected range over a spill doesn't make the spill ask: refusing to71// edit a spilled cell comes first, and the formula's own cell asks as any72// protected cell does.73func TestSpillProtected(t *testing.T) {74 m := newModel()75 press(t, m, "=SEQUENCE(3)", "<enter>")76 m.sheet.Protect(sheet.Protection{Range: sheet.Rect{From: addr("A1"), To: addr("A5")}})77 press(t, m, "<down>", "<up>", "x") // into A2, a spilled cell78 if m.mode != modeReady || !strings.Contains(line(m, contextLine), "A2 shows part of the array A1 spills") {79 t.Errorf("typing into a protected spilled cell: mode %v, %q", m.mode, line(m, contextLine))80 }81 press(t, m, "<esc>", "<shift+f2>")82 if !strings.Contains(line(m, contextLine), "A2 shows part of the array A1 spills") {83 t.Errorf("a note on a spilled cell: %q", line(m, contextLine))84 }85}