renaming MinRead to ReadingTime and adding to documentation.

This commit is contained in:
spf13 2014-01-21 17:12:59 -05:00
parent 50a7f97a62
commit 3851117c25
2 changed files with 707 additions and 707 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,34 +1,34 @@
package hugolib package hugolib
import ( import (
"html/template" "html/template"
"path" "path"
"strings" "strings"
"testing" "testing"
"time" "time"
) )
var EMPTY_PAGE = "" var EMPTY_PAGE = ""
const ( const (
SIMPLE_PAGE = "---\ntitle: Simple\n---\nSimple Page\n" SIMPLE_PAGE = "---\ntitle: Simple\n---\nSimple Page\n"
INVALID_FRONT_MATTER_MISSING = "This is a test" INVALID_FRONT_MATTER_MISSING = "This is a test"
RENDER_NO_FRONT_MATTER = "<!doctype><html><head></head><body>This is a test</body></html>" RENDER_NO_FRONT_MATTER = "<!doctype><html><head></head><body>This is a test</body></html>"
INVALID_FRONT_MATTER_SHORT_DELIM = ` INVALID_FRONT_MATTER_SHORT_DELIM = `
-- --
title: Short delim start title: Short delim start
--- ---
Short Delim Short Delim
` `
INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = ` INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = `
--- ---
title: Short delim ending title: Short delim ending
-- --
Short Delim Short Delim
` `
INVALID_FRONT_MATTER_LEADING_WS = ` INVALID_FRONT_MATTER_LEADING_WS = `
--- ---
title: Leading WS title: Leading WS
@ -36,7 +36,7 @@ title: Leading WS
Leading Leading
` `
SIMPLE_PAGE_JSON = ` SIMPLE_PAGE_JSON = `
{ {
"title": "spf13-vim 3.0 release and new website", "title": "spf13-vim 3.0 release and new website",
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.",
@ -51,7 +51,7 @@ Leading
Content of the file goes Here Content of the file goes Here
` `
SIMPLE_PAGE_JSON_LOOSE = ` SIMPLE_PAGE_JSON_LOOSE = `
{ {
"title": "spf13-vim 3.0 release and new website" "title": "spf13-vim 3.0 release and new website"
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim."
@ -66,8 +66,8 @@ Content of the file goes Here
Content of the file goes Here Content of the file goes Here
` `
SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
SIMPLE_PAGE_JSON_MULTIPLE = ` SIMPLE_PAGE_JSON_MULTIPLE = `
{ {
"title": "foobar", "title": "foobar",
"customData": { "foo": "bar" }, "customData": { "foo": "bar" },
@ -76,34 +76,34 @@ Content of the file goes Here
Some text Some text
` `
SIMPLE_PAGE_JSON_COMPACT = ` SIMPLE_PAGE_JSON_COMPACT = `
{"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"} {"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"}
Text Text
` `
SIMPLE_PAGE_NOLAYOUT = `--- SIMPLE_PAGE_NOLAYOUT = `---
title: simple_no_layout title: simple_no_layout
--- ---
No Layout called out` No Layout called out`
SIMPLE_PAGE_LAYOUT_FOOBAR = `--- SIMPLE_PAGE_LAYOUT_FOOBAR = `---
title: simple layout foobar title: simple layout foobar
layout: foobar layout: foobar
--- ---
Layout foobar` Layout foobar`
SIMPLE_PAGE_TYPE_FOOBAR = `--- SIMPLE_PAGE_TYPE_FOOBAR = `---
type: foobar type: foobar
--- ---
type foobar` type foobar`
SIMPLE_PAGE_TYPE_LAYOUT = `--- SIMPLE_PAGE_TYPE_LAYOUT = `---
type: barfoo type: barfoo
layout: buzfoo layout: buzfoo
--- ---
type and layout set` type and layout set`
SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `--- SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `---
title: Simple title: Simple
--- ---
Summary Next Line Summary Next Line
@ -111,7 +111,7 @@ Summary Next Line
<!--more--> <!--more-->
Some more text Some more text
` `
SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY = `--- SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY = `---
title: Simple title: Simple
--- ---
Summary Next Line. {{% img src="/not/real" %}}. Summary Next Line. {{% img src="/not/real" %}}.
@ -120,7 +120,7 @@ More text here.
Some more text Some more text
` `
SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE = `--- SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE = `---
title: Simple title: Simple
--- ---
Summary Same Line<!--more--> Summary Same Line<!--more-->
@ -128,7 +128,7 @@ Summary Same Line<!--more-->
Some more text Some more text
` `
SIMPLE_PAGE_WITH_LONG_CONTENT = `--- SIMPLE_PAGE_WITH_LONG_CONTENT = `---
title: Simple title: Simple
--- ---
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@ -150,310 +150,310 @@ a_date = 1979-05-27T07:32:00Z
Front Matter with various frontmatter types` Front Matter with various frontmatter types`
func checkError(t *testing.T, err error, expected string) { func checkError(t *testing.T, err error, expected string) {
if err == nil { if err == nil {
t.Fatalf("err is nil. Expected: %s", expected) t.Fatalf("err is nil. Expected: %s", expected)
} }
if err.Error() != expected { if err.Error() != expected {
t.Errorf("err.Error() returned: '%s'. Expected: '%s'", err.Error(), expected) t.Errorf("err.Error() returned: '%s'. Expected: '%s'", err.Error(), expected)
} }
} }
func TestDegenerateEmptyPageZeroLengthName(t *testing.T) { func TestDegenerateEmptyPageZeroLengthName(t *testing.T) {
_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "") _, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "")
if err == nil { if err == nil {
t.Fatalf("A zero length page name must return an error") t.Fatalf("A zero length page name must return an error")
} }
checkError(t, err, "Zero length page name") checkError(t, err, "Zero length page name")
} }
func TestDegenerateEmptyPage(t *testing.T) { func TestDegenerateEmptyPage(t *testing.T) {
_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "test") _, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "test")
if err != nil { if err != nil {
t.Fatalf("Empty files should not trigger an error. Should be able to touch a file while watching without erroring out.") t.Fatalf("Empty files should not trigger an error. Should be able to touch a file while watching without erroring out.")
} }
//checkError(t, err, "EOF") //checkError(t, err, "EOF")
} }
func checkPageTitle(t *testing.T, page *Page, title string) { func checkPageTitle(t *testing.T, page *Page, title string) {
if page.Title != title { if page.Title != title {
t.Fatalf("Page title is: %s. Expected %s", page.Title, title) t.Fatalf("Page title is: %s. Expected %s", page.Title, title)
} }
} }
func checkPageContent(t *testing.T, page *Page, content string) { func checkPageContent(t *testing.T, page *Page, content string) {
if page.Content != template.HTML(content) { if page.Content != template.HTML(content) {
t.Fatalf("Page content mismatch\nexp: %q\ngot: %q", content, page.Content) t.Fatalf("Page content mismatch\nexp: %q\ngot: %q", content, page.Content)
} }
} }
func checkPageSummary(t *testing.T, page *Page, summary string) { func checkPageSummary(t *testing.T, page *Page, summary string) {
if page.Summary != template.HTML(summary) { if page.Summary != template.HTML(summary) {
t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary) t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary)
} }
} }
func checkPageType(t *testing.T, page *Page, pageType string) { func checkPageType(t *testing.T, page *Page, pageType string) {
if page.Type() != pageType { if page.Type() != pageType {
t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType) t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType)
} }
} }
func checkPageLayout(t *testing.T, page *Page, layout ...string) { func checkPageLayout(t *testing.T, page *Page, layout ...string) {
if !listEqual(page.Layout(), layout) { if !listEqual(page.Layout(), layout) {
t.Fatalf("Page layout is: %s. Expected: %s", page.Layout(), layout) t.Fatalf("Page layout is: %s. Expected: %s", page.Layout(), layout)
} }
} }
func checkPageDate(t *testing.T, page *Page, time time.Time) { func checkPageDate(t *testing.T, page *Page, time time.Time) {
if page.Date != time { if page.Date != time {
t.Fatalf("Page date is: %s. Expected: %s", page.Date, time) t.Fatalf("Page date is: %s. Expected: %s", page.Date, time)
} }
} }
func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) { func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) {
if page.Summary == "" { if page.Summary == "" {
t.Fatal("page has no summary, can not check truncation") t.Fatal("page has no summary, can not check truncation")
} }
if page.Truncated != shouldBe { if page.Truncated != shouldBe {
if shouldBe { if shouldBe {
t.Fatalf("page wasn't truncated: %s", msg) t.Fatalf("page wasn't truncated: %s", msg)
} else { } else {
t.Fatalf("page was truncated: %s", msg) t.Fatalf("page was truncated: %s", msg)
} }
} }
} }
func TestCreateNewPage(t *testing.T) { func TestCreateNewPage(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple.md") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
checkPageTitle(t, p, "Simple") checkPageTitle(t, p, "Simple")
checkPageContent(t, p, "<p>Simple Page</p>\n") checkPageContent(t, p, "<p>Simple Page</p>\n")
checkPageSummary(t, p, "Simple Page") checkPageSummary(t, p, "Simple Page")
checkPageType(t, p, "page") checkPageType(t, p, "page")
checkPageLayout(t, p, "page/single.html", "single.html") checkPageLayout(t, p, "page/single.html", "single.html")
checkTruncation(t, p, false, "simple short page") checkTruncation(t, p, false, "simple short page")
} }
func TestPageWithDelimiter(t *testing.T) { func TestPageWithDelimiter(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple.md") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
checkPageTitle(t, p, "Simple") checkPageTitle(t, p, "Simple")
checkPageContent(t, p, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n") checkPageContent(t, p, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n")
checkPageSummary(t, p, "<p>Summary Next Line</p>\n") checkPageSummary(t, p, "<p>Summary Next Line</p>\n")
checkPageType(t, p, "page") checkPageType(t, p, "page")
checkPageLayout(t, p, "page/single.html", "single.html") checkPageLayout(t, p, "page/single.html", "single.html")
checkTruncation(t, p, true, "page with summary delimiter") checkTruncation(t, p, true, "page with summary delimiter")
} }
func TestPageWithShortCodeInSummary(t *testing.T) { func TestPageWithShortCodeInSummary(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY), "simple.md") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY), "simple.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
checkPageTitle(t, p, "Simple") checkPageTitle(t, p, "Simple")
checkPageContent(t, p, "<p>Summary Next Line. {{% img src=&ldquo;/not/real&rdquo; %}}.\nMore text here.</p>\n\n<p>Some more text</p>\n") checkPageContent(t, p, "<p>Summary Next Line. {{% img src=&ldquo;/not/real&rdquo; %}}.\nMore text here.</p>\n\n<p>Some more text</p>\n")
checkPageSummary(t, p, "Summary Next Line. . More text here. Some more text") checkPageSummary(t, p, "Summary Next Line. . More text here. Some more text")
checkPageType(t, p, "page") checkPageType(t, p, "page")
checkPageLayout(t, p, "page/single.html", "single.html") checkPageLayout(t, p, "page/single.html", "single.html")
} }
func TestPageWithMoreTag(t *testing.T) { func TestPageWithMoreTag(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
checkPageTitle(t, p, "Simple") checkPageTitle(t, p, "Simple")
checkPageContent(t, p, "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n") checkPageContent(t, p, "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n")
checkPageSummary(t, p, "<p>Summary Same Line</p>\n") checkPageSummary(t, p, "<p>Summary Same Line</p>\n")
checkPageType(t, p, "page") checkPageType(t, p, "page")
checkPageLayout(t, p, "page/single.html", "single.html") checkPageLayout(t, p, "page/single.html", "single.html")
} }
func TestPageWithDate(t *testing.T) { func TestPageWithDate(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE), "simple") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE), "simple")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
d, err := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z") d, err := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z")
if err != nil { if err != nil {
t.Fatalf("Unable to prase page.") t.Fatalf("Unable to prase page.")
} }
checkPageDate(t, p, d) checkPageDate(t, p, d)
} }
func TestWordCount(t *testing.T) { func TestWordCount(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT), "simple.md") p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT), "simple.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
} }
if p.WordCount != 483 { if p.WordCount != 483 {
t.Fatalf("incorrect word count. expected %v, got %v", 483, p.WordCount) t.Fatalf("incorrect word count. expected %v, got %v", 483, p.WordCount)
} }
if p.FuzzyWordCount != 500 { if p.FuzzyWordCount != 500 {
t.Fatalf("incorrect word count. expected %v, got %v", 500, p.WordCount) t.Fatalf("incorrect word count. expected %v, got %v", 500, p.WordCount)
} }
if p.MinRead != 3 { if p.ReadingTime != 3 {
t.Fatalf("incorrect min read. expected %v, got %v", 3, p.MinRead) t.Fatalf("incorrect min read. expected %v, got %v", 3, p.ReadingTime)
} }
checkTruncation(t, p, true, "long page") checkTruncation(t, p, true, "long page")
} }
func TestCreatePage(t *testing.T) { func TestCreatePage(t *testing.T) {
var tests = []struct { var tests = []struct {
r string r string
}{ }{
{SIMPLE_PAGE_JSON}, {SIMPLE_PAGE_JSON},
{SIMPLE_PAGE_JSON_LOOSE}, {SIMPLE_PAGE_JSON_LOOSE},
{SIMPLE_PAGE_JSON_MULTIPLE}, {SIMPLE_PAGE_JSON_MULTIPLE},
//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)}, //{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
} }
for _, test := range tests { for _, test := range tests {
if _, err := ReadFrom(strings.NewReader(test.r), "page"); err != nil { if _, err := ReadFrom(strings.NewReader(test.r), "page"); err != nil {
t.Errorf("Unable to parse page: %s", err) t.Errorf("Unable to parse page: %s", err)
} }
} }
} }
func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) { func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
var tests = []struct { var tests = []struct {
r string r string
err string err string
}{ }{
{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "Unable to read frontmatter at filepos 45: EOF"}, {INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "Unable to read frontmatter at filepos 45: EOF"},
} }
for _, test := range tests { for _, test := range tests {
_, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim") _, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim")
checkError(t, err, test.err) checkError(t, err, test.err)
} }
} }
func TestShouldRenderContent(t *testing.T) { func TestShouldRenderContent(t *testing.T) {
var tests = []struct { var tests = []struct {
text string text string
render bool render bool
}{ }{
{INVALID_FRONT_MATTER_MISSING, true}, {INVALID_FRONT_MATTER_MISSING, true},
// TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown. // TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown.
{INVALID_FRONT_MATTER_SHORT_DELIM, true}, {INVALID_FRONT_MATTER_SHORT_DELIM, true},
{RENDER_NO_FRONT_MATTER, false}, {RENDER_NO_FRONT_MATTER, false},
} }
for _, test := range tests { for _, test := range tests {
p := pageMust(ReadFrom(strings.NewReader(test.text), "render/front/matter")) p := pageMust(ReadFrom(strings.NewReader(test.text), "render/front/matter"))
if p.IsRenderable() != test.render { if p.IsRenderable() != test.render {
t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable()) t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable())
} }
} }
} }
func TestDifferentFrontMatterVarTypes(t *testing.T) { func TestDifferentFrontMatterVarTypes(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES), "test/file1.md") page, _ := ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES), "test/file1.md")
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
if page.GetParam("a_string") != "bar" { if page.GetParam("a_string") != "bar" {
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string")) t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string"))
} }
if page.GetParam("an_integer") != 1 { if page.GetParam("an_integer") != 1 {
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer")) t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer"))
} }
if page.GetParam("a_float") != 1.3 { if page.GetParam("a_float") != 1.3 {
t.Errorf("frontmatter not handling floats correctly should be %s, got: %s", 1.3, page.GetParam("a_float")) t.Errorf("frontmatter not handling floats correctly should be %s, got: %s", 1.3, page.GetParam("a_float"))
} }
if page.GetParam("a_date") != dateval { if page.GetParam("a_date") != dateval {
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date")) t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date"))
} }
} }
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) { func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws") _, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
if err != nil { if err != nil {
t.Fatalf("Unable to parse front matter given leading whitespace: %s", err) t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)
} }
} }
func TestSectionEvaluation(t *testing.T) { func TestSectionEvaluation(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(SIMPLE_PAGE), "blue/file1.md") page, _ := ReadFrom(strings.NewReader(SIMPLE_PAGE), "blue/file1.md")
if page.Section != "blue" { if page.Section != "blue" {
t.Errorf("Section should be %s, got: %s", "blue", page.Section) t.Errorf("Section should be %s, got: %s", "blue", page.Section)
} }
} }
func L(s ...string) []string { func L(s ...string) []string {
return s return s
} }
func TestLayoutOverride(t *testing.T) { func TestLayoutOverride(t *testing.T) {
var ( var (
path_content_two_dir = path.Join("content", "dub", "sub", "file1.md") path_content_two_dir = path.Join("content", "dub", "sub", "file1.md")
path_content_one_dir = path.Join("content", "gub", "file1.md") path_content_one_dir = path.Join("content", "gub", "file1.md")
path_content_no_dir = path.Join("content", "file1") path_content_no_dir = path.Join("content", "file1")
path_one_directory = path.Join("fub", "file1.md") path_one_directory = path.Join("fub", "file1.md")
path_no_directory = path.Join("file1.md") path_no_directory = path.Join("file1.md")
) )
tests := []struct { tests := []struct {
content string content string
path string path string
expectedLayout []string expectedLayout []string
}{ }{
{SIMPLE_PAGE_NOLAYOUT, path_content_two_dir, L("dub/sub/single.html", "dub/single.html", "single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_content_two_dir, L("dub/sub/single.html", "dub/single.html", "single.html")},
{SIMPLE_PAGE_NOLAYOUT, path_content_one_dir, L("gub/single.html", "single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_content_one_dir, L("gub/single.html", "single.html")},
{SIMPLE_PAGE_NOLAYOUT, path_content_no_dir, L("page/single.html", "single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_content_no_dir, L("page/single.html", "single.html")},
{SIMPLE_PAGE_NOLAYOUT, path_one_directory, L("fub/single.html", "single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_one_directory, L("fub/single.html", "single.html")},
{SIMPLE_PAGE_NOLAYOUT, path_no_directory, L("page/single.html", "single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_no_directory, L("page/single.html", "single.html")},
{SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_two_dir, L("dub/sub/foobar.html", "dub/foobar.html", "foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_two_dir, L("dub/sub/foobar.html", "dub/foobar.html", "foobar.html")},
{SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_one_dir, L("gub/foobar.html", "foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_one_dir, L("gub/foobar.html", "foobar.html")},
{SIMPLE_PAGE_LAYOUT_FOOBAR, path_one_directory, L("fub/foobar.html", "foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_one_directory, L("fub/foobar.html", "foobar.html")},
{SIMPLE_PAGE_LAYOUT_FOOBAR, path_no_directory, L("page/foobar.html", "foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_no_directory, L("page/foobar.html", "foobar.html")},
{SIMPLE_PAGE_TYPE_FOOBAR, path_content_two_dir, L("foobar/single.html", "single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_two_dir, L("foobar/single.html", "single.html")},
{SIMPLE_PAGE_TYPE_FOOBAR, path_content_one_dir, L("foobar/single.html", "single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_one_dir, L("foobar/single.html", "single.html")},
{SIMPLE_PAGE_TYPE_FOOBAR, path_content_no_dir, L("foobar/single.html", "single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_no_dir, L("foobar/single.html", "single.html")},
{SIMPLE_PAGE_TYPE_FOOBAR, path_one_directory, L("foobar/single.html", "single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_one_directory, L("foobar/single.html", "single.html")},
{SIMPLE_PAGE_TYPE_FOOBAR, path_no_directory, L("foobar/single.html", "single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_no_directory, L("foobar/single.html", "single.html")},
{SIMPLE_PAGE_TYPE_LAYOUT, path_content_two_dir, L("barfoo/buzfoo.html", "buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_two_dir, L("barfoo/buzfoo.html", "buzfoo.html")},
{SIMPLE_PAGE_TYPE_LAYOUT, path_content_one_dir, L("barfoo/buzfoo.html", "buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_one_dir, L("barfoo/buzfoo.html", "buzfoo.html")},
{SIMPLE_PAGE_TYPE_LAYOUT, path_content_no_dir, L("barfoo/buzfoo.html", "buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_no_dir, L("barfoo/buzfoo.html", "buzfoo.html")},
{SIMPLE_PAGE_TYPE_LAYOUT, path_one_directory, L("barfoo/buzfoo.html", "buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_one_directory, L("barfoo/buzfoo.html", "buzfoo.html")},
{SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, L("barfoo/buzfoo.html", "buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, L("barfoo/buzfoo.html", "buzfoo.html")},
} }
for _, test := range tests { for _, test := range tests {
p, err := ReadFrom(strings.NewReader(test.content), test.path) p, err := ReadFrom(strings.NewReader(test.content), test.path)
if err != nil { if err != nil {
t.Fatalf("Unable to parse content:\n%s\n", test.content) t.Fatalf("Unable to parse content:\n%s\n", test.content)
} }
if !listEqual(p.Layout(), test.expectedLayout) { if !listEqual(p.Layout(), test.expectedLayout) {
t.Errorf("Layout mismatch. Expected: %s, got: %s", test.expectedLayout, p.Layout()) t.Errorf("Layout mismatch. Expected: %s, got: %s", test.expectedLayout, p.Layout())
} }
} }
} }
func listEqual(left, right []string) bool { func listEqual(left, right []string) bool {
if len(left) != len(right) { if len(left) != len(right) {
return false return false
} }
for i := range left { for i := range left {
if left[i] != right[i] { if left[i] != right[i] {
return false return false
} }
} }
return true return true
} }