add tests for site struct

Squashed commit of the following:

commit 288291800c346cc103513169b33dc32e0d2182a0
Author: facundoolano <facundo.olano@gmail.com>
Date:   Tue Feb 13 13:36:33 2024 -0300

    test the rest of the pages

commit 184fad8d925b7e2f5e6942b148508d835fda2207
Author: facundoolano <facundo.olano@gmail.com>
Date:   Tue Feb 13 13:34:40 2024 -0300

    remove extra new lines

commit 813bc35ba98c13dbf0951638fab6f04db59768b0
Author: facundoolano <facundo.olano@gmail.com>
Date:   Tue Feb 13 13:15:23 2024 -0300

    first portion of site test

commit 766aecb68c74e812bfe65479cd16a97f85dcb86f
Author: facundoolano <facundo.olano@gmail.com>
Date:   Mon Feb 12 16:44:43 2024 -0300

    stub site tests
This commit is contained in:
facundoolano 2024-02-13 13:43:20 -03:00
parent 99654851f4
commit 526e3c3e5a
4 changed files with 176 additions and 68 deletions

View file

@ -123,8 +123,10 @@ func (site Site) Render(templ *templates.Template) (string, error) {
func (site Site) baseContext() map[string]interface{} {
return map[string]interface{}{
"config": site.config,
"posts": site.posts,
"tags": site.tags,
"site": map[string]interface{}{
"config": site.config,
"posts": site.posts,
"tags": site.tags,
},
}
}

161
site/site_test.go Normal file
View file

@ -0,0 +1,161 @@
package site
import (
"os"
"path/filepath"
"testing"
)
func TestLoadAndRenderTemplates(t *testing.T) {
root, src, layouts := newProject()
defer os.RemoveAll(root)
// add two layouts
content := `---
---
<html>
<head><title>{{page.title}}</title></head>
<body>
{{content}}
</body>
</html>`
file := newFile(layouts, "base.html", content)
defer os.Remove(file.Name())
content = `---
layout: base
---
<h1>{{page.title}}</h1>
<h2>{{page.subtitle}}</h2>
{{content}}`
file = newFile(layouts, "post.html", content)
defer os.Remove(file.Name())
// add two posts
content = `---
layout: post
title: hello world!
subtitle: my first post
date: 2024-01-01
---
<p>Hello world!</p>`
file = newFile(src, "hello.html", content)
defer os.Remove(file.Name())
content = `---
layout: post
title: goodbye!
subtitle: my last post
date: 2024-02-01
---
<p>goodbye world!</p>`
file = newFile(src, "goodbye.html", content)
defer os.Remove(file.Name())
// add a page (no date)
content = `---
layout: base
title: about
---
<p>about this site</p>`
file = newFile(src, "about.html", content)
defer os.Remove(file.Name())
// add a static file (no front matter)
content = `go away!`
file = newFile(src, "robots.txt", content)
site, err := Load(src, layouts)
assertEqual(t, err, nil)
assertEqual(t, len(site.posts), 2)
assertEqual(t, len(site.pages), 1)
assertEqual(t, len(site.layouts), 2)
_, ok := site.layouts["base"]
assert(t, ok)
_, ok = site.layouts["post"]
assert(t, ok)
hello := site.posts[1]
content, err = site.Render(&hello)
assertEqual(t, err, nil)
assertEqual(t, content, `<html>
<head><title>hello world!</title></head>
<body>
<h1>hello world!</h1>
<h2>my first post</h2>
<p>Hello world!</p>
</body>
</html>`)
goodbye := site.posts[0]
content, err = site.Render(&goodbye)
assertEqual(t, err, nil)
assertEqual(t, content, `<html>
<head><title>goodbye!</title></head>
<body>
<h1>goodbye!</h1>
<h2>my last post</h2>
<p>goodbye world!</p>
</body>
</html>`)
about := site.pages[0]
content, err = site.Render(&about)
assertEqual(t, err, nil)
assertEqual(t, content, `<html>
<head><title>about</title></head>
<body>
<p>about this site</p>
</body>
</html>`)
}
func TestRenderArchive(t *testing.T) {
// TODO
}
func TestRenderTags(t *testing.T) {
// TODO
}
func TestRenderDataFile(t *testing.T) {
// TODO
}
// ------ HELPERS --------
func newProject() (string, string, string) {
projectDir, _ := os.MkdirTemp("", "root")
layoutsDir := filepath.Join(projectDir, "layouts")
srcDir := filepath.Join(projectDir, "src")
os.Mkdir(layoutsDir, 0777)
os.Mkdir(filepath.Join(projectDir, "src"), 0777)
return projectDir, layoutsDir, srcDir
}
func newFile(dir string, filename string, contents string) *os.File {
path := filepath.Join(dir, filename)
file, _ := os.Create(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)
}
}

View file

@ -87,8 +87,14 @@ func (templ Template) Render(context map[string]interface{}) (string, error) {
// now read the proper template contents to memory
contents := ""
isFirstLine := true
for scanner.Scan() {
contents += scanner.Text() + "\n"
if isFirstLine {
isFirstLine = false
contents = scanner.Text()
} else {
contents += "\n" + scanner.Text()
}
}
if strings.HasSuffix(templ.SrcPath, ".org") {

View file

@ -29,7 +29,7 @@ tags: ["software", "web"]
content, err := templ.Render(nil)
assertEqual(t, err, nil)
assertEqual(t, string(content), "<p>Hello World!</p>\n")
assertEqual(t, string(content), "<p>Hello World!</p>")
}
func TestNonTemplate(t *testing.T) {
@ -96,8 +96,7 @@ tags: ["software", "web"]
<h2>{{ page.subtitle }}</h2>
<ul>{% for tag in page.tags %}
<li>{{tag}}</li>{% endfor %}
</ul>
`
</ul>`
file := newFile("test*.html", input)
defer os.Remove(file.Name())
@ -112,8 +111,7 @@ tags: ["software", "web"]
<ul>
<li>software</li>
<li>web</li>
</ul>
`
</ul>`
assertEqual(t, string(content), expected)
}
@ -161,65 +159,6 @@ my Subtitle
assertEqual(t, string(content), expected)
}
func TestRenderLiquidLayout(t *testing.T) {
// input := `---
// title: base layout
// ---
// <!DOCTYPE html>
// <html>
// <body>
// <p>this is the {{layout.title}} that wraps the content of {{ page.title}}</p>
// {{ content }}
// </body>
// </html>
// `
// base := newFile("layouts/base*.html", input)
// defer os.Remove(base.Name())
// baseTempl, err := Parse(base.Name())
// assertEqual(t, err, nil)
// context := map[string]interface{}{
// "layouts": map[string]Template{
// "base": *baseTempl,
// },
// }
// input = `---
// title: my very first post
// layout: base
// date: 2023-12-01
// ---
// <h1>{{page.title}}</h1>`
// post := newFile("src/post1*.html", input)
// defer os.Remove(post.Name())
// templ, err := Parse(post.Name())
// assertEqual(t, err, nil)
// content, err := templ.Render(context)
// assertEqual(t, err, nil)
// expected := `<!DOCTYPE html>
// <html>
// <body>
// <p>this is the base layout that wraps the content of my very first post</p>
// <h1>my very first post</h1>
// </body>
// </html>
// `
//
// 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 {