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"] ---

{{ page.title }}

{{ page.subtitle }}

` file := newFile("test*.html", input) defer os.Remove(file.Name()) templ, err := Parse(file.Name()) assertEqual(t, err, nil) ctx := map[string]interface{}{"page": templ.Metadata} content, err := templ.Render(ctx) assertEqual(t, err, nil) expected := `

my new post

a blog post

` assertEqual(t, string(content), expected) } func TestRenderOrg(t *testing.T) { input := `--- title: my new post subtitle: a blog post tags: ["software", "web"] --- #+OPTIONS: toc:nil num:nil * My title ** my Subtitle - list 1 - list 2 ` file := newFile("test*.org", input) defer os.Remove(file.Name()) templ, err := Parse(file.Name()) assertEqual(t, err, nil) assertEqual(t, templ.Ext(), ".html") content, err := templ.Render(nil) assertEqual(t, err, nil) expected := `

My title

my Subtitle

  • list 1
  • list 2
` assertEqual(t, string(content), expected) } func TestRenderLiquidLayout(t *testing.T) { input := `--- title: base layout ---

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 ---

{{page.title}}

` post := newFile("src/post1*.html", input) defer os.Remove(post.Name()) templ, err := Parse(post.Name()) assertEqual(t, err, nil) assertEqual(t, templ.Type, POST) content, err := templ.Render(context) assertEqual(t, err, nil) expected := `

this is the base layout that wraps the content of my very first post

my very first post

` assertEqual(t, string(content), expected) } func TestRenderOrgLayout(t *testing.T) { // TODO } func TestRenderLayoutLayout(t *testing.T) { // TODO } // ------ HELPERS -------- func newFile(path string, contents string) *os.File { parts := strings.Split(path, string(filepath.Separator)) name := parts[len(parts)-1] path = filepath.Join(parts[:len(parts)-1]...) path = filepath.Join(os.TempDir(), path) os.MkdirAll(path, 0777) file, _ := os.CreateTemp(path, name) file.WriteString(contents) return file } // TODO move to assert package func assert(t *testing.T, cond bool) { t.Helper() if !cond { t.Fatalf("%v is false", cond) } } func assertEqual(t *testing.T, a interface{}, b interface{}) { t.Helper() if a != b { t.Fatalf("%v != %v", a, b) } }