Introduce unit testing for page.go

This commit is contained in:
Noah Campbell 2013-08-04 19:02:15 -07:00
parent fa55cd9857
commit 274d324c8b
3 changed files with 24 additions and 9 deletions

View file

@ -78,10 +78,9 @@ func (p Pages) Limit(n int) Pages { return p[0:n] }
func initializePage(filename string) (page Page) { func initializePage(filename string) (page Page) {
page = Page{contentType: "", page = Page{contentType: "",
File: File{FileName: filename, File: File{FileName: filename, Extension: "html"},
Extension: "html"}, Node: Node{Keywords: make([]string, 10, 30)},
Params: make(map[string]interface{}), Params: make(map[string]interface{}),
Node: Node{Keywords: make([]string, 10, 30)},
Markup: "md"} Markup: "md"}
page.Date, _ = time.Parse("20060102", "20080101") page.Date, _ = time.Parse("20060102", "20080101")
page.setSection() page.setSection()

View file

@ -1,17 +1,33 @@
package hugolib package hugolib
import ( import (
"testing"
"path/filepath" "path/filepath"
"testing"
) )
func TestDegenerateMissingFolderInPageFilename(t *testing.T) { func TestDegenerateMissingFolderInPageFilename(t *testing.T) {
p := NewPage(filepath.Join("foobar")) p := NewPage(filepath.Join("foobar"))
if p != nil { if p.Section != "" {
t.Fatalf("Creating a new Page without a subdirectory should result in nil page") t.Fatalf("No section should be set for a file path: foobar")
} }
} }
func TestCreateNewPage(t *testing.T) {
toCheck := []map[string]string{
{"input": filepath.Join("sub", "foobar.html"), "expect": "sub"},
{"input": filepath.Join("content", "sub", "foobar.html"), "expect": "sub"},
{"input": filepath.Join("content", "dub", "sub", "foobar.html"), "expect": "sub"},
}
for _, el := range toCheck {
p := NewPage(el["input"])
if p.Section != el["expect"] {
t.Fatalf("Section not set to %s for page %s. Got: %s", el["expect"], el["input"], p.Section)
}
}
}
func TestSettingOutFileOnPageContainsCorrectSlashes(t *testing.T) { func TestSettingOutFileOnPageContainsCorrectSlashes(t *testing.T) {
s := NewSite(&Config{}) s := NewSite(&Config{})
p := NewPage(filepath.Join("sub", "foobar")) p := NewPage(filepath.Join("sub", "foobar"))

View file

@ -297,7 +297,7 @@ func (s *Site) RenderPages() error {
func (s *Site) WritePages() { func (s *Site) WritePages() {
for _, p := range s.Pages { for _, p := range s.Pages {
s.WritePublic(p.Section + slash + p.OutFile, p.RenderedContent.Bytes()) s.WritePublic(p.Section+slash+p.OutFile, p.RenderedContent.Bytes())
} }
} }
@ -409,7 +409,7 @@ func (s *Site) RenderLists() error {
if err != nil { if err != nil {
return err return err
} }
s.WritePublic(section + slash + "index.html", x.Bytes()) s.WritePublic(section+slash+"index.html", x.Bytes())
if a := s.Tmpl.Lookup("rss.xml"); a != nil { if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed // XML Feed
@ -417,7 +417,7 @@ func (s *Site) RenderLists() error {
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url) n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
y := s.NewXMLBuffer() y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n) s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
s.WritePublic(section + slash + "index.xml", y.Bytes()) s.WritePublic(section+slash+"index.xml", y.Bytes())
} }
} }
return nil return nil