motorino/internal/render/render_test.go
tgiachi 917683afa4 fix: close the minor findings of the first review
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.
2026-11-15 10:32:19 +01:00

111 lines
4.1 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package render
import (
"strings"
"testing"
)
// TestSubstituteKnown checks that known placeholders are replaced everywhere.
func TestSubstituteKnown(t *testing.T) {
ctx := map[string]string{"name": "kmalloc", "tld_1": "org"}
got, err := Substitute("page.html", "<p>{{name}}.{{tld_1}} and {{name}}</p>", ctx)
if err != nil {
t.Fatalf("Substitute() error = %v", err)
}
if want := "<p>kmalloc.org and kmalloc</p>"; got != want {
t.Errorf("Substitute() = %q, want %q", got, want)
}
}
// TestSubstituteUnknownNamesFile checks that an unknown placeholder stops the
// build and names both the file and the placeholder.
func TestSubstituteUnknownNamesFile(t *testing.T) {
_, err := Substitute("news/post.md", "Hello {{nope}}", map[string]string{})
if err == nil {
t.Fatal("Substitute() error = nil")
}
for _, want := range []string{"news/post.md", "nope"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not contain %q", err, want)
}
}
}
// TestSubstituteLeavesNonPlaceholders checks that braces that do not form a
// placeholder are left as written, so articles can show template code.
func TestSubstituteLeavesNonPlaceholders(t *testing.T) {
for _, in := range []string{"{{.Name}}", "{{ spaced }}", "{{UPPER}}", "{{a-b}}", "{single}", "{{}}"} {
got, err := Substitute("f", in, map[string]string{})
if err != nil {
t.Errorf("Substitute(%q) error = %v", in, err)
}
if got != in {
t.Errorf("Substitute(%q) = %q, want it unchanged", in, got)
}
}
}
// TestSubstituteValueIsNotRescanned checks that a replaced value is inserted
// as is and its own braces are not substituted again.
func TestSubstituteValueIsNotRescanned(t *testing.T) {
got, err := Substitute("f", "{{a}}", map[string]string{"a": "{{b}}", "b": "x"})
if err != nil {
t.Fatal(err)
}
if got != "{{b}}" {
t.Errorf("Substitute() = %q, want %q", got, "{{b}}")
}
}
// TestSubstituteMarkdownFences checks that placeholders in fenced code blocks
// stay as written (an unknown one is no error there), while code spans are
// substituted: the pages use them to show a value in monospace.
func TestSubstituteMarkdownFences(t *testing.T) {
ctx := map[string]string{"lang": "en"}
src := "Outside {{lang}} and `{{lang}}`.\n\n```\n{{lang}} {{nope}}\n```\n\n~~~go\n{{lang}}\n~~~\n\nEnd {{lang}}.\n"
got, err := SubstituteMarkdown("page.md", src, ctx)
if err != nil {
t.Fatalf("SubstituteMarkdown() error = %v", err)
}
want := "Outside en and `en`.\n\n```\n{{lang}} {{nope}}\n```\n\n~~~go\n{{lang}}\n~~~\n\nEnd en.\n"
if got != want {
t.Errorf("SubstituteMarkdown() = %q, want %q", got, want)
}
}
// TestSubstituteMarkdownEscape checks that a backslash before a placeholder
// keeps it as written: Markdown then drops the backslash.
func TestSubstituteMarkdownEscape(t *testing.T) {
got, err := SubstituteMarkdown("page.md", "Write \\{{lang}} for {{lang}}, and \\{{nope}} is fine.\n", map[string]string{"lang": "en"})
if err != nil {
t.Fatalf("SubstituteMarkdown() error = %v", err)
}
if want := "Write \\{{lang}} for en, and \\{{nope}} is fine.\n"; got != want {
t.Errorf("SubstituteMarkdown() = %q, want %q", got, want)
}
}
// TestSubstituteMarkdownUnknownOutsideCode checks that an unknown placeholder
// outside a fence still stops the build and names the file.
func TestSubstituteMarkdownUnknownOutsideCode(t *testing.T) {
_, err := SubstituteMarkdown("news/x.md", "ok {{a}}\n\nbad {{nope}}\n", map[string]string{"a": "1"})
if err == nil || !strings.Contains(err.Error(), "news/x.md") || !strings.Contains(err.Error(), "nope") {
t.Errorf("SubstituteMarkdown() error = %v, want it to name news/x.md and nope", err)
}
}
// TestEscape checks that text escaping leaves quotes alone.
func TestEscape(t *testing.T) {
if got, want := Escape(`a & b < c > "d" 'e'`), `a &amp; b &lt; c &gt; "d" 'e'`; got != want {
t.Errorf("Escape() = %q, want %q", got, want)
}
}
// TestEscapeAttr checks that attribute escaping also covers both quotes.
func TestEscapeAttr(t *testing.T) {
if got, want := EscapeAttr(`a & "b" 'c' <d>`), `a &amp; &quot;b&quot; &#x27;c&#x27; &lt;d&gt;`; got != want {
t.Errorf("EscapeAttr() = %q, want %q", got, want)
}
}