| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // LU17's template check (§6.8): Fichas renders through ui.Funcs, where a |
| 12 | // locale function called without the reader's localizer silently formats |
| 13 | // in English. So every call of one names L first, and commas is gone. |
| 14 | // |
| 15 | // Mutation, 2026-11-17: {{number .Result.Total}} in index.html failed it. |
| 16 | func TestEveryLocaleCallNamesTheReadersLocalizer(t *testing.T) { |
| 17 | // Each {{action}}, then each call of a locale function inside it, |
| 18 | // wherever it stands: first, after with, if, else if or range, in |
| 19 | // brackets, or after a pipe. |
| 20 | action := regexp.MustCompile(`\{\{-?([^}]*)-?\}\}`) |
| 21 | call := regexp.MustCompile(`(?:^|[\s(|])(number|money|showNumber|showValue|seed|count|ago|dateAgo|stamp|longDate|editField|dateLang|agoLang)\s+(\S+)`) |
| 22 | paths, _ := filepath.Glob(filepath.Join("..", "fichas", "templates", "*.html")) |
| 23 | for _, path := range paths { |
| 24 | body := read(t, path) |
| 25 | if strings.Contains(body, "commas") { |
| 26 | t.Errorf("%s still calls commas", filepath.Base(path)) |
| 27 | } |
| 28 | var calls [][]string |
| 29 | for _, a := range action.FindAllStringSubmatch(body, -1) { |
| 30 | calls = append(calls, call.FindAllStringSubmatch(a[1], -1)...) |
| 31 | } |
| 32 | for _, m := range calls { |
| 33 | fn, arg := m[1], strings.TrimRight(m[2], ")}") |
| 34 | if fn == "editField" { |
| 35 | continue // its localizer is its third argument, checked below |
| 36 | } |
| 37 | if arg != "$.L" && arg != ".L" && arg != "$L" { |
| 38 | t.Errorf("%s: {{%s %s}} does not name the reader's localizer first", filepath.Base(path), fn, arg) |
| 39 | } |
| 40 | } |
| 41 | for _, m := range regexp.MustCompile(`editField \S+ \S+ (\S+?)\)`).FindAllStringSubmatch(body, -1) { |
| 42 | if m[1] != "$L" && m[1] != "$.L" && m[1] != ".L" { |
| 43 | t.Errorf("%s: editField's localizer is %s", filepath.Base(path), m[1]) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | // And no handler writes a date for a person with time.Format: dates |
| 48 | // go through the reader's locale (render.go's ago, stamp, longDate). |
| 49 | gos, _ := filepath.Glob(filepath.Join("..", "fichas", "*.go")) |
| 50 | people := regexp.MustCompile(`\.Format\("2 January|\.Format\("Jan`) |
| 51 | for _, path := range gos { |
| 52 | if strings.HasSuffix(path, "_test.go") { |
| 53 | continue |
| 54 | } |
| 55 | b, err := os.ReadFile(path) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if people.Match(b) { |
| 60 | t.Errorf("%s formats a date for a person outside the reader's locale", filepath.Base(path)) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |