oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3import (
4 "bytes"
5 "os"
6 "testing"
7
8 "amadan.net/oficina/mapa/internal/mapa/locale"
9)
10
11// Gate L4 in other locales (level-up build note 85): every corpus body
12// holding a date link, rendered by Go in each locale below, chips
13// labelled as that locale writes them. editor/parity.test.mjs draws
14// the same bodies with toDOM, labelling each date chip itself with
15// dates.mjs from that locale's island data, and must give this HTML byte
16// for byte. make golden rewrites it.
17
18const goldenLocalesPath = "testdata/golden_locales.json"
19
20// goldenLocales are the locales L4 is held in beyond the default's: a
21// day-first English with a comma, a month-first one, and two others.
22var goldenLocales = []string{"en-IE", "en-US", "de", "fr"}
23
24// datedBodies is every corpus body with a date link in it.
25func datedBodies(t *testing.T) []corpusBody {
26 t.Helper()
27 var out []corpusBody
28 for _, body := range loadCorpus(t) {
29 for _, link := range Links(body.Body) {
30 if IsDateShaped(link.Target) {
31 out = append(out, body)
32 break
33 }
34 }
35 }
36 return out
37}
38
39func goldenInLocales(t *testing.T) []goldenBody {
40 t.Helper()
41 bodies := datedBodies(t)
42 var out []goldenBody
43 for _, code := range goldenLocales {
44 drawn := goldenOf(bodies, corpusResolver{locale.Get(code)})
45 for at := range drawn {
46 drawn[at].Name = code + " " + drawn[at].Name
47 }
48 out = append(out, drawn...)
49 }
50 return out
51}
52
53func TestGoldenLocales(t *testing.T) {
54 drawn := goldenInLocales(t)
55 if len(drawn) < 4*len(goldenLocales) {
56 t.Fatalf("only %d dated bodies across %d locales", len(drawn),
57 len(goldenLocales))
58 }
59 encoded := encodeGolden(drawn)
60 if *update {
61 if err := os.WriteFile(goldenLocalesPath, encoded,
62 0o644); err != nil {
63 t.Fatal(err)
64 }
65 return
66 }
67 committed, err := os.ReadFile(goldenLocalesPath)
68 if err != nil {
69 t.Fatal(err)
70 }
71 if !bytes.Equal(committed, encoded) {
72 t.Fatalf("%s is stale: run make golden and commit it (%s)",
73 goldenLocalesPath, firstDifference(committed, encoded))
74 }
75}
76
77// TestGoldenLocalesDiffer keeps the file honest: it proves something
78// only if the locales really draw the same body differently.
79func TestGoldenLocalesDiffer(t *testing.T) {
80 seen := map[string]bool{}
81 for _, body := range goldenInLocales(t) {
82 seen[body.HTML] = true
83 }
84 bodies := len(datedBodies(t))
85 if len(seen) < bodies*len(goldenLocales)/2 {
86 t.Errorf("%d bodies in %d locales draw only %d ways", bodies,
87 len(goldenLocales), len(seen))
88 }
89}
90