jorge/markup/templates_test.go

203 lines
4.1 KiB
Go
Raw Normal View History

package markup
import (
"os"
"strings"
"testing"
)
func TestParseTemplate(t *testing.T) {
input := `---
title: my new post
subtitle: a blog post
tags: ["software", "web"]
---
<p>Hello World!</p>
`
file := newFile("test*.html", input)
defer os.Remove(file.Name())
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
assertEqual(t, err, nil)
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()
assertEqual(t, err, nil)
assertEqual(t, string(content), "<p>Hello World!</p>")
}
func TestNonTemplate(t *testing.T) {
// not identified as front matter, leaving file as is
input := `+++
title: my new post
subtitle: a blog post
+++
<p>Hello World!</p>`
file := newFile("test*.html", input)
defer os.Remove(file.Name())
_, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
assertEqual(t, err, nil)
// 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"]
---
<p>Hello World!</p>`
file = newFile("test*.html", input)
defer os.Remove(file.Name())
_, err = Parse(NewEngine("https://olano.dev", "includes"), file.Name())
assertEqual(t, err, nil)
}
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(NewEngine("https://olano.dev", "includes"), file.Name())
assertEqual(t, err.Error(), "front matter not closed")
input = `---
title
tags: ["software", "web"]
---
<p>Hello World!</p>`
file = newFile("test*.html", input)
defer os.Remove(file.Name())
_, err = Parse(NewEngine("https://olano.dev", "includes"), file.Name())
assert(t, strings.Contains(err.Error(), "invalid yaml"))
}
func TestRenderLiquid(t *testing.T) {
2024-02-11 17:30:10 +01:00
input := `---
title: my new post
subtitle: a blog post
tags: ["software", "web"]
---
Add site struct and layout support Squashed commit of the following: commit 0ee8a385f111be807b4485b42316df6698d962f9 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 14:12:22 2024 -0300 load layouts commit 1c2594bb8aa6d6d9fbafcb530fdcdbdec2c146e7 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 13:17:28 2024 -0300 add Site struct, explore some refactors commit 3d8acb3957f5ba38a6e48d2614b9af65f1219298 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 11:33:19 2024 -0300 prepare new phases structure for build command commit fe7dcf9fb08c7b3e5679cf08c80beecc2eeb36e5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:57:52 2024 -0300 set template type commit d9faa70c8d2d23c9b62904e7429a82437517b9c5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:49:30 2024 -0300 add Type to template commit 27e0feede10f6c1340ce722085011a5eebcaee13 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 19:01:16 2024 -0300 stub build and render extensions commit e25518de3440cb3df2aa5674523128eff1d23404 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 17:37:30 2024 -0300 pass pre-parsed layouts by arg instead commit b3c2c9ebeb0d07d425f6db6a1bbbb5ee61c04548 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 15:13:17 2024 -0300 first stab at recursively populating layouts commit 4fe112694a3c44056f7aa5bd2be0794abcf4aa2a Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 14:33:07 2024 -0300 initial support for page bindings
2024-02-12 19:16:56 +01:00
<h1>{{ page.title }}</h1>
<h2>{{ page.subtitle }}</h2>
<ul>{% for tag in page.tags %}
2024-02-11 17:30:10 +01:00
<li>{{tag}}</li>{% endfor %}
</ul>`
2024-02-11 17:30:10 +01:00
file := newFile("test*.html", input)
defer os.Remove(file.Name())
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
2024-02-11 17:30:10 +01:00
assertEqual(t, err, nil)
content, err := templ.Render()
2024-02-11 17:30:10 +01:00
assertEqual(t, err, nil)
expected := `<h1>my new post</h1>
<h2>a blog post</h2>
<ul>
<li>software</li>
<li>web</li>
</ul>`
2024-02-11 17:30:10 +01:00
assertEqual(t, string(content), expected)
}
func TestRenderOrg(t *testing.T) {
2024-02-11 18:03:28 +01:00
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(NewEngine("https://olano.dev", "includes"), file.Name())
2024-02-11 18:03:28 +01:00
assertEqual(t, err, nil)
content, err := templ.Render()
2024-02-11 18:03:28 +01:00
assertEqual(t, err, nil)
expected := `<h1 id="my-title">
2024-02-11 18:03:28 +01:00
My title
2024-02-16 15:55:53 +01:00
</h1>
<h2 id="my-subtitle">
2024-02-11 18:03:28 +01:00
my Subtitle
2024-02-16 15:55:53 +01:00
</h2>
2024-02-11 18:03:28 +01:00
<ul>
<li>list 1</li>
<li>list 2</li>
</ul>
`
assertEqual(t, string(content), expected)
}
func TestRenderMarkdown(t *testing.T) {
input := `---
title: my new post
subtitle: a blog post
tags: ["software", "web"]
---
# My title
## my Subtitle
- list 1
- list 2
`
file := newFile("test*.md", input)
defer os.Remove(file.Name())
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
assertEqual(t, err, nil)
content, err := templ.Render()
assertEqual(t, err, nil)
expected := `<h1>My title</h1>
<h2>my Subtitle</h2>
<ul>
<li>list 1</li>
<li>list 2</li>
</ul>
`
assertEqual(t, string(content), expected)
}
// ------ HELPERS --------
Add site struct and layout support Squashed commit of the following: commit 0ee8a385f111be807b4485b42316df6698d962f9 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 14:12:22 2024 -0300 load layouts commit 1c2594bb8aa6d6d9fbafcb530fdcdbdec2c146e7 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 13:17:28 2024 -0300 add Site struct, explore some refactors commit 3d8acb3957f5ba38a6e48d2614b9af65f1219298 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 11:33:19 2024 -0300 prepare new phases structure for build command commit fe7dcf9fb08c7b3e5679cf08c80beecc2eeb36e5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:57:52 2024 -0300 set template type commit d9faa70c8d2d23c9b62904e7429a82437517b9c5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:49:30 2024 -0300 add Type to template commit 27e0feede10f6c1340ce722085011a5eebcaee13 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 19:01:16 2024 -0300 stub build and render extensions commit e25518de3440cb3df2aa5674523128eff1d23404 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 17:37:30 2024 -0300 pass pre-parsed layouts by arg instead commit b3c2c9ebeb0d07d425f6db6a1bbbb5ee61c04548 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 15:13:17 2024 -0300 first stab at recursively populating layouts commit 4fe112694a3c44056f7aa5bd2be0794abcf4aa2a Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 14:33:07 2024 -0300 initial support for page bindings
2024-02-12 19:16:56 +01:00
func newFile(path string, contents string) *os.File {
2024-02-12 20:40:12 +01:00
file, _ := os.CreateTemp("", path)
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)
}
}