From 274d324c8bb818a76ffc5c35afcc33f4cf9eb5c3 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Sun, 4 Aug 2013 19:02:15 -0700 Subject: [PATCH] Introduce unit testing for page.go --- hugolib/page.go | 5 ++--- hugolib/path_seperators_test.go | 22 +++++++++++++++++++--- hugolib/site.go | 6 +++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 3ec2b9005..b6a8498f7 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -78,10 +78,9 @@ func (p Pages) Limit(n int) Pages { return p[0:n] } func initializePage(filename string) (page Page) { page = Page{contentType: "", - File: File{FileName: filename, - Extension: "html"}, + File: File{FileName: filename, Extension: "html"}, + Node: Node{Keywords: make([]string, 10, 30)}, Params: make(map[string]interface{}), - Node: Node{Keywords: make([]string, 10, 30)}, Markup: "md"} page.Date, _ = time.Parse("20060102", "20080101") page.setSection() diff --git a/hugolib/path_seperators_test.go b/hugolib/path_seperators_test.go index 79f1dd46b..e86d5a216 100644 --- a/hugolib/path_seperators_test.go +++ b/hugolib/path_seperators_test.go @@ -1,17 +1,33 @@ package hugolib import ( - "testing" "path/filepath" + "testing" ) func TestDegenerateMissingFolderInPageFilename(t *testing.T) { p := NewPage(filepath.Join("foobar")) - if p != nil { - t.Fatalf("Creating a new Page without a subdirectory should result in nil page") + if p.Section != "" { + 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) { s := NewSite(&Config{}) p := NewPage(filepath.Join("sub", "foobar")) diff --git a/hugolib/site.go b/hugolib/site.go index 4b21679b8..c3a65f80e 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -297,7 +297,7 @@ func (s *Site) RenderPages() error { func (s *Site) WritePages() { 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 { 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 { // XML Feed @@ -417,7 +417,7 @@ func (s *Site) RenderLists() error { n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url) y := s.NewXMLBuffer() 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