Require refuses an empty value. Heading ids: an explicit {#id} must be sound
and unique, generated ids avoid the explicit ones and each other, and a heading
with no ASCII letters gets a fallback. Fenced code blocks keep their
placeholders and a backslash keeps one anywhere. The em dash rule covers the
header of the manifesto pages and the HTML entities. The feed makes src links
absolute, base_url is required, and the news page must be a folder index. A
category written twice in a post counts once, a block name in a news title is no
longer taken for a placeholder, and build refuses extra arguments.
167 lines
6.7 KiB
Go
167 lines
6.7 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package content
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPagePath checks how a page file becomes an address.
|
|
func TestPagePath(t *testing.T) {
|
|
tests := []struct {
|
|
lang, rel, want string
|
|
}{
|
|
{"en", "index.md", "/"},
|
|
{"en", "services.md", "/services.html"},
|
|
{"en", "news/index.md", "/news/"},
|
|
{"it", "index.md", "/it/"},
|
|
{"it", "servizi.md", "/it/servizi.html"},
|
|
{"it", "news/index.md", "/it/news/"},
|
|
{"es", "manifesto.md", "/es/manifesto.html"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.lang+"/"+tt.rel, func(t *testing.T) {
|
|
if got := PagePath("en", tt.lang, tt.rel); got != tt.want {
|
|
t.Errorf("PagePath(en, %s, %s) = %q, want %q", tt.lang, tt.rel, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// pageFile is a valid page with the given extra header lines and body.
|
|
func pageFile(extra, body string) string {
|
|
return "Title: Services\nID: services\nDescription: What we run.\n" + extra + "-\n" + body
|
|
}
|
|
|
|
// TestLoadPages checks the fields of a loaded page.
|
|
func TestLoadPages(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{
|
|
"content/en/services.md": pageFile("Switch: it=/it/servizi.html, de=/de/\n", "Body.\n"),
|
|
"content/en/news/index.md": "Title: News\nID: news\nDescription: The news.\n-\nIntro.\n",
|
|
"content/it/servizi.md": "Title: Servizi\nID: services\nDescription: Cosa offriamo.\nLayout: bare\n-\nCorpo.\n",
|
|
"content/en/notes/ignored.txt": "not markdown",
|
|
})
|
|
pages, err := LoadPages(dir, []string{"en", "it"})
|
|
if err != nil {
|
|
t.Fatalf("LoadPages() error = %v", err)
|
|
}
|
|
if len(pages) != 3 {
|
|
t.Fatalf("got %d pages, want 3: %+v", len(pages), pages)
|
|
}
|
|
byPath := map[string]Page{}
|
|
for _, p := range pages {
|
|
byPath[p.Lang+" "+p.Path] = p
|
|
}
|
|
s := byPath["en /services.html"]
|
|
if s.ID != "services" || s.Title != "Services" || s.Description != "What we run." ||
|
|
s.Layout != "page" || s.Body != "Body.\n" || s.Source != "content/en/services.md" {
|
|
t.Errorf("en services = %+v", s)
|
|
}
|
|
if s.Switch["it"] != "/it/servizi.html" || s.Switch["de"] != "/de/" {
|
|
t.Errorf("Switch = %v", s.Switch)
|
|
}
|
|
if it := byPath["it /it/servizi.html"]; it.Layout != "bare" || it.Lang != "it" {
|
|
t.Errorf("it services = %+v", it)
|
|
}
|
|
if n := byPath["en /news/"]; n.ID != "news" {
|
|
t.Errorf("news page = %+v", n)
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesManifestoVersion checks the keys of a translation of an older manifesto.
|
|
func TestLoadPagesManifestoVersion(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{
|
|
"content/es/m.md": "Title: M\nID: manifesto\nDescription: d\nLayout: manifesto\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n-\nB\n",
|
|
})
|
|
pages, err := LoadPages(dir, []string{"es"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(pages) != 1 || pages[0].ManifestoVersion != "1.0" || pages[0].ManifestoDate != "2026-11-12" {
|
|
t.Errorf("pages = %+v, want ManifestoVersion 1.0 and ManifestoDate 2026-11-12", pages)
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesRejectsEmDash checks that an em dash in a page is an error.
|
|
func TestLoadPagesRejectsEmDash(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{"content/en/x.md": pageFile("", "Words — more words.\n")})
|
|
_, err := LoadPages(dir, []string{"en"})
|
|
if err == nil || !strings.Contains(err.Error(), "content/en/x.md") {
|
|
t.Errorf("LoadPages() error = %v, want it to name content/en/x.md", err)
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesManifestoKeepsEmDash checks that the manifesto layout is exempt:
|
|
// its text is the owner's and is rewritten when the page is built.
|
|
func TestLoadPagesManifestoKeepsEmDash(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{
|
|
"content/en/manifesto.md": "Title: Manifesto\nID: manifesto\nDescription: What we stand for.\nLayout: manifesto\n-\n## 01 — Antifascist\n",
|
|
})
|
|
pages, err := LoadPages(dir, []string{"en"})
|
|
if err != nil {
|
|
t.Fatalf("LoadPages() error = %v", err)
|
|
}
|
|
if len(pages) != 1 || !strings.Contains(pages[0].Body, "—") {
|
|
t.Errorf("pages = %+v, want one page that keeps its em dash", pages)
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesManifestoHeaderEmDash checks that the exemption of the manifesto
|
|
// body does not cover the title, the description and the note.
|
|
func TestLoadPagesManifestoHeaderEmDash(t *testing.T) {
|
|
for _, field := range []string{"Title", "Description", "Note"} {
|
|
head := map[string]string{"Title": "M", "Description": "d", "Note": "n"}
|
|
head[field] = "words \u2014 more"
|
|
file := "Title: " + head["Title"] + "\nID: manifesto\nDescription: " + head["Description"] + "\nLayout: manifesto\nNote: " + head["Note"] + "\n-\n## 01 \u2014 Body\n"
|
|
dir := writeFiles(t, map[string]string{"content/en/m.md": file})
|
|
_, err := LoadPages(dir, []string{"en"})
|
|
if err == nil || !strings.Contains(err.Error(), "content/en/m.md") || !strings.Contains(err.Error(), field) {
|
|
t.Errorf("%s with an em dash: LoadPages() error = %v, want it to name content/en/m.md and %s", field, err, field)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesDuplicateID checks that two pages cannot share an ID in a language.
|
|
func TestLoadPagesDuplicateID(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{
|
|
"content/en/a.md": pageFile("", "A.\n"),
|
|
"content/en/b.md": pageFile("", "B.\n"),
|
|
})
|
|
_, err := LoadPages(dir, []string{"en"})
|
|
if err == nil || !strings.Contains(err.Error(), "services") {
|
|
t.Errorf("LoadPages() error = %v, want it to name the duplicate ID", err)
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesHeaderErrors checks the header rules of pages.
|
|
func TestLoadPagesHeaderErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name, file, want string
|
|
}{
|
|
{"unknown key", "Title: T\nID: x\nDescription: d\nTitolo: T\n-\nB\n", "Titolo"},
|
|
{"missing id", "Title: T\nDescription: d\n-\nB\n", "ID"},
|
|
{"bad layout", "Title: T\nID: x\nDescription: d\nLayout: fancy\n-\nB\n", "fancy"},
|
|
{"bad switch", "Title: T\nID: x\nDescription: d\nSwitch: nonsense\n-\nB\n", "Switch"},
|
|
{"switch target without a slash", "Title: T\nID: x\nDescription: d\nSwitch: en=elsewhere\n-\nB\n", "Switch"},
|
|
{"switch language twice", "Title: T\nID: x\nDescription: d\nSwitch: en=/a, en=/b\n-\nB\n", "Switch"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{"content/en/x.md": tt.file})
|
|
_, err := LoadPages(dir, []string{"en"})
|
|
if err == nil || !strings.Contains(err.Error(), tt.want) || !strings.Contains(err.Error(), "content/en/x.md") {
|
|
t.Errorf("LoadPages() error = %v, want it to name content/en/x.md and %s", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestLoadPagesMissingLanguageFolder checks that a listed language needs its folder.
|
|
func TestLoadPagesMissingLanguageFolder(t *testing.T) {
|
|
dir := writeFiles(t, map[string]string{"content/en/x.md": pageFile("", "B.\n")})
|
|
_, err := LoadPages(dir, []string{"en", "it"})
|
|
if err == nil || !strings.Contains(err.Error(), "content/it") {
|
|
t.Errorf("LoadPages() error = %v, want it to name content/it", err)
|
|
}
|
|
}
|