cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

main
012 / internal / sheet / functions_test.go
functions_test.go 2.0K
1package sheet23import (4 "math"5 "testing"6)78// fixture is a small sheet the function tests read from.9//10// A B C D E F G H I11// 1 10 north 1 Apple 1.5 9/26/2026 1 -1000 3012// 2 20 south 2 Banana 2.5 2026-01-31 2 300 2013// 3 30 north 3 Cherry 3.5 14:30 2 400 1014// 4 apple east 4 date x 2/29/2024 3 50015// 5 north 5 Apple 9 12/31/2025 516// 6 TRUE "" 617// 7 -5 Ab*d 718func fixture(t *testing.T) *Sheet {19 t.Helper()20 fixedNow(t)21 s := New()22 for a, in := range map[string]string{23 "A1": "10", "A2": "20", "A3": "30", "A4": "apple", "A6": "TRUE", "A7": "-5",24 "B1": "north", "B2": "south", "B3": "north", "B4": "east", "B5": "north", "B6": `=""`, "B7": "Ab*d",25 "C1": "1", "C2": "2", "C3": "3", "C4": "4", "C5": "5", "C6": "6", "C7": "7",26 "D1": "Apple", "D2": "Banana", "D3": "Cherry", "D4": "date", "D5": "Apple",27 "E1": "1.5", "E2": "2.5", "E3": "3.5", "E4": "x", "E5": "9",28 "F1": "9/26/2026", "F2": "2026-01-31", "F3": "14:30", "F4": "2/29/2024", "F5": "12/31/2025",29 "G1": "1", "G2": "2", "G3": "2", "G4": "3",30 "H1": "-1000", "H2": "300", "H3": "400", "H4": "500",31 "I1": "30", "I2": "20", "I3": "10",32 } {33 if err := s.Set(at(a), in); err != nil {34 t.Fatalf("%s: %v", a, err)35 }36 }37 return s38}3940type fnCase struct {41 in string42 want Value43}4445// checkFormulas evaluates each formula in Z1 and compares, allowing46// rounding differences in the 12th significant digit.47func checkFormulas(t *testing.T, s *Sheet, tests []fnCase) {48 t.Helper()49 for _, tt := range tests {50 if err := s.Set(at("Z1"), tt.in); err != nil {51 t.Errorf("Set(%q): %v", tt.in, err)52 continue53 }54 got := s.Value(at("Z1"))55 if got.Kind == Number && tt.want.Kind == Number {56 if d := math.Abs(got.Num - tt.want.Num); d > 1e-9*math.Max(1, math.Abs(tt.want.Num)) {57 t.Errorf("%s = %v, want %v", tt.in, got.Num, tt.want.Num)58 }59 continue60 }61 if got != tt.want {62 t.Errorf("%s = %+v, want %+v", tt.in, got, tt.want)63 }64 }65}