mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 22:19:57 -05:00
RenderThing test cases
Provide unit test support RenderThing. One observation is that creating the site.Tmpl variable is a one time event. site.Tmpl doesn't like additional templates with the same name. This means that updating a template while in --watch mode requires throwing away the entire Site object and creating a new one. Not that this is a bad idea, but it is something I discovered while working on these unit tests.
This commit is contained in:
parent
9d15262ee5
commit
f28a8fa0c2
2 changed files with 127 additions and 7 deletions
|
@ -169,9 +169,7 @@ func (s *Site) loadTemplates() {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text := string(filetext)
|
||||
t := s.Tmpl.New(s.generateTemplateNameFrom(path))
|
||||
template.Must(t.Parse(text))
|
||||
s.addTemplate(s.generateTemplateNameFrom(path), string(filetext))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -179,6 +177,11 @@ func (s *Site) loadTemplates() {
|
|||
filepath.Walk(s.absLayoutDir(), walker)
|
||||
}
|
||||
|
||||
func (s *Site) addTemplate(name, tmpl string) (err error) {
|
||||
_, err = s.Tmpl.New(name).Parse(tmpl)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Site) generateTemplateNameFrom(path string) (name string) {
|
||||
name = filepath.ToSlash(path[len(s.absLayoutDir())+1:])
|
||||
return
|
||||
|
@ -188,11 +191,9 @@ func (s *Site) primeTemplates() {
|
|||
alias := "<!DOCTYPE html>\n <html>\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
|
||||
alias_xhtml := "<!DOCTYPE html>\n <html xmlns=\"http://www.w3.org/1999/xhtml\">\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
|
||||
|
||||
t := s.Tmpl.New("alias")
|
||||
template.Must(t.Parse(alias))
|
||||
s.addTemplate("alias", alias)
|
||||
s.addTemplate("alias-xhtml", alias_xhtml)
|
||||
|
||||
t = s.Tmpl.New("alias-xhtml")
|
||||
template.Must(t.Parse(alias_xhtml))
|
||||
}
|
||||
|
||||
func (s *Site) initialize() {
|
||||
|
@ -616,6 +617,9 @@ func (s *Site) NewNode() Node {
|
|||
}
|
||||
|
||||
func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error) {
|
||||
if s.Tmpl.Lookup(layout) == nil {
|
||||
return nil, errors.New("Layout not found")
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
err := s.Tmpl.ExecuteTemplate(buffer, layout, d)
|
||||
return buffer, err
|
||||
|
|
116
hugolib/site_test.go
Normal file
116
hugolib/site_test.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package hugolib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var TEMPLATE_TITLE = "{{ .Title }}"
|
||||
var PAGE_SIMPLE_TITLE = `---
|
||||
title: simple template
|
||||
---
|
||||
content`
|
||||
|
||||
var TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}"
|
||||
var TEMPLATE_FUNC = "{{ .Title | urlize }}"
|
||||
|
||||
func pageMust(p *Page, err error) *Page {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
|
||||
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
_, err := s.RenderThing(p, "foobar")
|
||||
if err == nil {
|
||||
t.Errorf("Expected err to be returned when missing the template.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrimeTempaltes(t *testing.T) {
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
s.primeTemplates()
|
||||
if s.Tmpl.Lookup("alias") == nil {
|
||||
t.Fatalf("alias template not created.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddInvalidTemplate(t *testing.T) {
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
err := s.addTemplate("missing", TEMPLATE_MISSING_FUNC)
|
||||
if err == nil {
|
||||
t.Fatalf("Expecting the template to return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) {
|
||||
content, err := s.RenderThing(p, tmplName)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to render template.")
|
||||
}
|
||||
|
||||
if string(content.Bytes()) != expected {
|
||||
t.Fatalf("Content did not match expected: %s. got: %s", expected, content)
|
||||
}
|
||||
}
|
||||
|
||||
func _TestAddSameTemplateTwice(t *testing.T) {
|
||||
p := pageMust(ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md"))
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
err := s.addTemplate("foo", TEMPLATE_TITLE)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to add template foo")
|
||||
}
|
||||
|
||||
matchRender(t, s, p, "foo", "simple template")
|
||||
|
||||
err = s.addTemplate("foo", "NEW {{ .Title }}")
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to add template foo: %s", err)
|
||||
}
|
||||
|
||||
matchRender(t, s, p, "foo", "NEW simple template")
|
||||
}
|
||||
|
||||
func TestRenderThing(t *testing.T) {
|
||||
tests := []struct {
|
||||
content string
|
||||
template string
|
||||
expected string
|
||||
}{
|
||||
{PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"},
|
||||
{PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"},
|
||||
}
|
||||
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
|
||||
for i, test := range tests {
|
||||
p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing buffer: %s", err)
|
||||
}
|
||||
templateName := fmt.Sprintf("foobar%d", i)
|
||||
err = s.addTemplate(templateName, test.template)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to add template")
|
||||
}
|
||||
|
||||
html, err := s.RenderThing(p, templateName)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to render html: %s", err)
|
||||
}
|
||||
|
||||
if string(html.Bytes()) != test.expected {
|
||||
t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, html)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue