| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "regexp" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // TestEditorLayoutsMatchTheBundle holds the New slide picker's list |
| 12 | // (layoutNames, which the template renders) to the layouts the bundle |
| 13 | // can make (fixtures/slide-layouts.mjs's LAYOUTS), in the same order: a |
| 14 | // name on one side only is a picker entry that makes the wrong slide, or |
| 15 | // a layout nobody can choose. |
| 16 | func TestEditorLayoutsMatchTheBundle(t *testing.T) { |
| 17 | src, err := os.ReadFile("../../fixtures/slide-layouts.mjs") |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | body := string(src) |
| 22 | start := strings.Index(body, "export const LAYOUTS = {") |
| 23 | end := strings.Index(body[start:], "\n}\n") |
| 24 | if start < 0 || end < 0 { |
| 25 | t.Fatal("no LAYOUTS object in fixtures/slide-layouts.mjs") |
| 26 | } |
| 27 | var names []string |
| 28 | for _, m := range regexp.MustCompile(`(?m)^ '?([a-z-]+)'?: \{`).FindAllStringSubmatch(body[start:start+end], -1) { |
| 29 | names = append(names, m[1]) |
| 30 | } |
| 31 | if !slices.Equal(names, layoutNames) { |
| 32 | t.Errorf("the bundle's layouts are %v, the picker's %v", names, layoutNames) |
| 33 | } |
| 34 | for _, n := range layoutNames { |
| 35 | if !strings.Contains(string(mustReadCatalog(t)), "slides.editor.layout."+n+" = ") { |
| 36 | t.Errorf("layout %q has no catalog label", n) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func mustReadCatalog(t *testing.T) []byte { |
| 42 | t.Helper() |
| 43 | b, err := os.ReadFile("locales/en.toml") |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | return b |
| 48 | } |
| 49 | |