74 lines
2.4 KiB
Go
74 lines
2.4 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}}")
|
|
}
|
|
}
|
|
|
|
// TestEscape checks that text escaping leaves quotes alone.
|
|
func TestEscape(t *testing.T) {
|
|
if got, want := Escape(`a & b < c > "d" 'e'`), `a & b < c > "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 & "b" 'c' <d>`; got != want {
|
|
t.Errorf("EscapeAttr() = %q, want %q", got, want)
|
|
}
|
|
}
|