shithub: hugo

Download patch

ref: 274d324c8bb818a76ffc5c35afcc33f4cf9eb5c3
parent: fa55cd98575185d1e86b3de48796aeb424f8933a
author: Noah Campbell <[email protected]>
date: Sun Aug 4 15:02:15 EDT 2013

Introduce unit testing for page.go

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -78,10 +78,9 @@
 
 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()
--- a/hugolib/path_seperators_test.go
+++ b/hugolib/path_seperators_test.go
@@ -1,16 +1,32 @@
 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{})
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -297,7 +297,7 @@
 
 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 @@
 		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 @@
 			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