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 / e2e / macros_test.go
macros_test.go 4.5K
1package e2e23import (4 "os"5 "path/filepath"6 "strings"7 "testing"8)910// recordTotals records a macro with relative references at A1: a label,11// a formula to its right, and bold on both. It's saved as Totals with12// the shortcut Ctrl+Alt+Shift+1.13func recordTotals(s *session) {14 s.keys("<ctrl+k>", "relative references")15 s.waitFor("Record macro with relative references")16 s.keys("<enter>")17 s.waitFor(" REC ")18 s.waitFor("Recording (relative references)")19 s.keys("Total", "<tab>", "=SUM(2,3)", "<enter>")20 s.keys("<up>", "<shift+right>", "<ctrl+b>")21 s.waitFor("Bold on for A1:B1")22 s.keys("<ctrl+k>", "stop and save", "<enter>")23 s.waitFor("Save macro as: Macro 1")24 s.keys("Totals", "<enter>")25 s.waitFor("Shortcut Ctrl+Alt+Shift+ (a digit, or empty for none): 1")26 s.keys("<enter>")27 s.waitFor("Saved macro Totals; Ctrl+Alt+Shift+1 runs it")28}2930func TestMacroRecordAndReplay(t *testing.T) {31 t.Parallel()32 dir := t.TempDir()33 s := start(t, dir, "book.012")34 recordTotals(s)35 if strings.Contains(s.line(0), "REC") {36 t.Errorf("still shows REC: %q", s.line(0))37 }3839 // The shortcut replays it from wherever the active cell is.40 s.keys("<f5>", "D5", "<enter>")41 s.waitForBar("D5", "")42 s.keys("<ctrl+alt+shift+1>")43 s.waitFor("Ran Totals")44 s.keys("<right>")45 s.waitForBar("E5", "=SUM(2,3)")4647 // One Ctrl+Z undoes the whole run.48 s.keys("<ctrl+z>")49 s.waitFor("Undid: run macro Totals")50 s.waitForBar("D5", "")5152 // Saved in the file, as a script.53 s.keys("<ctrl+s>")54 s.eventually("saved", func() bool {55 data, err := os.ReadFile(filepath.Join(dir, "book.012"))56 return err == nil && strings.Contains(string(data), `"macros": [`) &&57 strings.Contains(string(data), `{"name": "Totals", "key": "1", "api": 1, "source": "# Recorded with relative references`) &&58 strings.Contains(string(data), `enter(\"=SUM(2,3)\", origin=\"B1\")`)59 })60 s.keys("<ctrl+q>")61 s.waitExit()6263 // Opening the file runs nothing; on this computer the macro runs64 // without asking.65 s = start(t, dir, "book.012")66 s.waitForBar("A1", "Total")67 s.keys("<f5>", "A3", "<enter>")68 s.waitForBar("A3", "")69 s.keys("<ctrl+k>", "totals")70 s.waitFor("Ctrl+Alt+Shift+1")71 s.keys("<enter>")72 s.waitFor("Ran Totals")73 s.waitForBar("A3:B3", "Total")74}7576func TestMacroFromAnotherComputerAsks(t *testing.T) {77 t.Parallel()78 dir := t.TempDir()79 s := start(t, dir, "book.012")80 recordTotals(s)81 s.keys("<ctrl+s>")82 s.eventually("saved", func() bool {83 data, err := os.ReadFile(filepath.Join(dir, "book.012"))84 return err == nil && strings.Contains(string(data), "macroOrigin")85 })86 s.keys("<ctrl+q>")87 s.waitExit()8889 s = startWith(t, options{dir: dir, configDir: t.TempDir()}, "book.012")90 s.waitForBar("A1", "Total")91 s.keys("<f5>", "C3", "<enter>", "<ctrl+alt+shift+1>")92 s.waitFor("Trust this file's macros?")93 s.keys("<esc>")94 s.waitForBar("C3", "")95 s.keys("<ctrl+alt+shift+1>")96 s.waitFor("made on another computer and can change the file")97 s.keys("<enter>")98 s.waitForBar("C3:D3", "Total") // the macro ends selecting the pair99 // Trusted now: no second question.100 s.keys("<down>", "<ctrl+alt+shift+1>")101 s.waitForBar("C4:D4", "Total")102}103104// TestMacroRecordsTheSortBar records the choices made in a dialog and105// replays them.106func TestMacroRecordsTheSortBar(t *testing.T) {107 t.Parallel()108 dir := t.TempDir()109 s := start(t, dir, "book.012")110 s.keys("Month", "<tab>", "Sales", "<enter>", "Jan", "<tab>", "10", "<enter>", "Feb", "<tab>", "30", "<enter>", "Mar", "<tab>", "20", "<enter>")111 s.keys("<ctrl+home>", "<ctrl+k>", "record macro")112 s.waitFor("Record macro")113 s.keys("<enter>")114 s.waitFor("Recording (absolute references)")115 s.keys("<ctrl+k>", "sort range", "<enter>")116 s.waitFor("Sort A2:B4 by A Month A→Z")117 s.keys("<right>", "<space>", "<enter>")118 s.waitFor("Sorted A2:B4 by B Z→A")119 s.waitForLine(gridRow1+1, " 2 Feb 30")120 s.keys("<ctrl+k>", "stop and save", "<enter>")121 s.waitFor("Save macro as: Macro 1")122 s.keys("Sort", "<enter>", "<enter>")123 s.waitFor("Saved macro Sort")124125 // Sorted back by Sales A to Z, then replayed from the palette.126 s.keys("<right>", "<ctrl+k>", "sort range a to z", "<enter>")127 s.waitForLine(gridRow1+1, " 2 Jan 10")128 s.keys("<left>")129 s.keys("<ctrl+k>", "sort", "<enter>")130 s.waitFor("Ran Sort")131 s.waitForLine(gridRow1+1, " 2 Feb 30")132 s.keys("<ctrl+s>")133 s.eventually("saved", func() bool {134 data, err := os.ReadFile(filepath.Join(dir, "book.012"))135 return err == nil && strings.Contains(string(data), `run(\"data.sort_range\", answer={\"by\": [{\"column\": \"B\", \"order\": \"desc\"}], \"header\": True})`)136 })137}