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 / cellformat_test.go
cellformat_test.go 3.0K
1package sheet23import "testing"45// Undo snapshots cells by pointer, so formatting must store new cells6// rather than change stored ones.7func TestFormattingReplacesCells(t *testing.T) {8 s := New()9 s.Set(at("A1"), "5")10 r := NewRect(at("A1"), at("A1"))11 for _, change := range []func(){12 func() { s.SetFormat(r, Preset(FmtCurrency)) },13 func() { s.SetStyle(r, func(st *Style) { st.Bold = true }) },14 func() { s.AdjustDecimals(r, 1) },15 func() { s.ClearFormatting(r) },16 func() { s.EraseRange(r) },17 } {18 before := s.Cell(at("A1"))19 snapshot := *before20 change()21 if before.Format != snapshot.Format || before.Style != snapshot.Style || before.Input != snapshot.Input {22 t.Fatalf("a stored cell changed in place: %+v, was %+v", before, snapshot)23 }24 if s.Cell(at("A1")) == nil {25 s.SetFormat(r, Preset(FmtNumber)) // keep a cell for the next step26 }27 }28}2930// FuzzFormatPattern checks that any pattern and value render without31// panicking, as TEXT() passes user patterns straight through.32func FuzzFormatPattern(f *testing.F) {33 for _, p := range []string{"#,##0.00", "0.0%", "0.00E+00", `"$"#,##0;(#,##0)`, "m/d/yyyy h:mm am/pm", "[h]:mm:ss.00", `\`, `"`, "[", "*", "_"} {34 f.Add(1234.5, p)35 f.Add(-1e300, p)36 }37 f.Fuzz(func(t *testing.T, v float64, pat string) {38 if len(pat) > 200 {39 return40 }41 FormatPattern(v, pat)42 Display(Value{Kind: Number, Num: v}, Format{Kind: FmtCustom, Pattern: pat}, 12)43 })44}4546func TestBatchJoinsFormattingSteps(t *testing.T) {47 s := New()48 r := NewRect(at("A1"), at("B2"))49 s.Set(at("C1"), "=A1")50 s.Set(at("A1"), "5")51 s.Batch(Change{Label: "bold A1:B2", Focus: r}, func() error {52 s.SetStyle(r, func(st *Style) { st.Bold = true })53 s.SetFormat(r, Preset(FmtCurrency))54 return nil55 })56 if c := s.Cell(at("B2")); c == nil || !c.Style.Bold || c.Format.Kind != FmtCurrency {57 t.Errorf("B2 = %+v", c)58 }59 // Formulas following a reformatted cell are recalculated once the60 // batch ends.61 if f := s.DisplayFormat(at("C1")); f.Kind != FmtCurrency {62 t.Errorf("C1 format %+v", f)63 }64}6566// An entry reads as it was typed until it's typed again: a number or67// formula formatted as Plain text afterwards keeps computing, and text68// typed into a Plain text cell stays text in another format or none,69// in the file too, and a formula that doesn't parse stays text without70// making the file unreadable.71func TestPlainTextFormatKeepsEntries(t *testing.T) {72 s := New()73 s.Set(at("A1"), "=1+1")74 s.Set(at("A2"), "5")75 s.SetFormat(rect("A1:A2"), Format{Kind: FmtText})76 s.SetFormat(rect("B1:B3"), Format{Kind: FmtText})77 s.Set(at("B1"), "=1+1")78 s.Set(at("B2"), "5")79 s.Set(at("B3"), "=SUM(")80 s.SetFormat(rect("B1:B1"), Format{Kind: FmtNumber, Decimals: 1})81 s.ClearFormatting(rect("B2:B3"))82 want := map[string]Value{"A1": num(2), "A2": num(5),83 "B1": {Kind: Text, Str: "=1+1"}, "B2": {Kind: Text, Str: "5"}, "B3": {Kind: Text, Str: "=SUM("}}84 for name, sh := range map[string]*Sheet{"edited": s, "reopened": roundTrip(t, s)} {85 for a, v := range want {86 if got := sh.Value(at(a)); got != v {87 t.Errorf("%s: %s = %+v, want %+v", name, a, got, v)88 }89 }90 }91}