From 599e6672f748c19bf6b11481736061be7a03c762 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Wed, 14 Aug 2013 15:22:57 -0700 Subject: [PATCH] Removing GetSection Using GetXXX is not idiomatic to Go. Also added a bunch of unit testing around this method. --- hugolib/node.go | 4 -- hugolib/page.go | 2 +- hugolib/page_test.go | 87 ++++++++++++++++++++++++++++++++++++++------ hugolib/site.go | 4 +- 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/hugolib/node.go b/hugolib/node.go index a04a34f25..918edeb5e 100644 --- a/hugolib/node.go +++ b/hugolib/node.go @@ -37,7 +37,3 @@ type UrlPath struct { Section string Path string } - -func (n *Node) GetSection() string { - return n.Section -} diff --git a/hugolib/page.go b/hugolib/page.go index c3aebb3f2..ef3b2f4f6 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -118,7 +118,7 @@ func (page *Page) Type() string { return page.contentType } page.guessSection() - if x := page.GetSection(); x != "" { + if x := page.Section; x != "" { return x } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 48bf1498d..d55edac62 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -2,7 +2,7 @@ package hugolib import ( "html/template" - "io" + "path/filepath" "strings" "testing" ) @@ -69,6 +69,28 @@ var SIMPLE_PAGE_JSON_COMPACT = ` Text ` +var SIMPLE_PAGE_NOLAYOUT = `--- +title: simple_no_layout +--- +No Layout called out` + +var SIMPLE_PAGE_LAYOUT_FOOBAR = `--- +title: simple layout foobar +layout: foobar +--- +Layout foobar` + +var SIMPLE_PAGE_TYPE_FOOBAR = `--- +type: foobar +--- +type foobar` + +var SIMPLE_PAGE_TYPE_LAYOUT = `--- +type: barfoo +layout: buzfoo +--- +type and layout set` + func checkError(t *testing.T, err error, expected string) { if err == nil { t.Fatalf("err is nil") @@ -133,16 +155,15 @@ func TestCreateNewPage(t *testing.T) { func TestCreatePage(t *testing.T) { var tests = []struct { - r io.Reader + r string }{ - {strings.NewReader(SIMPLE_PAGE_JSON)}, - {strings.NewReader(SIMPLE_PAGE_JSON_MULTIPLE)}, + {SIMPLE_PAGE_JSON}, + {SIMPLE_PAGE_JSON_MULTIPLE}, //{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)}, } for _, test := range tests { - _, err := ReadFrom(test.r, "page") - if err != nil { + if _, err := ReadFrom(strings.NewReader(test.r), "page"); err != nil { t.Errorf("Unable to parse page: %s", err) } } @@ -150,15 +171,15 @@ func TestCreatePage(t *testing.T) { func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) { var tests = []struct { - r io.Reader + r string err string }{ - {strings.NewReader(INVALID_FRONT_MATTER_SHORT_DELIM), "unable to match beginning front matter delimiter"}, - {strings.NewReader(INVALID_FRONT_MATTER_SHORT_DELIM_ENDING), "unable to match ending front matter delimiter"}, - {strings.NewReader(INVALID_FRONT_MATTER_MISSING), "unable to detect front matter"}, + {INVALID_FRONT_MATTER_SHORT_DELIM, "unable to match beginning front matter delimiter"}, + {INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "unable to match ending front matter delimiter"}, + {INVALID_FRONT_MATTER_MISSING, "unable to detect front matter"}, } for _, test := range tests { - _, err := ReadFrom(test.r, "invalid/front/matter/short/delim") + _, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim") checkError(t, err, test.err) } } @@ -169,3 +190,47 @@ func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) { t.Fatalf("Unable to parse front matter given leading whitespace: %s", err) } } + +func TestLayoutOverride(t *testing.T) { + var ( + path_content_one_dir = filepath.Join("content", "gub", "file1.md") + path_content_two_dir = filepath.Join("content", "dub", "sub", "file1.md") + path_content_no_dir = filepath.Join("content", "file1") + path_one_directory = filepath.Join("fub", "file1.md") + path_no_directory = filepath.Join("file1.md") + ) + tests := []struct { + content string + path string + expectedLayout string + }{ + {SIMPLE_PAGE_NOLAYOUT, path_content_two_dir, "sub/single.html"}, + {SIMPLE_PAGE_NOLAYOUT, path_content_one_dir, "gub/single.html"}, + {SIMPLE_PAGE_NOLAYOUT, path_content_no_dir, "page/single.html"}, + {SIMPLE_PAGE_NOLAYOUT, path_one_directory, "fub/single.html"}, + {SIMPLE_PAGE_NOLAYOUT, path_no_directory, "page/single.html"}, + {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_two_dir, "foobar"}, + {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_one_dir, "foobar"}, + {SIMPLE_PAGE_LAYOUT_FOOBAR, path_one_directory, "foobar"}, + {SIMPLE_PAGE_LAYOUT_FOOBAR, path_no_directory, "foobar"}, + {SIMPLE_PAGE_TYPE_FOOBAR, path_content_two_dir, "foobar/single.html"}, + {SIMPLE_PAGE_TYPE_FOOBAR, path_content_one_dir, "foobar/single.html"}, + {SIMPLE_PAGE_TYPE_FOOBAR, path_content_no_dir, "foobar/single.html"}, + {SIMPLE_PAGE_TYPE_FOOBAR, path_one_directory, "foobar/single.html"}, + {SIMPLE_PAGE_TYPE_FOOBAR, path_no_directory, "foobar/single.html"}, + {SIMPLE_PAGE_TYPE_LAYOUT, path_content_two_dir, "buzfoo"}, + {SIMPLE_PAGE_TYPE_LAYOUT, path_content_one_dir, "buzfoo"}, + {SIMPLE_PAGE_TYPE_LAYOUT, path_content_no_dir, "buzfoo"}, + {SIMPLE_PAGE_TYPE_LAYOUT, path_one_directory, "buzfoo"}, + {SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, "buzfoo"}, + } + for _, test := range tests { + p, err := ReadFrom(strings.NewReader(test.content), test.path) + if err != nil { + t.Fatalf("Unable to parse content:\n%s\n", test.content) + } + if p.Layout() != test.expectedLayout { + t.Errorf("Layout mismatch. Expected: %s, got: %s", test.expectedLayout, p.Layout()) + } + } +} diff --git a/hugolib/site.go b/hugolib/site.go index a64475216..0695a0be9 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -25,7 +25,6 @@ import ( "path/filepath" "strings" "time" - //"sync" ) var DefaultTimer = nitro.Initalize() @@ -381,8 +380,7 @@ func (s *Site) BuildSiteMeta() (err error) { } for i, p := range s.Pages { - sect := p.GetSection() - s.Sections.Add(sect, s.Pages[i]) + s.Sections.Add(p.Section, s.Pages[i]) } for k, _ := range s.Sections {