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" ) var EMPTY_PAGE = "" const ( SIMPLE_PAGE = "---\ntitle: Simple\n---\nSimple Page\n" INVALID_FRONT_MATTER_MISSING = "This is a test" RENDER_NO_FRONT_MATTER = "
This is a test" CONTENT_WITH_COMMENTED_FM = "\n\n# Network configuration\n\n##\nSummary" CONTENT_WITH_COMMENTED_TEXT_FM = "\n\n# Network configuration\n\n##\nSummary" CONTENT_WITH_COMMENTED_LONG_FM = "\n\n# Network configuration\n\n##\nSummary" CONTENT_WITH_COMMENTED_LONG2_FM = "\n\n# Network configuration\n\n##\nSummary" INVALID_FRONT_MATTER_SHORT_DELIM = ` -- title: Short delim start --- Short Delim ` INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = ` --- title: Short delim ending -- Short Delim ` INVALID_FRONT_MATTER_LEADING_WS = ` --- title: Leading WS --- Leading ` SIMPLE_PAGE_JSON = ` { "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 ` SIMPLE_PAGE_JSON_LOOSE = ` { "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 ` SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" SIMPLE_PAGE_JSON_MULTIPLE = ` { "title": "foobar", "customData": { "foo": "bar" }, "date": "2012-08-06" } Some text ` SIMPLE_PAGE_JSON_COMPACT = ` {"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"} Text ` SIMPLE_PAGE_NOLAYOUT = `--- title: simple_no_layout --- No Layout called out` SIMPLE_PAGE_LAYOUT_FOOBAR = `--- title: simple layout foobar layout: foobar --- Layout foobar` SIMPLE_PAGE_TYPE_FOOBAR = `--- type: foobar --- type foobar` SIMPLE_PAGE_TYPE_LAYOUT = `--- type: barfoo layout: buzfoo --- type and layout set` SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `--- title: Simple --- Summary Next Line Some more text ` SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY = `--- 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") } func TestPageWithDelimiter(t *testing.T) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER)) p.Convert() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } checkPageTitle(t, p, "Simple") checkPageContent(t, p, "Summary Next Line
\n\nSome more text
\n") checkPageSummary(t, p, "Summary Next Line
\n") 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") } func TestPageWithShortCodeInSummary(t *testing.T) { s := new(Site) s.prepTemplates() p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY)) if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } p.Convert() checkPageTitle(t, p, "Simple") checkPageContent(t, p, "Summary 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") } func TestPageWithEmbeddedScriptTag(t *testing.T) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_EMBEDDED_SCRIPT)) p.Convert() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } checkPageContent(t, p, "\n") } func TestPageWithAdditionalExtension(t *testing.T) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ADDITIONAL_EXTENSION)) p.Convert() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } checkPageContent(t, p, "first line.
\nsecond line.
fourth line.
\n") } func TestTableOfContents(t *testing.T) { p, _ := NewPage("tocpage.md") _, err := p.ReadFrom(strings.NewReader(PAGE_WITH_TOC)) p.Convert() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } 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) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE)) p.Convert() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } checkPageTitle(t, p, "Simple") checkPageContent(t, p, "Summary Same Line
\n\nSome more text
\n") checkPageSummary(t, p, "Summary Same Line
\n") checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html", "_default/single.html", "theme/page/single.html", "theme/_default/single.html") } func TestPageWithDate(t *testing.T) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE)) p.Convert() if err != nil { 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") if err != nil { t.Fatalf("Unable to prase page.") } checkPageDate(t, p, d) } func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) { viper.Reset() p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ALL_CJK_RUNES)) p.Convert() p.analyzePage() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } if p.WordCount != 8 { t.Fatalf("incorrect word count for content '%s'. expected %v, got %v", p.plain, 8, p.WordCount) } } func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) { viper.Reset() defer viper.Reset() viper.Set("HasCJKLanguage", true) p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ALL_CJK_RUNES)) p.Convert() p.analyzePage() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } if p.WordCount != 15 { t.Fatalf("incorrect word count for content '%s'. expected %v, got %v", p.plain, 15, p.WordCount) } } func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) { viper.Reset() defer viper.Reset() viper.Set("HasCJKLanguage", true) p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_MAIN_ENGLISH_WITH_CJK_RUNES)) p.Convert() p.analyzePage() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } if p.WordCount != 74 { t.Fatalf("incorrect word count for content '%s'. expected %v, got %v", p.plain, 74, p.WordCount) } if p.Summary != SIMPLE_PAGE_WITH_MAIN_ENGLISH_WITH_CJK_RUNES_SUMMARY { t.Fatalf("incorrect Summary for content '%s'. expected %v, got %v", p.plain, SIMPLE_PAGE_WITH_MAIN_ENGLISH_WITH_CJK_RUNES_SUMMARY, p.Summary) } } func TestWordCountWithIsCJKLanguageFalse(t *testing.T) { viper.Reset() defer viper.Reset() viper.Set("HasCJKLanguage", true) p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ISCJKLANGUAGE_FALSE)) p.Convert() p.analyzePage() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } if p.WordCount != 75 { t.Fatalf("incorrect word count for content '%s'. expected %v, got %v", p.plain, 75, p.WordCount) } if p.Summary != SIMPLE_PAGE_WITH_ISCJKLANGUAGE_FALSE_SUMMARY { t.Fatalf("incorrect Summary for content '%s'. expected %v, got %v", p.plain, SIMPLE_PAGE_WITH_ISCJKLANGUAGE_FALSE_SUMMARY, p.Summary) } } func TestWordCount(t *testing.T) { p, _ := NewPage("simple.md") _, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT)) p.Convert() p.analyzePage() if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } if p.WordCount != 483 { t.Fatalf("incorrect word count. expected %v, got %v", 483, p.WordCount) } if p.FuzzyWordCount != 500 { t.Fatalf("incorrect word count. expected %v, got %v", 500, p.WordCount) } if p.ReadingTime != 3 { t.Fatalf("incorrect min read. expected %v, got %v", 3, p.ReadingTime) } checkTruncation(t, p, true, "long page") } func TestCreatePage(t *testing.T) { var tests = []struct { r string }{ {SIMPLE_PAGE_JSON}, {SIMPLE_PAGE_JSON_LOOSE}, {SIMPLE_PAGE_JSON_MULTIPLE}, //{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 }{ {INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "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 }{ {INVALID_FRONT_MATTER_MISSING, true}, // TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown. {INVALID_FRONT_MATTER_SHORT_DELIM, true}, {RENDER_NO_FRONT_MATTER, false}, {CONTENT_WITH_COMMENTED_FM, true}, {CONTENT_WITH_COMMENTED_TEXT_FM, true}, {CONTENT_WITH_COMMENTED_LONG_FM, false}, {CONTENT_WITH_COMMENTED_LONG2_FM, 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(PAGE_WITH_CALENDAR_JSON_FRONTMATTER)) pageYAML, _ := NewPage("test/fileYAML.md") _, _ = pageYAML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_YAML_FRONTMATTER)) pageTOML, _ := NewPage("test/fileTOML.md") _, _ = pageTOML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_TOML_FRONTMATTER)) 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(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES)) 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(INVALID_FRONT_MATTER_LEADING_WS)) 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(SIMPLE_PAGE)) 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 ( path_content_two_dir = filepath.Join("content", "dub", "sub", "file1.md") path_content_one_dir = filepath.Join("content", "gub", "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, L("dub/single.html", "_default/single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_content_one_dir, L("gub/single.html", "_default/single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_content_no_dir, L("page/single.html", "_default/single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_one_directory, L("fub/single.html", "_default/single.html")}, {SIMPLE_PAGE_NOLAYOUT, path_no_directory, L("page/single.html", "_default/single.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_two_dir, L("dub/foobar.html", "_default/foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_one_dir, L("gub/foobar.html", "_default/foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_one_directory, L("fub/foobar.html", "_default/foobar.html")}, {SIMPLE_PAGE_LAYOUT_FOOBAR, path_no_directory, L("page/foobar.html", "_default/foobar.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_two_dir, L("foobar/single.html", "_default/single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_one_dir, L("foobar/single.html", "_default/single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_content_no_dir, L("foobar/single.html", "_default/single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_one_directory, L("foobar/single.html", "_default/single.html")}, {SIMPLE_PAGE_TYPE_FOOBAR, path_no_directory, L("foobar/single.html", "_default/single.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_two_dir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_one_dir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_content_no_dir, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_one_directory, L("barfoo/buzfoo.html", "_default/buzfoo.html")}, {SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, 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) { viper.Reset() defer viper.Reset() viper.Set("DefaultExtension", "html") site_permalinks_setting := PermalinkOverrides{ "post": ":year/:month/:day/:title/", } tests := []struct { content string path string hasPermalink bool expected string }{ {SIMPLE_PAGE, "content/post/x.md", false, "content/post/x.html"}, {SIMPLE_PAGE_WITH_URL, "content/post/x.md", false, "simple/url/index.html"}, {SIMPLE_PAGE_WITH_SLUG, "content/post/x.md", false, "content/post/simple-slug.html"}, {SIMPLE_PAGE_WITH_DATE, "content/post/x.md", true, "2013/10/15/simple/index.html"}, {UTF8_PAGE, "content/post/x.md", false, "content/post/x.html"}, {UTF8_PAGE_WITH_URL, "content/post/x.md", false, "ラーメン/url/index.html"}, {UTF8_PAGE_WITH_SLUG, "content/post/x.md", false, "content/post/ラーメン-slug.html"}, {UTF8_PAGE_WITH_DATE, "content/post/x.md", true, "2013/10/15/ラーメン/index.html"}, } for _, test := range tests { p, _ := NewPageFrom(strings.NewReader(test.content), filepath.FromSlash(test.path)) p.Node.Site = &SiteInfo{} if test.hasPermalink { p.Node.Site.Permalinks = site_permalinks_setting } 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 PAGE_WITH_DRAFT_AND_PUBLISHED = `--- title: broken published: false draft: true --- some content ` func TestDraftAndPublishedFrontMatterError(t *testing.T) { _, err := NewPageFrom(strings.NewReader(PAGE_WITH_DRAFT_AND_PUBLISHED), "content/post/broken.md") if err != ErrHasDraftAndPublished { t.Errorf("expected ErrHasDraftAndPublished, was %#v", err) } } var PAGE_WITH_PUBLISHED_FALSE = `--- title: okay published: false --- some content ` var PAGE_WITH_PUBLISHED_TRUE = `--- title: okay published: true --- some content ` func TestPublishedFrontMatter(t *testing.T) { p, err := NewPageFrom(strings.NewReader(PAGE_WITH_PUBLISHED_FALSE), "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(PAGE_WITH_PUBLISHED_TRUE), "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) } } func listEqual(left, right []string) bool { if len(left) != len(right) { return false } for i := range left { if left[i] != right[i] { return false } } return true } // TODO(bep) this may be useful for other tests. func compareObjects(a interface{}, b interface{}) bool { aStr := strings.Split(fmt.Sprintf("%v", a), "") sort.Strings(aStr) bStr := strings.Split(fmt.Sprintf("%v", b), "") sort.Strings(bStr) return strings.Join(aStr, "") == strings.Join(bStr, "") }