shithub: hugo

Download patch

ref: f28a8fa0c2d8a7ec038a9dc8a07ecf5716587178
parent: 9d15262ee54ee03010e4f46b1a575744bda640da
author: Noah Campbell <[email protected]>
date: Fri Aug 23 10:47:03 EDT 2013

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.

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -169,9 +169,7 @@
 			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 @@
 	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 @@
 	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) 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
--- /dev/null
+++ b/hugolib/site_test.go
@@ -1,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)
+		}
+	}
+}