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 / internal / sheet / locale_test.go
locale_test.go 5.1K
1package sheet23import (4 "bytes"5 "reflect"6 "strings"7 "testing"89 "src.rest/cat/012/internal/locale"10)1112func mustLocale(t *testing.T, tag string) *locale.Locale {13 t.Helper()14 l, ok := locale.Lookup(tag)15 if !ok {16 t.Fatalf("no locale %s", tag)17 }18 return l19}2021// TestEntriesInLocales types entries in a locale, stores them, and checks22// the value, the format and what editing the cell shows again.23func TestEntriesInLocales(t *testing.T) {24 cases := []struct {25 tag, typed, stored, shown string26 kind FormatKind27 }{28 {"de-DE", "1.234,5", "1,234.5", "1.234,5", FmtNumber},29 {"de-DE", "12,50 €", "$12.50", "12,50 €", FmtCurrency},30 {"de-DE", "26.09.2026", "09/26/2026", "26.09.2026", FmtDate},31 {"de-DE", "=SUM(1;2)", "=SUM(1,2)", "=SUM(1;2)", FmtAuto},32 {"de-DE", "=ROUND(A1*1,19; 2)", "=ROUND(A1*1.19, 2)", "=ROUND(A1*1,19; 2)", FmtAuto},33 {"de-DE", "-A1*0,5", "-A1*0.5", "-A1*0,5", FmtAuto},34 {"de-DE", "Hallo, Welt", "Hallo, Welt", "Hallo, Welt", FmtAuto},35 {"fr-FR", "1.5", "'1.5", "1.5", FmtAuto},36 {"de-DE", "1,234.5", "'1,234.5", "1,234.5", FmtAuto},37 {"de-DE", "'1,5", "'1,5", "'1,5", FmtAuto},38 {"de-DE", "TRUE", "TRUE", "TRUE", FmtAuto},39 {"en-GB", "26/09/2026", "09/26/2026", "26/09/2026", FmtDate},40 {"en-GB", "9/26/2026", "'9/26/2026", "9/26/2026", FmtAuto},41 {"en-US", "1,234.5", "1,234.5", "1,234.5", FmtNumber},42 }43 for _, c := range cases {44 l := mustLocale(t, c.tag)45 stored := CanonicalEntry(c.typed, l)46 if stored != c.stored {47 t.Errorf("%s %q stored as %q, want %q", c.tag, c.typed, stored, c.stored)48 continue49 }50 if shown := LocalEntry(stored, l); shown != c.shown {51 t.Errorf("%s %q shown as %q, want %q", c.tag, stored, shown, c.shown)52 }53 s := New()54 if err := s.Set(at("A1"), stored); err != nil {55 if !strings.Contains(c.typed, "SUM") {56 t.Errorf("%s %q: %v", c.tag, c.typed, err)57 }58 continue59 }60 if k := s.Cell(at("A1")).Format.Kind; k != c.kind {61 t.Errorf("%s %q: format %v, want %v", c.tag, c.typed, k, c.kind)62 }63 }64}6566// TestSetLocale switches a workbook's locale: values stay, the display67// follows, undo puts it back, and the file keeps it.68func TestSetLocale(t *testing.T) {69 s := New()70 for a, in := range map[string]string{"A1": "1234.5", "A2": "$12.50", "A3": "9/26/2026", "A4": "=A1/4"} {71 if err := s.Set(at(a), in); err != nil {72 t.Fatal(err)73 }74 }75 s.SetFormat(NewRect(at("A1"), at("A1")), Preset(FmtNumber))76 shown := func(a string) string {77 text, _ := DisplayIn(s.Cell(at(a)).Value, s.DisplayFormat(at(a)), 20, s.Locale())78 return text79 }80 before := s.Cell(at("A4")).Value81 if !s.Book().SetLocale("de-DE") {82 t.Fatal("de-DE unknown")83 }84 if got := s.Cell(at("A4")).Value; got != before {85 t.Errorf("A4 changed to %v", got)86 }87 for a, want := range map[string]string{"A1": "1.234,50", "A2": "12,50 €", "A3": "26.9.2026", "A4": "308,63"} {88 if got := shown(a); got != want {89 t.Errorf("de-DE %s = %q, want %q", a, got, want)90 }91 }92 var buf bytes.Buffer93 if err := s.Write(&buf); err != nil {94 t.Fatal(err)95 }96 if !strings.Contains(buf.String(), `"locale": "de-DE"`) || !strings.Contains(buf.String(), `"A4": "=A1/4"`) {97 t.Errorf("file:\n%s", buf.String())98 }99 back, err := Read(&buf)100 if err != nil {101 t.Fatal(err)102 }103 if back.Book().LocaleTag() != "de-DE" || back.Cell(at("A4")).Value != before {104 t.Errorf("read back %q, %v", back.Book().LocaleTag(), back.Cell(at("A4")).Value)105 }106 if _, ok := s.Undo(); !ok {107 t.Fatal("nothing to undo")108 }109 if s.Book().LocaleTag() != "" || shown("A1") != "1,234.50" {110 t.Errorf("after undo: %q, %q", s.Book().LocaleTag(), shown("A1"))111 }112 if s.Book().SetLocale("xx-YY") {113 t.Error("xx-YY accepted")114 }115}116117// TestDefaultLocale follows the config's locale unless the file names118// its own.119func TestDefaultLocale(t *testing.T) {120 defer SetDefaultLocale(nil)121 SetDefaultLocale(mustLocale(t, "fr-FR"))122 w := NewBook()123 if w.Locale().Tag != "fr-FR" {124 t.Errorf("new workbook in %s", w.Locale().Tag)125 }126 w.SetLocale("ja-JP")127 if w.Locale().Tag != "ja-JP" {128 t.Errorf("workbook in %s", w.Locale().Tag)129 }130 SetDefaultLocale(nil)131 if NewBook().Locale().Tag != "en-US" {132 t.Error("the default isn't en-US")133 }134}135136// TestFilterInLocale checks the filter's values list shows values as137// the locale does while keeping them in en-US's form, and text138// conditions test the text the locale shows.139func TestFilterInLocale(t *testing.T) {140 s := fruit(t)141 if !s.Book().SetLocale("de-DE") {142 t.Fatal("no de-DE")143 }144 s.Set(at("B2"), "1,234.5")145 s.CreateFilter(Rect{From: at("A1"), To: at("B6")})146 labels := map[string]string{}147 for _, v := range s.FilterValues(1) {148 labels[v.Text] = v.Label149 }150 if labels["1,234.50"] != "1.234,50" || labels["$25"] != "25\u00a0€" {151 t.Errorf("labels %q", labels)152 }153 s.FilterColumn(1, Criteria{Hidden: []string{"$25"}, Cond: Condition{CondContains, "4,5"}})154 if got := shownRows(s); !reflect.DeepEqual(got, []int{1, 2, 7, 8}) {155 t.Errorf("contains 4,5 shows rows %v", got)156 }157 if got := CondArg(CondContains, "4,5", s.Locale()); got != "4,5" {158 t.Errorf("text condition kept as %q", got)159 }160 if got := CondArg(CondGreater, "4,5", s.Locale()); got != "4.5" {161 t.Errorf("number condition kept as %q", got)162 }163}