| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/fichas/internal/fichas/icons" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | tagRe = regexp.MustCompile(`<[a-zA-Z][^>]*>`) |
| 16 | attrRe = regexp.MustCompile(`\s((?:rst|fichas)-[a-z0-9-]+)`) |
| 17 | selectorRe = regexp.MustCompile(`\[((?:rst|fichas)-[a-z0-9-]+)`) |
| 18 | iconRe = regexp.MustCompile(`\{\{-?\s*icon\s+"([a-z0-9-]+)"`) |
| 19 | ) |
| 20 | |
| 21 | func read(t *testing.T, path string) string { |
| 22 | t.Helper() |
| 23 | b, err := os.ReadFile(path) |
| 24 | if err != nil { |
| 25 | t.Fatalf("reading %s: %v", path, err) |
| 26 | } |
| 27 | return string(b) |
| 28 | } |
| 29 | |
| 30 | // TestTemplatesUseOnlyDefinedAttributes: every rst-* or fichas-* attribute a |
| 31 | // template writes must have a rule in one of the app's stylesheets. |
| 32 | // |
| 33 | // The first index shipped rst-stack and rst-muted, which rastrillo did not |
| 34 | // have. With no rule behind them the browser's defaults showed through — a |
| 35 | // bullet point and an underlined purple link — and nothing failed. Only |
| 36 | // attributes in THIS app's templates are checked: what rastrillo's partials |
| 37 | // emit is the framework's business, and some of those are script hooks. |
| 38 | func TestTemplatesUseOnlyDefinedAttributes(t *testing.T) { |
| 39 | defined := map[string]bool{} |
| 40 | sheets, _ := filepath.Glob("../fichas/static/*.css") |
| 41 | brand, _ := filepath.Glob("../fichas/static/brand/*.css") |
| 42 | for _, css := range append(sheets, brand...) { |
| 43 | for _, m := range selectorRe.FindAllStringSubmatch(read(t, css), -1) { |
| 44 | defined[m[1]] = true |
| 45 | } |
| 46 | } |
| 47 | if !defined["rst-btn"] || !defined["fichas-grid"] { |
| 48 | t.Fatal("the stylesheets were not found or not parsed; this test would pass vacuously") |
| 49 | } |
| 50 | |
| 51 | templates, _ := filepath.Glob("../fichas/templates/*.html") |
| 52 | if len(templates) == 0 { |
| 53 | t.Fatal("no templates found; this test would pass vacuously") |
| 54 | } |
| 55 | var bad []string |
| 56 | for _, f := range templates { |
| 57 | for _, tag := range tagRe.FindAllString(read(t, f), -1) { |
| 58 | for _, m := range attrRe.FindAllStringSubmatch(tag, -1) { |
| 59 | if !defined[m[1]] { |
| 60 | bad = append(bad, filepath.Base(f)+": "+m[1]) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | sort.Strings(bad) |
| 66 | for _, b := range bad { |
| 67 | t.Errorf("%s has no rule in any stylesheet — an invented attribute renders as browser defaults", b) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // TestTemplateIconsExist: icons.Icon returns "" for a slug it does not know, |
| 72 | // so a missing icon renders as nothing. Six of the first seven the mockups |
| 73 | // used were missing, and nothing noticed until a screenshot. |
| 74 | func TestTemplateIconsExist(t *testing.T) { |
| 75 | templates, _ := filepath.Glob("../fichas/templates/*.html") |
| 76 | var missing []string |
| 77 | for _, f := range templates { |
| 78 | for _, m := range iconRe.FindAllStringSubmatch(read(t, f), -1) { |
| 79 | if icons.Icon(m[1]) == "" { |
| 80 | missing = append(missing, filepath.Base(f)+": "+m[1]) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if len(missing) > 0 { |
| 85 | t.Errorf("icon slugs not in internal/fichas/icons:\n %s", strings.Join(missing, "\n ")) |
| 86 | } |
| 87 | } |
| 88 | |