package web
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"unicode"
)
// titleLiteral matches a page title written as a literal: `Title: "..."`
// in a pageData composite, or an assignment to a .Title field.
var titleLiteral = regexp.MustCompile(`\.?Title:?\s*=?\s*"([^"]*)"`)
// A page's title is a heading and a browser tab, and both are read as a
// name. One lowercase title among thirty capitalised ones does not read
// as a style, it reads as a page somebody forgot — which is exactly
// what "notifications" was.
//
// Source-scanning rather than page-rendering on purpose: a title is
// wrong the moment it is WRITTEN, and a test that only sees the pages
// somebody remembered to fixture cannot say that.
func TestEveryPageTitleStartsWithACapital(t *testing.T) {
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
checked := 0
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
continue
}
src, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
for _, m := range titleLiteral.FindAllStringSubmatch(string(src), -1) {
title := m[1]
if title == "" {
continue
}
r := []rune(title)[0]
if !unicode.IsLetter(r) {
// A title starting with a symbol or digit is somebody
// else's words — a hash, a path — not a page name.
continue
}
// The forge's own name is lowercase everywhere it appears —
// the logo, the footer, the tab's suffix — and the layout
// compares a title against it to avoid "quarry — quarry".
// The brand is not a page title.
if title == "quarry" {
continue
}
checked++
if unicode.IsLower(r) {
t.Errorf("FAIL: %s: page title %q starts lowercase", f, title)
}
}
}
if checked < 20 {
t.Fatalf("FAIL: only %d titles were checked — the scan stopped seeing them", checked)
}
t.Logf("ok: %d page titles, every one capitalised", checked)
}