oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package calc
2
3import "testing"
4
5func TestRowColumnOfReference(t *testing.T) {
6 cases := map[string]float64{
7 "ROW(C7)": 7,
8 "COLUMN(C7)": 3,
9 "ROW($B$12)": 12,
10 "ROW(B4:D9)": 4,
11 "COLUMN(D2:B9)": 2,
12 }
13 for src, want := range cases {
14 if got := evalNum(t, src, emptyEnv{}); got != want {
15 t.Errorf("%d-char formula = %v, want %v", len(src), got, want)
16 }
17 }
18}
19
20func TestRowColumnBadArguments(t *testing.T) {
21 for _, src := range []string{"ROW(5)", "COLUMN(A1,B1)", "ROW()", "COLUMN()"} {
22 v := Eval(mustParse(t, src), emptyEnv{})
23 if v.Kind != KindErr || v.Err != ErrValue {
24 t.Errorf("%d-char formula = %+v, want #VALUE!", len(src), v)
25 }
26 }
27}
28
29func TestRowColumnOfCallingCell(t *testing.T) {
30 g := NewGraph()
31 d7 := CellKey{RowID: 7, ColID: 4}
32 g.SetFormula(d7, mustParse(t, "ROW()*100+COLUMN()"))
33 if v := g.Value(d7, emptyEnv{}); v.Kind != KindNum || v.Num != 704 {
34 t.Fatalf("D7 = %+v, want 704", v)
35 }
36 // A formula reading D7 sees D7's own position, not the reader's.
37 e1 := CellKey{RowID: 1, ColID: 5}
38 g.SetFormula(e1, mustParse(t, "D7+ROW()"))
39 if v := g.Value(e1, emptyEnv{}); v.Kind != KindNum || v.Num != 705 {
40 t.Fatalf("E1 = %+v, want 705", v)
41 }
42}
43
44func TestInt(t *testing.T) {
45 cases := map[string]float64{"INT(8.9)": 8, "INT(-8.9)": -9, "INT(3)": 3, "INT(\"2.5\")": 2}
46 for src, want := range cases {
47 if got := evalNum(t, src, emptyEnv{}); got != want {
48 t.Errorf("%d-char formula = %v, want %v", len(src), got, want)
49 }
50 }
51}
52
53// runningTotalGrid builds the "€100 a day for 265 days" sheet: B1 daily
54// amount, B2 days, B3 =B1*B2, Day 1..7 across B..H, Week rows from
55// firstRow down, each cell the running total capped at B3.
56func runningTotalGrid(t *testing.T, firstRow, weeks int) (*Graph, mapEnv) {
57 t.Helper()
58 g := NewGraph()
59 inputs := mapEnv{
60 {RowID: 1, ColID: 2}: {Kind: KindNum, Num: 100},
61 {RowID: 2, ColID: 2}: {Kind: KindNum, Num: 265},
62 }
63 g.SetFormula(CellKey{RowID: 3, ColID: 2}, mustParse(t, "B1*B2"))
64 src := "MIN($B$3,$B$1*((ROW()-ROW($B$" + itoa(firstRow) + "))*7+COLUMN()-1))"
65 for w := 0; w < weeks; w++ {
66 for d := 0; d < 7; d++ {
67 g.SetFormula(CellKey{RowID: uint64(firstRow + w), ColID: uint64(2 + d)}, mustParse(t, src))
68 }
69 }
70 return g, inputs
71}
72
73func TestRunningTotalGrid(t *testing.T) {
74 g, inputs := runningTotalGrid(t, 6, 40)
75 check := func(row, col int, want float64) {
76 t.Helper()
77 v := g.Value(CellKey{RowID: uint64(row), ColID: uint64(col)}, inputs)
78 if v.Kind != KindNum || v.Num != want {
79 t.Errorf("R%dC%d = %+v, want %v", row, col, v, want)
80 }
81 }
82 check(6, 2, 100) // Week 1 Day 1
83 check(6, 8, 700) // Week 1 Day 7
84 check(7, 2, 800) // Week 2 Day 1
85 check(43, 7, 26500) // day 265: Week 38 Day 6
86 check(43, 8, 26500) // day 266 capped
87 check(45, 8, 26500)
88
89 // Changing the daily amount recalculates the grid.
90 inputs[CellKey{RowID: 1, ColID: 2}] = Value{Kind: KindNum, Num: 50}
91 g.Recalc(CellKey{RowID: 1, ColID: 2}, inputs)
92 check(6, 8, 350)
93}
94
95// After a row is inserted above the grid, the graph is rebuilt with every
96// formula at its new position (store's buildHandle; references shift the
97// same way). The positional formulas must give the same answers.
98func TestRunningTotalGridAfterRowInsert(t *testing.T) {
99 g, inputs := runningTotalGrid(t, 7, 2)
100 for _, c := range [][3]float64{{7, 2, 100}, {7, 8, 700}, {8, 2, 800}} {
101 v := g.Value(CellKey{RowID: uint64(c[0]), ColID: uint64(c[1])}, inputs)
102 if v.Kind != KindNum || v.Num != c[2] {
103 t.Errorf("R%vC%v = %+v, want %v", c[0], c[1], v, c[2])
104 }
105 }
106}
107