// Copyright 2015 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package hugolib import ( "fmt" "html/template" "path/filepath" "reflect" "sort" "strings" "testing" "time" "github.com/spf13/cast" "github.com/spf13/hugo/helpers" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) var emptyPage = "" const ( simplePage = "---\ntitle: Simple\n---\nSimple Page\n" invalidFrontMatterMissing = "This is a test" renderNoFrontmatter = "
This is a test" contentWithCommentedFrontmatter = "\n\n# Network configuration\n\n##\nSummary" contentWithCommentedTextFrontmatter = "\n\n# Network configuration\n\n##\nSummary" contentWithCommentedLongFrontmatter = "\n\n# Network configuration\n\n##\nSummary" contentWithCommentedLong2Frontmatter = "\n\n# Network configuration\n\n##\nSummary" invalidFrontmatterShortDelim = ` -- title: Short delim start --- Short Delim ` invalidFrontmatterShortDelimEnding = ` --- title: Short delim ending -- Short Delim ` invalidFrontmatterLadingWs = ` --- title: Leading WS --- Leading ` simplePageJSON = ` { "title": "spf13-vim 3.0 release and new website", "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", "tags": [ ".vimrc", "plugins", "spf13-vim", "VIm" ], "date": "2012-04-06", "categories": [ "Development", "VIM" ], "slug": "-spf13-vim-3-0-release-and-new-website-" } Content of the file goes Here ` simplePageJSONLoose = ` { "title": "spf13-vim 3.0 release and new website" "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." "tags": [ ".vimrc", "plugins", "spf13-vim", "VIm" ] "date": "2012-04-06" "categories": [ "Development" "VIM" ], "slug": "spf13-vim-3-0-release-and-new-website" } Content of the file goes Here ` simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" simplePageJSONMultiple = ` { "title": "foobar", "customData": { "foo": "bar" }, "date": "2012-08-06" } Some text ` simplePageNoLayout = `--- title: simple_no_layout --- No Layout called out` simplePageLayoutFoobar = `--- title: simple layout foobar layout: foobar --- Layout foobar` simplePageTypeFoobar = `--- type: foobar --- type foobar` simplePageTypeLayout = `--- type: barfoo layout: buzfoo --- type and layout set` simplePageWithSummaryDelimiter = `--- title: Simple --- Summary Next Line Some more text ` simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder = `--- title: Simple --- The [best static site generator][hugo].[^1] [hugo]: http://gohugo.io/ [^1]: Many people say so. ` simplePageWithShortcodeInSummary = `--- title: Simple --- Summary Next Line. {{Simple Page
\n")) checkPageSummary(t, p, "Simple Page") checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html", "_default/single.html", "theme/page/single.html", "theme/_default/single.html") checkTruncation(t, p, false, "simple short page") } settings := map[string]interface{}{ "contentDir": "mycontent", } testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePage) } func TestSplitSummaryAndContent(t *testing.T) { for i, this := range []struct { markup string content string expectedSummary string expectedContent string expectedContentWithoutSummary string }{ {"markdown", `Summary Same LineHUGOMORE42
Some more text
`, "Summary Same Line
", "Summary Same Line
\n\nSome more text
", "Some more text
"}, {"asciidoc", `sn
HUGOMORE42Some more text
sn
sn
Some more text
Some more text
Summary Next Line
HUGOMORE42Some more text
Summary Next Line
Summary Next Line
Some more text
Some more text
a
b
HUGOMORE42c
", "a
b
", "a
b
c
", "c
"}, {"markdown", "a
b
cHUGOMORE42
", "a
b
c
", "a
b
c
", ""}, {"markdown", "a
bHUGOMORE42
c
", "a
b
", "a
b
c
", "c
"}, {"markdown", "aHUGOMORE42
b
c
", "a
", "a
b
c
", "b
c
"}, {"markdown", " HUGOMORE42 ", "", "", ""}, {"markdown", "HUGOMORE42", "", "", ""}, {"markdown", "HUGOMORE42", "
", "
", ""}, {"markdown", "HUGOMORE42
", "", "
", "
"}, {"markdown", "\n\n
HUGOMORE42
\n", "", "", ""}, // Issue #2586 // Note: Hugo will not split mid-sentence but will look for the closest // paragraph end marker. This may be a change from Hugo 0.16, but it makes sense. {"markdown", `this is an example HUGOMORE42of the issue.
`, "this is an example of the issue.
", "this is an example of the issue.
", ""}, // Issue: #2538 {"markdown", fmt.Sprintf(`%s
HUGOMORE42%s
`, strings.Repeat("A", 10), strings.Repeat("B", 31)), fmt.Sprintf(`%s
`, strings.Repeat("A", 10)), fmt.Sprintf(`%s
%s
`, strings.Repeat("A", 10), strings.Repeat("B", 31)), fmt.Sprintf(`%s
`, strings.Repeat("B", 31)), }, } { sc, err := splitUserDefinedSummaryAndContent(this.markup, []byte(this.content)) require.NoError(t, err) require.NotNil(t, sc, fmt.Sprintf("[%d] Nil %s", i, this.markup)) require.Equal(t, this.expectedSummary, string(sc.summary), fmt.Sprintf("[%d] Summary markup %s", i, this.markup)) require.Equal(t, this.expectedContent, string(sc.content), fmt.Sprintf("[%d] Content markup %s", i, this.markup)) require.Equal(t, this.expectedContentWithoutSummary, string(sc.contentWithoutSummary), fmt.Sprintf("[%d] Content without summary, markup %s", i, this.markup)) } } func TestPageWithDelimiter(t *testing.T) { assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "Summary Next Line
\n\nSome more text
\n"), ext) checkPageSummary(t, p, normalizeExpected(ext, "Summary Next Line
"), ext) checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html", "_default/single.html", "theme/page/single.html", "theme/_default/single.html") checkTruncation(t, p, true, "page with summary delimiter") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiter) } // Issue #1076 func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) { s := newSiteFromSources("simple.md", simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder) if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } require.Len(t, s.RegularPages, 1) p := s.RegularPages[0] if p.Summary != template.HTML("The best static site generator.1\n
") { t.Fatalf("Got summary:\n%q", p.Summary) } if p.Content != template.HTML("The best static site generator.1\n
\nSummary Next Line. \n\n \n \n
Some more text
\n")) checkPageSummary(t, p, "Summary Next Line. . More text here. Some more text") checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html", "_default/single.html", "theme/page/single.html", "theme/_default/single.html") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithShortcodeInSummary) } func TestPageWithEmbeddedScriptTag(t *testing.T) { assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if ext == "ad" || ext == "rst" { // TOD(bep) return } checkPageContent(t, p, "\n", ext) } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithEmbeddedScript) } func TestPageWithAdditionalExtension(t *testing.T) { s := newSiteFromSources("simple.md", simplePageWithAdditionalExtension) if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } require.Len(t, s.RegularPages, 1) p := s.RegularPages[0] checkPageContent(t, p, "first line.
\nsecond line.
fourth line.
\n") } func TestTableOfContents(t *testing.T) { s := newSiteFromSources("tocpage.md", pageWithToC) if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } require.Len(t, s.RegularPages, 1) p := s.RegularPages[0] checkPageContent(t, p, "\n\nFor some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
\n\nI have no idea, of course, how long it took me to reach the limit of the plain,\nbut at last I entered the foothills, following a pretty little canyon upward\ntoward the mountains. Beside me frolicked a laughing brooklet, hurrying upon\nits noisy way down to the silent sea. In its quieter pools I discovered many\nsmall fish, of four-or five-pound weight I should imagine. In appearance,\nexcept as to size and color, they were not unlike the whale of our own seas. As\nI watched them playing about I discovered, not only that they suckled their\nyoung, but that at intervals they rose to the surface to breathe as well as to\nfeed upon certain grasses and a strange, scarlet lichen which grew upon the\nrocks just above the water line.
\n\nI remember I felt an extraordinary persuasion that I was being played with,\nthat presently, when I was upon the very verge of safety, this mysterious\ndeath–as swift as the passage of light–would leap after me from the pit about\nthe cylinder and strike me down. ## BB
\n\n“You’re a great Granser,” he cried delightedly, “always making believe them little marks mean something.”
\n") checkPageTOC(t, p, "") } func TestPageWithMoreTag(t *testing.T) { assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "Summary Same Line
\n\nSome more text
\n")) checkPageSummary(t, p, normalizeExpected(ext, "Summary Same Line
")) checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html", "_default/single.html", "theme/page/single.html", "theme/_default/single.html") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiterSameLine) } func TestPageWithDate(t *testing.T) { s := newSiteFromSources("simple.md", simplePageRFC3339Date) if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } require.Len(t, s.RegularPages, 1) p := s.RegularPages[0] d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z") checkPageDate(t, p, d) } func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) { testCommonResetState() assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if p.WordCount() != 8 { t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 8, p.WordCount()) } } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithAllCJKRunes) } func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) { settings := map[string]interface{}{"hasCJKLanguage": true} assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if p.WordCount() != 15 { t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 15, p.WordCount()) } } testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithAllCJKRunes) } func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) { settings := map[string]interface{}{"hasCJKLanguage": true} assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if p.WordCount() != 74 { t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 74, p.WordCount()) } if p.Summary != simplePageWithMainEnglishWithCJKRunesSummary { t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.plain, simplePageWithMainEnglishWithCJKRunesSummary, p.Summary) } } testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithMainEnglishWithCJKRunes) } func TestWordCountWithIsCJKLanguageFalse(t *testing.T) { testCommonResetState() viper.Set("hasCJKLanguage", true) assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if p.WordCount() != 75 { t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 74, p.WordCount()) } if p.Summary != simplePageWithIsCJKLanguageFalseSummary { t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.plain, simplePageWithIsCJKLanguageFalseSummary, p.Summary) } } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithIsCJKLanguageFalse) } func TestWordCount(t *testing.T) { assertFunc := func(t *testing.T, ext string, pages Pages) { p := pages[0] if p.WordCount() != 483 { t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 483, p.WordCount()) } if p.FuzzyWordCount() != 500 { t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 500, p.WordCount()) } if p.ReadingTime() != 3 { t.Fatalf("[%s] incorrect min read. expected %v, got %v", ext, 3, p.ReadingTime()) } checkTruncation(t, p, true, "long page") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithLongContent) } func TestCreatePage(t *testing.T) { var tests = []struct { r string }{ {simplePageJSON}, {simplePageJSONLoose}, {simplePageJSONMultiple}, //{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)}, } for _, test := range tests { p, _ := NewPage("page") if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil { t.Errorf("Unable to parse page: %s", err) } } } func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) { var tests = []struct { r string err string }{ {invalidFrontmatterShortDelimEnding, "unable to read frontmatter at filepos 45: EOF"}, } for _, test := range tests { p, _ := NewPage("invalid/front/matter/short/delim") _, err := p.ReadFrom(strings.NewReader(test.r)) checkError(t, err, test.err) } } func TestShouldRenderContent(t *testing.T) { var tests = []struct { text string render bool }{ {invalidFrontMatterMissing, true}, // TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown. {invalidFrontmatterShortDelim, true}, {renderNoFrontmatter, false}, {contentWithCommentedFrontmatter, true}, {contentWithCommentedTextFrontmatter, true}, {contentWithCommentedLongFrontmatter, false}, {contentWithCommentedLong2Frontmatter, true}, } for _, test := range tests { p, _ := NewPage("render/front/matter") _, err := p.ReadFrom(strings.NewReader(test.text)) p = pageMust(p, err) if p.IsRenderable() != test.render { t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable()) } } } // Issue #768 func TestCalendarParamsVariants(t *testing.T) { pageJSON, _ := NewPage("test/fileJSON.md") _, _ = pageJSON.ReadFrom(strings.NewReader(pageWithCalendarJSONFrontmatter)) pageYAML, _ := NewPage("test/fileYAML.md") _, _ = pageYAML.ReadFrom(strings.NewReader(pageWithCalendarYAMLFrontmatter)) pageTOML, _ := NewPage("test/fileTOML.md") _, _ = pageTOML.ReadFrom(strings.NewReader(pageWithCalendarTOMLFrontmatter)) assert.True(t, compareObjects(pageJSON.Params, pageYAML.Params)) assert.True(t, compareObjects(pageJSON.Params, pageTOML.Params)) } func TestDifferentFrontMatterVarTypes(t *testing.T) { page, _ := NewPage("test/file1.md") _, _ = page.ReadFrom(strings.NewReader(pageWithVariousFrontmatterTypes)) dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") if page.GetParam("a_string") != "bar" { t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string")) } if page.GetParam("an_integer") != 1 { t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer")) } if page.GetParam("a_float") != 1.3 { t.Errorf("frontmatter not handling floats correctly should be %f, got: %s", 1.3, page.GetParam("a_float")) } if page.GetParam("a_bool") != false { t.Errorf("frontmatter not handling bools correctly should be %t, got: %s", false, page.GetParam("a_bool")) } if page.GetParam("a_date") != dateval { t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date")) } param := page.GetParam("a_table") if param == nil { t.Errorf("frontmatter not handling tables correctly should be type of %v, got: type of %v", reflect.TypeOf(page.Params["a_table"]), reflect.TypeOf(param)) } if cast.ToStringMap(param)["a_key"] != "a_value" { t.Errorf("frontmatter not handling values inside a table correctly should be %s, got: %s", "a_value", cast.ToStringMap(page.Params["a_table"])["a_key"]) } } func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) { p, _ := NewPage("invalid/front/matter/leading/ws") _, err := p.ReadFrom(strings.NewReader(invalidFrontmatterLadingWs)) if err != nil { t.Fatalf("Unable to parse front matter given leading whitespace: %s", err) } } func TestSectionEvaluation(t *testing.T) { page, _ := NewPage(filepath.FromSlash("blue/file1.md")) page.ReadFrom(strings.NewReader(simplePage)) if page.Section() != "blue" { t.Errorf("Section should be %s, got: %s", "blue", page.Section()) } } func L(s ...string) []string { return s } func TestLayoutOverride(t *testing.T) { var ( pathContentTwoDir = filepath.Join("content", "dub", "sub", "file1.md") pathContentOneDir = filepath.Join("content", "gub", "file1.md") pathContentNoDir = filepath.Join("content", "file1") pathOneDirectory = filepath.Join("fub", "file1.md") pathNoDirectory = filepath.Join("file1.md") ) tests := []struct { content string path string expectedLayout []string }{ {simplePageNoLayout, pathContentTwoDir, L("dub/single.html", "_default/single.html")}, {simplePageNoLayout, pathContentOneDir, L("gub/single.html", "_default/single.html")}, {simplePageNoLayout, pathContentNoDir, L("page/single.html", "_default/single.html")}, {simplePageNoLayout, pathOneDirectory, L("fub/single.html", "_default/single.html")}, {simplePageNoLayout, pathNoDirectory, L("page/single.html", "_default/single.html")}, {simplePageLayoutFoobar, pathContentTwoDir, L("dub/foobar.html", "_default/foobar.html")}, {simplePageLayoutFoobar, pathContentOneDir, L("gub/foobar.html", "_default/foobar.html")}, {simplePageLayoutFoobar, pathOneDirectory, L("fub/foobar.html", "_default/foobar.html")}, {simplePageLayoutFoobar, pathNoDirectory, L("page/foobar.html", "_default/foobar.html")}, {simplePageTypeFoobar, pathContentTwoDir, L("foobar/single.html", "_default/single.html")}, {simplePageTypeFoobar, pathContentOneDir, L("foobar/single.html", "_default/single.html")}, {simplePageTypeFoobar, pathContentNoDir, L("foobar/single.html", "_default/single.html")}, {simplePageTypeFoobar, pathOneDirectory, L("foobar/single.html", "_default/single.html")}, {simplePageTypeFoobar, pathNoDirectory, L("foobar/single.html", "_default/single.html")}, {simplePageTypeLayout, pathContentTwoDir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {simplePageTypeLayout, pathContentOneDir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {simplePageTypeLayout, pathContentNoDir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {simplePageTypeLayout, pathOneDirectory, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {simplePageTypeLayout, pathNoDirectory, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, } for _, test := range tests { p, _ := NewPage(test.path) _, err := p.ReadFrom(strings.NewReader(test.content)) if err != nil { t.Fatalf("Unable to parse content:\n%s\n", test.content) } for _, y := range test.expectedLayout { test.expectedLayout = append(test.expectedLayout, "theme/"+y) } if !listEqual(p.layouts(), test.expectedLayout) { t.Errorf("Layout mismatch. Expected: %s, got: %s", test.expectedLayout, p.layouts()) } } } func TestSliceToLower(t *testing.T) { tests := []struct { value []string expected []string }{ {[]string{"a", "b", "c"}, []string{"a", "b", "c"}}, {[]string{"a", "B", "c"}, []string{"a", "b", "c"}}, {[]string{"A", "B", "C"}, []string{"a", "b", "c"}}, } for _, test := range tests { res := helpers.SliceToLower(test.value) for i, val := range res { if val != test.expected[i] { t.Errorf("Case mismatch. Expected %s, got %s", test.expected[i], res[i]) } } } } func TestPagePaths(t *testing.T) { testCommonResetState() viper.Set("defaultExtension", "html") siteParmalinksSetting := PermalinkOverrides{ "post": ":year/:month/:day/:title/", } tests := []struct { content string path string hasPermalink bool expected string }{ {simplePage, "content/post/x.md", false, "content/post/x.html"}, {simplePageWithURL, "content/post/x.md", false, "simple/url/index.html"}, {simplePageWithSlug, "content/post/x.md", false, "content/post/simple-slug.html"}, {simplePageWithDate, "content/post/x.md", true, "2013/10/15/simple/index.html"}, {UTF8Page, "content/post/x.md", false, "content/post/x.html"}, {UTF8PageWithURL, "content/post/x.md", false, "ラーメン/url/index.html"}, {UTF8PageWithSlug, "content/post/x.md", false, "content/post/ラーメン-slug.html"}, {UTF8PageWithDate, "content/post/x.md", true, "2013/10/15/ラーメン/index.html"}, } for _, test := range tests { p, _ := NewPageFrom(strings.NewReader(test.content), filepath.FromSlash(test.path)) info := newSiteInfo(siteBuilderCfg{language: helpers.NewDefaultLanguage()}) p.Site = &info if test.hasPermalink { p.Site.Permalinks = siteParmalinksSetting } expectedTargetPath := filepath.FromSlash(test.expected) expectedFullFilePath := filepath.FromSlash(test.path) if p.TargetPath() != expectedTargetPath { t.Errorf("%s => TargetPath expected: '%s', got: '%s'", test.content, expectedTargetPath, p.TargetPath()) } if p.FullFilePath() != expectedFullFilePath { t.Errorf("%s => FullFilePath expected: '%s', got: '%s'", test.content, expectedFullFilePath, p.FullFilePath()) } } } var pageWithDraftAndPublished = `--- title: broken published: false draft: true --- some content ` func TestDraftAndPublishedFrontMatterError(t *testing.T) { _, err := NewPageFrom(strings.NewReader(pageWithDraftAndPublished), "content/post/broken.md") if err != ErrHasDraftAndPublished { t.Errorf("expected ErrHasDraftAndPublished, was %#v", err) } } var pagesWithPublishedFalse = `--- title: okay published: false --- some content ` var pageWithPublishedTrue = `--- title: okay published: true --- some content ` func TestPublishedFrontMatter(t *testing.T) { p, err := NewPageFrom(strings.NewReader(pagesWithPublishedFalse), "content/post/broken.md") if err != nil { t.Fatalf("err during parse: %s", err) } if !p.Draft { t.Errorf("expected true, got %t", p.Draft) } p, err = NewPageFrom(strings.NewReader(pageWithPublishedTrue), "content/post/broken.md") if err != nil { t.Fatalf("err during parse: %s", err) } if p.Draft { t.Errorf("expected false, got %t", p.Draft) } } var pagesDraftTemplate = []string{`--- title: "okay" draft: %t --- some content `, `+++ title = "okay" draft = %t +++ some content `, } func TestDraft(t *testing.T) { for _, draft := range []bool{true, false} { for i, templ := range pagesDraftTemplate { pageContent := fmt.Sprintf(templ, draft) p, err := NewPageFrom(strings.NewReader(pageContent), "content/post/broken.md") if err != nil { t.Fatalf("err during parse: %s", err) } if p.Draft != draft { t.Errorf("[%d] expected %t, got %t", i, draft, p.Draft) } } } } func TestPageSimpleMethods(t *testing.T) { for i, this := range []struct { assertFunc func(p *Page) bool }{ {func(p *Page) bool { return !p.IsNode() }}, {func(p *Page) bool { return p.IsPage() }}, {func(p *Page) bool { return p.Plain() == "Do Be Do Be Do" }}, {func(p *Page) bool { return strings.Join(p.PlainWords(), " ") == "Do Be Do Be Do" }}, } { p, _ := NewPage("Test") p.Content = "