package templates import ( "os" "path/filepath" "strings" "testing" ) func TestParseTemplate(t *testing.T) { input := `--- title: my new post subtitle: a blog post tags: ["software", "web"] ---
Hello World!
` file := newFile("test*.html", input) defer os.Remove(file.Name()) templ, err := Parse(file.Name()) assertEqual(t, err, nil) assertEqual(t, templ.Type, PAGE) assertEqual(t, templ.Ext(), ".html") assertEqual(t, templ.Metadata["title"], "my new post") assertEqual(t, templ.Metadata["subtitle"], "a blog post") assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software") assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web") content, err := templ.Render(nil) assertEqual(t, err, nil) assertEqual(t, string(content), "Hello World!
\n") } func TestNonTemplate(t *testing.T) { // not identified as front matter, leaving file as is input := `+++ title: my new post subtitle: a blog post +++Hello World!
` file := newFile("test*.html", input) defer os.Remove(file.Name()) templ, err := Parse(file.Name()) assertEqual(t, err, nil) assertEqual(t, templ.Type, STATIC) // not first thing in file, leaving as is input = `#+OPTIONS: toc:nil num:nil --- title: my new post subtitle: a blog post tags: ["software", "web"] ---Hello World!
` file = newFile("test*.html", input) defer os.Remove(file.Name()) templ, err = Parse(file.Name()) assertEqual(t, err, nil) assertEqual(t, templ.Type, STATIC) } func TestInvalidFrontMatter(t *testing.T) { input := `--- title: my new post subtitle: a blog post tags: ["software", "web"] ` file := newFile("test*.html", input) defer os.Remove(file.Name()) _, err := Parse(file.Name()) assertEqual(t, err.Error(), "front matter not closed") input = `--- title tags: ["software", "web"] ---Hello World!
` file = newFile("test*.html", input) defer os.Remove(file.Name()) _, err = Parse(file.Name()) assert(t, strings.Contains(err.Error(), "invalid yaml")) } func TestRenderLiquid(t *testing.T) { input := `--- title: my new post subtitle: a blog post tags: ["software", "web"] ---this is the {{layout.title}} that wraps the content of {{ page.title}}
{{ content }} ` base := newFile("layouts/base*.html", input) defer os.Remove(base.Name()) baseTempl, err := Parse(base.Name()) assertEqual(t, err, nil) assertEqual(t, baseTempl.Type, LAYOUT) context := map[string]interface{}{ "layouts": map[string]Template{ "base": *baseTempl, }, } input = `--- title: my very first post layout: base date: 2023-12-01 ---this is the base layout that wraps the content of my very first post