oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package layout
2
3import (
4 "math"
5 "os"
6 "path/filepath"
7 "regexp"
8 "slices"
9 "strings"
10 "testing"
11)
12
13// regions_test.go is the structural guard the slice 3 plan asks for:
14// the region table is a reading of internal/slides/static/slides.css,
15// so the stylesheet is parsed here and the two are held to the same
16// set of region names per template.
17//
18// Names, not pixels. A grid's `auto` row is as tall as what is in it
19// and this table has to name a number before anything is measured, so
20// there is no coordinate here the stylesheet could be checked against.
21// What CAN drift silently is a template gaining or losing a region —
22// somebody adds an `aside` to `title` in CSS and the engine keeps
23// putting it where the body goes, on screen and never in print. That
24// is what this catches.
25
26func stylesheet(t *testing.T) string {
27 t.Helper()
28 b, err := os.ReadFile(filepath.Join("..", "static", "slides.css"))
29 if err != nil {
30 t.Fatalf("read slides.css: %v", err)
31 }
32 return string(b)
33}
34
35// areasFor reads every region name a template places, two ways: the
36// areas named in its `grid-template-areas`, and any region given an
37// explicit `grid-area` in a rule scoped to that template (which is how
38// image-full puts its heading over the picture).
39func areasFor(t *testing.T, css, template string) []string {
40 t.Helper()
41 var out []string
42 add := func(name string) {
43 if name != "" && name != "." && !slices.Contains(out, name) {
44 out = append(out, name)
45 }
46 }
47
48 sel := regexp.QuoteMeta(`[slides-slide][data-template="` + template + `"]`)
49 // The template's own rule, and its `grid-template-areas`. The grid
50 // is on the frame inside the section rather than on the section
51 // (slides.css says why), so the selector may carry that child step
52 // — and may not, for a declaration that is still the section's.
53 frame := `(?:\s*>\s*` + regexp.QuoteMeta(`[slides-frame]`) + `)?`
54 own := regexp.MustCompile(sel + frame + `\s*\{([^}]*)\}`)
55 for _, m := range own.FindAllStringSubmatch(css, -1) {
56 areas := regexp.MustCompile(`grid-template-areas:\s*([^;]*);`).FindStringSubmatch(m[1])
57 if areas == nil {
58 continue
59 }
60 for _, row := range regexp.MustCompile(`"([^"]*)"`).FindAllStringSubmatch(areas[1], -1) {
61 for _, name := range strings.Fields(row[1]) {
62 add(name)
63 }
64 }
65 }
66 // A region placed into an area by a rule of its own.
67 scoped := regexp.MustCompile(sel + `\s*\[data-region="([a-z-]+)"\]\s*\{([^}]*)\}`)
68 for _, m := range scoped.FindAllStringSubmatch(css, -1) {
69 if strings.Contains(m[2], "grid-area:") {
70 add(m[1])
71 }
72 }
73 return out
74}
75
76func TestEveryTemplateInTheStylesheetIsInTheTable(t *testing.T) {
77 css := stylesheet(t)
78 for _, tmpl := range Templates() {
79 if !strings.Contains(css, `[data-template="`+tmpl+`"]`) {
80 t.Errorf("slides.css has no rules for the %q template", tmpl)
81 }
82 if _, ok := regions[tmpl]; !ok {
83 t.Errorf("regions has no entry for the %q template", tmpl)
84 }
85 }
86 for tmpl := range regions {
87 if !slices.Contains(Templates(), tmpl) {
88 t.Errorf("regions has an entry for %q, which is not a template", tmpl)
89 }
90 }
91}
92
93// The set of region names per template, both ways round.
94func TestRegionNamesMatchTheStylesheetGrid(t *testing.T) {
95 css := stylesheet(t)
96 for _, tmpl := range Templates() {
97 want := areasFor(t, css, tmpl)
98 if len(want) == 0 {
99 t.Errorf("no grid areas found for %q in slides.css", tmpl)
100 continue
101 }
102 got := regions[tmpl]
103 for _, name := range want {
104 if _, ok := got[name]; !ok {
105 t.Errorf("slides.css puts a %q region on a %s slide; the table has no box for it",
106 name, tmpl)
107 }
108 }
109 for name := range got {
110 if !slices.Contains(want, name) {
111 t.Errorf("the table gives a %s slide a %q region; slides.css places no such area",
112 tmpl, name)
113 }
114 }
115 }
116}
117
118// Every area name a template uses is also a region name the renderer
119// writes: `[slides-region][data-region="X"] { grid-area: X }` is the
120// identity mapping, and this table leans on that.
121func TestAreaNamesAreRegionNames(t *testing.T) {
122 css := stylesheet(t)
123 for _, name := range regionOrder {
124 rule := regexp.MustCompile(regexp.QuoteMeta(`[slides-region][data-region="`+name+`"]`) +
125 `\s*\{\s*grid-area:\s*` + name + `;`)
126 if !rule.MatchString(css) {
127 t.Errorf("slides.css does not map the %q region to the %q area", name, name)
128 }
129 }
130}
131
132// Every box lands inside the frame and is the right way up. A region
133// with a negative width is a table entry somebody typed wrong, and it
134// would show up as a slide with nothing on it rather than as an error.
135func TestEveryRegionBoxIsInsideTheFrame(t *testing.T) {
136 for tmpl, byName := range regions {
137 for name, r := range byName {
138 switch {
139 case r.W <= 0 || r.H <= 0:
140 t.Errorf("%s/%s is %vx%v", tmpl, name, r.W, r.H)
141 case r.X < 0 || r.Y < 0:
142 t.Errorf("%s/%s starts at %v,%v", tmpl, name, r.X, r.Y)
143 case r.Right() > FrameW+0.001 || r.Bottom() > FrameH+0.001:
144 t.Errorf("%s/%s ends at %v,%v, outside the frame", tmpl, name, r.Right(), r.Bottom())
145 }
146 }
147 }
148}
149
150// The numbers the whole package is built on, spelled out once so a
151// change to the stylesheet's scale fails here first.
152//
153// The wanted values are Chromium's, read off a real slide at 1920 (the
154// browser drive's probe, slice 3 task 2): a padded template's h1 is
155// 110.88px and a canvas slide's is 126.72, because a container query
156// unit is a percent of the container's CONTENT box and the section's
157// padding is 120 each side.
158func TestTheScaleIsTheStylesheetAt1920(t *testing.T) {
159 for _, c := range []struct {
160 name string
161 got float64
162 want float64
163 }{
164 {"1cqw with the section's padding", padded.cqw, 16.8},
165 {"1cqw without it", full.cqw, 19.2},
166 {"--slides-t-1 padded", padded.t1(), 110.88},
167 {"--slides-t-1 full", full.t1(), 126.72},
168 {"--slides-t-2 padded", padded.t2(), 84},
169 {"--slides-t-3 padded", padded.t3(), 67.2},
170 {"--slides-body-l padded", padded.bodyL(), 57.12},
171 {"--slides-body-m padded", padded.bodyM(), 47.04},
172 {"--slides-body-s padded", padded.bodyS(), 36.96},
173 {"--slides-code padded", padded.code(), 38.64},
174 {"--slides-gap padded", padded.gap(), 43.68},
175 {"--slides-rule padded", padded.rule(), 8.4},
176 {"--slides-pad", pad, 120},
177 // The frame's own gap and column-gap. They are the same
178 // declarations a descendant reads, because the frame IS a
179 // descendant of the query container — which is the whole
180 // point of it existing.
181 {"the gap between regions", sectionGap, 43.68},
182 {"two-column's column-gap", sectionColGap, 69.888},
183 } {
184 if diff := c.got - c.want; diff > 1e-9 || diff < -1e-9 {
185 t.Errorf("%s is %v, want %v", c.name, c.got, c.want)
186 }
187 }
188}
189
190// image-full and canvas drop the section's padding, so they are drawn
191// at the bigger scale; every other template keeps it.
192func TestTheScaleFollowsTheSectionsPadding(t *testing.T) {
193 for _, c := range []struct {
194 layout, template string
195 want scale
196 }{
197 {"flow", "title", padded},
198 {"flow", "blank", padded},
199 {"flow", "image-full", full},
200 {"canvas", "blank", full},
201 {"canvas", "image-full", full},
202 } {
203 if got := scaleFor(c.layout, c.template); got != c.want {
204 t.Errorf("a %s %s slide is drawn at %v cqw, want %v", c.layout, c.template, got.cqw, c.want.cqw)
205 }
206 }
207}
208
209// A region name no template places falls back to where the body goes,
210// which is what the stylesheet's auto placement does with it.
211func TestAnUnplacedRegionFallsBackToTheBody(t *testing.T) {
212 got := regionFor("title", "aside")
213 want := regions["title"]["body"]
214 if got != want {
215 t.Errorf("an aside on a title slide is %+v, want the body's %+v", got, want)
216 }
217 if regionFor("a template from a later build", "body") != regions["blank"]["body"] {
218 t.Error("an unknown template does not fall back to blank")
219 }
220}
221
222// A `title` slide with one of its two regions empty still centres what
223// it has, as the stylesheet does: `grid-template-rows: auto auto` keeps
224// the empty row, zero high, and the gap beside it, so `align-content:
225// center` centres the one region and that gap as a pair. A heading
226// alone sits half a gap above the middle (measured in Chromium: its
227// box 457 to 579 of 1080 at a 24px gap), not hung from the bottom of
228// the top half where its own row's box puts it.
229func TestATitleSlideWithOneRegionCentresIt(t *testing.T) {
230 const total = 120.0
231 middle := contentY + contentH/2
232 for _, c := range []struct {
233 name string
234 want float64 // the region's top
235 }{
236 {"heading", middle - (total+sectionGap)/2},
237 {"body", middle - (total+sectionGap)/2 + sectionGap},
238 } {
239 rg := &region{name: c.name, box: regionFor("title", c.name), total: total}
240 (&engine{}).place("title", []*region{rg})
241 if math.Abs(rg.y-c.want) > 0.01 {
242 t.Errorf("a title slide with only its %s: the %s's top is %.2f, want %.2f (centred with the empty row's gap)", c.name, c.name, rg.y, c.want)
243 }
244 }
245}
246