| 1 | package layout_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/slides/internal/slides/deck" |
| 10 | "amadan.net/oficina/slides/internal/slides/layout" |
| 11 | "amadan.net/oficina/slides/internal/slides/svg" |
| 12 | ) |
| 13 | |
| 14 | // existingDecks are decks written before uploaded fonts existed: the |
| 15 | // golden fixture, and the same deck naming a family this build has |
| 16 | // never heard of (the substitution path, warning and all). |
| 17 | func existingDecks() map[string]*deck.Deck { |
| 18 | named := fixture() |
| 19 | named.Meta.Fonts = &deck.Fonts{Heading: "Nonesuch Grotesk, Georgia, serif", Body: "Liberation Serif, serif"} |
| 20 | // listed uploads a family its stacks never name. |
| 21 | listed := fixture() |
| 22 | listed.Meta.FontFiles = []deck.FontFamily{{Family: "Unused", CSS: "Unused", Faces: []deck.FontFace{ |
| 23 | {Ref: refRegular, Format: "ttf", Weight: [2]int{400, 400}, Pictures: true}}}} |
| 24 | return map[string]*deck.Deck{"fixture": fixture(), "named": named, "listed": listed} |
| 25 | } |
| 26 | |
| 27 | // TestExistingDecksRenderUnchanged holds the pictures of decks that |
| 28 | // upload nothing to the bytes they had before uploaded fonts: every |
| 29 | // fixture slide's SVG against a golden written by the code before the |
| 30 | // change (the paint-list goldens in TestPaintGoldens hold the layout). |
| 31 | func TestExistingDecksRenderUnchanged(t *testing.T) { |
| 32 | for name, d := range existingDecks() { |
| 33 | // A deck whose stacks name no uploaded family reads no font. |
| 34 | r := layout.New(d, layout.Options{Fonts: func(ref string) ([]byte, error) { |
| 35 | t.Errorf("%s read uploaded font %q", name, ref) |
| 36 | return nil, os.ErrNotExist |
| 37 | }}) |
| 38 | for _, g := range goldenNames { |
| 39 | t.Run(name+"/"+g.name, func(t *testing.T) { |
| 40 | p, _, err := r.Layout(g.index) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | // The paint list through the new path is the old one. |
| 45 | old, _, err := layout.Layout(d, g.index) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | a, _ := json.Marshal(p) |
| 50 | b, _ := json.Marshal(old) |
| 51 | if string(a) != string(b) { |
| 52 | t.Errorf("the paint list through layout.New differs from layout.Layout's") |
| 53 | } |
| 54 | got := svg.Encode(p, svg.Options{Title: g.name}) |
| 55 | path := filepath.Join("testdata", "svg", name+"-"+g.name+".golden.svg") |
| 56 | if *update { |
| 57 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | if err := os.WriteFile(path, got, 0o644); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | return |
| 64 | } |
| 65 | want, err := os.ReadFile(path) |
| 66 | if err != nil { |
| 67 | t.Fatalf("read golden: %v", err) |
| 68 | } |
| 69 | if string(got) != string(want) { |
| 70 | t.Errorf("%s's picture changed", path) |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |