internal/web/classes_test.go
124 lines · 3013 bytes
2 symbols in this file
1package web
2
3import (
4 "io/fs"
5 "regexp"
6 "sort"
7 "strings"
8 "testing"
9)
10
11// TestEveryTemplateClassHasARule fails on a class a template uses that
12// no selector in style.css mentions. Template actions are replaced by a
13// marker and any class token carrying one (chip-{{.State}}, l{{.Level}})
14// is skipped; the rules for the concrete values cover those.
15func TestEveryTemplateClassHasARule(t *testing.T) {
16 css := string(StyleCSS)
17 // Every ".name" that appears in a selector position: outside braces.
18 depth := 0
19 var sel strings.Builder
20 for _, r := range css {
21 switch r {
22 case '{':
23 depth++
24 case '}':
25 depth--
26 default:
27 if depth == 0 {
28 sel.WriteRune(r)
29 }
30 }
31 }
32 // Media queries wrap rules one level deeper; strip their headers and
33 // scan again at depth one.
34 depth = 0
35 inMedia := false
36 for i := 0; i < len(css); i++ {
37 if strings.HasPrefix(css[i:], "@media") {
38 inMedia = true
39 }
40 switch css[i] {
41 case '{':
42 depth++
43 if depth == 1 && !inMedia {
44 // ordinary rule; already scanned above
45 }
46 case '}':
47 depth--
48 if depth == 0 {
49 inMedia = false
50 }
51 default:
52 if inMedia && depth == 1 {
53 sel.WriteByte(css[i])
54 }
55 }
56 }
57 classRe := regexp.MustCompile(`\.([a-zA-Z_][a-zA-Z0-9_-]*)`)
58 styled := map[string]bool{}
59 for _, m := range classRe.FindAllStringSubmatch(sel.String(), -1) {
60 styled[m[1]] = true
61 }
62
63 attrRe := regexp.MustCompile(`class="([^"]*)"`)
64 actionRe := regexp.MustCompile(`(?s)\{\{.*?\}\}`)
65 missing := map[string][]string{}
66 entries, err := fs.ReadDir(templateFS, "templates")
67 if err != nil {
68 t.Fatal(err)
69 }
70 for _, e := range entries {
71 src, err := templateFS.ReadFile("templates/" + e.Name())
72 if err != nil {
73 t.Fatal(err)
74 }
75 clean := actionRe.ReplaceAllString(string(src), "\x00")
76 for _, m := range attrRe.FindAllStringSubmatch(clean, -1) {
77 for _, c := range strings.Fields(m[1]) {
78 if strings.Contains(c, "\x00") {
79 prefix, _, _ := strings.Cut(c, "\x00")
80 if prefix != "" {
81 hasPrefix := false
82 for s := range styled {
83 if strings.HasPrefix(s, prefix) {
84 hasPrefix = true
85 break
86 }
87 }
88 if !hasPrefix {
89 missing[prefix+"*"] = append(missing[prefix+"*"], e.Name())
90 }
91 }
92 continue // composed at render time; the concrete values have rules
93 }
94 if !styled[c] {
95 missing[c] = append(missing[c], e.Name())
96 }
97 }
98 }
99 // Build/check states use check-*, never badge-* (which is for signatures).
100 if strings.Contains(string(src), `badge-{{.}}`) {
101 t.Errorf("%s uses badge-{{.}} instead of check-{{.}}", e.Name())
102 }
103 }
104 var names []string
105 for c := range missing {
106 names = append(names, c)
107 }
108 sort.Strings(names)
109 for _, c := range names {
110 t.Errorf("class %q in %s has no rule in style.css", c, strings.Join(uniq(missing[c]), ", "))
111 }
112}
113
114func uniq(in []string) []string {
115 seen := map[string]bool{}
116 var out []string
117 for _, s := range in {
118 if !seen[s] {
119 seen[s] = true
120 out = append(out, s)
121 }
122 }
123 return out
124}