| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // TestRendererClonesPerPage: newRenderer parses the layout and the shared |
| 9 | // partials once and clones that tree for each page. Each clone must carry |
| 10 | // its own door and hop (the layout branches on both), its own "layout" |
| 11 | // template, and its own "content", so no page's definitions leak into |
| 12 | // another's through the shared parse. |
| 13 | func TestRendererClonesPerPage(t *testing.T) { |
| 14 | r, err := newRenderer() |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | if len(r.pages) < 2 { |
| 19 | t.Fatalf("%d pages", len(r.pages)) |
| 20 | } |
| 21 | layouts := map[any]string{} |
| 22 | contents := map[string]string{} |
| 23 | for name, page := range r.pages { |
| 24 | probe, err := page.Clone() |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if probe, err = probe.New("zz-probe").Parse("{{door}} {{hop}}"); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | var b strings.Builder |
| 32 | if err := probe.ExecuteTemplate(&b, "zz-probe", nil); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | want := map[bool]string{true: "true", false: "false"} |
| 36 | if got := b.String(); got != want[doorPages[name]]+" "+want[hopPages[name]] { |
| 37 | t.Errorf("%s: door hop = %q, want door=%v hop=%v", name, got, doorPages[name], hopPages[name]) |
| 38 | } |
| 39 | |
| 40 | layout := page.Lookup("layout") |
| 41 | if layout == nil { |
| 42 | t.Fatalf("%s: no layout", name) |
| 43 | } |
| 44 | if other, ok := layouts[layout]; ok { |
| 45 | t.Errorf("%s and %s share one layout template", name, other) |
| 46 | } |
| 47 | layouts[layout] = name |
| 48 | |
| 49 | content := page.Lookup("content") |
| 50 | if content == nil || content.Tree == nil { |
| 51 | t.Fatalf("%s: no content", name) |
| 52 | } |
| 53 | body := content.Tree.Root.String() |
| 54 | if other, ok := contents[body]; ok { |
| 55 | t.Errorf("%s draws %s's content", name, other) |
| 56 | } |
| 57 | contents[body] = name |
| 58 | } |
| 59 | for _, m := range []map[string]bool{doorPages, hopPages} { |
| 60 | for name := range m { |
| 61 | if _, ok := r.pages[name]; !ok { |
| 62 | t.Errorf("%s is listed as a door or hop page but has no template", name) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |