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 (
4 "testing"
5 "time"
6)
7
8// mustParse parses src or fails the test. It never puts src into the
9// failure message: formula text is cell content, and this codebase's
10// discipline (see parse.go) is that cell content never appears in a
11// string a person might read off a log or a test report.
12func mustParse(t *testing.T, src string) Node {
13 t.Helper()
14 n, err := Parse(src)
15 if err != nil {
16 t.Fatalf("mustParse: %v", err)
17 }
18 return n
19}
20
21// emptyEnv answers every cell as an untouched, empty cell (Value{}, which
22// is KindEmpty — the zero ValueKind) and pins Now to the zero time. Good
23// enough for tests that never read a cell or a clock.
24type emptyEnv struct{}
25
26func (emptyEnv) CellValue(CellKey) Value { return Value{} }
27func (emptyEnv) Now() time.Time { return time.Time{} }
28
29// mapEnv is a plain map standing in for a real cell store: exactly the
30// kind of test double Env exists to make possible without calc importing
31// store. A missing key reads as an untouched empty cell, same as emptyEnv.
32type mapEnv map[CellKey]Value
33
34func (m mapEnv) CellValue(k CellKey) Value {
35 if v, ok := m[k]; ok {
36 return v
37 }
38 return Value{}
39}
40func (m mapEnv) Now() time.Time { return time.Time{} }
41
42func TestEvalArithmetic(t *testing.T) {
43 v := Eval(mustParse(t, "1+2*3"), emptyEnv{})
44 if v.Kind != KindNum || v.Num != 7 {
45 t.Fatalf("1+2*3 = %v, want 7", v)
46 }
47}
48
49func TestDivideByZeroIsAnExcelError(t *testing.T) {
50 v := Eval(mustParse(t, "1/0"), emptyEnv{})
51 if v.Kind != KindErr || v.Err != ErrDiv0 {
52 t.Fatalf("1/0 = %v, want #DIV/0!", v)
53 }
54}
55
56// Errors propagate rather than being swallowed: =A1+1 where A1 is #REF!
57// must be #REF!, not 1. XLSX round-trips these, so they are values.
58func TestErrorsPropagate(t *testing.T) {
59 env := mapEnv{{1, 1}: {Kind: KindErr, Err: ErrRef}}
60 v := Eval(mustParse(t, "A1+1"), env)
61 if v.Kind != KindErr || v.Err != ErrRef {
62 t.Fatalf("propagation = %v, want #REF!", v)
63 }
64}
65
66// An untouched cell (emptyEnv answers every CellValue with the zero
67// Value) reads as 0 in arithmetic — Excel's own coercion for a blank.
68func TestEmptyCellArithmeticReadsAsZero(t *testing.T) {
69 v := Eval(mustParse(t, "A1+1"), emptyEnv{})
70 if v.Kind != KindNum || v.Num != 1 {
71 t.Fatalf("empty+1 = %v, want 1", v)
72 }
73}
74
75// The same untouched cell concatenates as "", not "0" — this is the
76// behavioural point of KindEmpty existing as its own kind rather than
77// collapsing into KindNum{0}.
78func TestEmptyCellConcatenatesAsEmptyString(t *testing.T) {
79 v := Eval(mustParse(t, `A1&"x"`), emptyEnv{})
80 if v.Kind != KindStr || v.Str != "x" {
81 t.Fatalf(`empty&"x" = %v, want "x"`, v)
82 }
83}
84
85// A blank cell compares equal to 0, to "", and to FALSE — Excel coerces
86// blank to whatever "nothing" means for the other operand's type.
87func TestEmptyCellComparesEqualToEachTypesZeroValue(t *testing.T) {
88 for _, formula := range []string{"A1=0", `A1=""`, "A1=FALSE"} {
89 v := Eval(mustParse(t, formula), emptyEnv{})
90 if v.Kind != KindBool || !v.Bool {
91 t.Errorf("%s = %v, want TRUE", formula, v)
92 }
93 }
94}
95
96// A blank cell is distinguishable from a cell that genuinely contains 0
97// — the property task 8's COUNT vs. COUNTA needs — even though the two
98// compare equal under "=" (see TestEmptyCellComparesEqualToEachTypesZeroValue).
99func TestEmptyCellIsDistinguishableFromZero(t *testing.T) {
100 empty := Eval(mustParse(t, "A1"), emptyEnv{})
101 if empty.Kind != KindEmpty {
102 t.Fatalf("untouched cell Kind = %v, want KindEmpty", empty.Kind)
103 }
104 if empty.Kind == KindNum {
105 t.Fatal("empty cell must not be indistinguishable from KindNum{0}")
106 }
107}
108
109// rangeCells must not silently truncate an over-cap range to some prefix
110// of itself: past maxRangeCells it reports ok=false with no cells at all.
111func TestRangeCellsCapsAtMaxRangeCells(t *testing.T) {
112 atCap := &Range{From: &Ref{RowPos: 0, ColPos: 0}, To: &Ref{RowPos: maxRangeCells - 1, ColPos: 0}}
113 cells, ok := rangeCells(atCap)
114 if !ok || len(cells) != maxRangeCells {
115 t.Fatalf("range at exactly the cap: ok=%v len=%d, want ok=true len=%d", ok, len(cells), maxRangeCells)
116 }
117
118 overCap := &Range{From: &Ref{RowPos: 0, ColPos: 0}, To: &Ref{RowPos: maxRangeCells, ColPos: 0}} // one more row
119 cells, ok = rangeCells(overCap)
120 if ok || cells != nil {
121 t.Fatalf("range one cell over the cap: ok=%v cells=%v, want ok=false cells=nil", ok, cells)
122 }
123}
124
125// A call argument that is a range over the cap resolves to a single
126// #REF! value, not a truncated slice of the range's cells.
127func TestEvalCallArgOverCapRangeIsRefError(t *testing.T) {
128 overCap := &Range{From: &Ref{RowPos: 0, ColPos: 0}, To: &Ref{RowPos: maxRangeCells, ColPos: 0}}
129 got := evalCallArg(overCap, emptyEnv{})
130 if len(got) != 1 || got[0].Kind != KindErr || got[0].Err != ErrRef {
131 t.Fatalf("over-cap range argument = %v, want a single #REF!", got)
132 }
133}
134