// 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", "

{{name}}.{{tld_1}} and {{name}}

", ctx) if err != nil { t.Fatalf("Substitute() error = %v", err) } if want := "

kmalloc.org and kmalloc

"; 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}}") } } // TestSubstituteEscapedKeepsEscapes checks that a backslash before a placeholder // keeps it as written: it becomes a marker that Unescape turns back into the // placeholder after the Markdown is rendered. An unknown escaped one is no error. func TestSubstituteEscapedKeepsEscapes(t *testing.T) { got, err := SubstituteEscaped("page.md", "Write \\{{lang}} for {{lang}}, and \\{{nope}}.", map[string]string{"lang": "en"}) if err != nil { t.Fatalf("SubstituteEscaped() error = %v", err) } if strings.Contains(got, "{{") || strings.Contains(got, "\\") { t.Errorf("SubstituteEscaped() = %q, want no braces and no backslash left", got) } if want := "Write {{lang}} for en, and {{nope}}."; Unescape(got) != want { t.Errorf("Unescape(%q) = %q, want %q", got, Unescape(got), want) } } // TestSubstituteEscapedBackslashParity checks that an escaped backslash does not // escape the placeholder: two backslashes are one literal backslash. func TestSubstituteEscapedBackslashParity(t *testing.T) { ctx := map[string]string{"lang": "en"} even, err := SubstituteEscaped("f", "\\\\{{lang}}", ctx) if err != nil || even != "\\\\en" { t.Errorf("two backslashes: got %q, %v, want the value after both backslashes", even, err) } odd, err := SubstituteEscaped("f", "\\\\\\{{lang}}", ctx) if err != nil || Unescape(odd) != "\\\\{{lang}}" { t.Errorf("three backslashes: got %q, %v, want two backslashes then the placeholder", Unescape(odd), err) } } // TestSubstituteEscapedUnknown checks that an unknown placeholder still stops the // build and names the file. func TestSubstituteEscapedUnknown(t *testing.T) { _, err := SubstituteEscaped("news/x.md", "ok {{a}} bad {{nope}}", map[string]string{"a": "1"}) if err == nil || !strings.Contains(err.Error(), "news/x.md") || !strings.Contains(err.Error(), "nope") { t.Errorf("SubstituteEscaped() 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 & 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' `), `a & "b" 'c' <d>`; got != want { t.Errorf("EscapeAttr() = %q, want %q", got, want) } }