2013-08-23 17:47:03 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-08-27 05:09:50 -04:00
|
|
|
"bytes"
|
2013-08-30 17:38:33 -04:00
|
|
|
"fmt"
|
2013-09-20 20:03:43 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
2013-10-01 17:56:14 -04:00
|
|
|
"github.com/spf13/hugo/target"
|
2013-09-03 18:38:20 -04:00
|
|
|
"html/template"
|
2013-10-08 12:33:57 -04:00
|
|
|
"io"
|
2013-08-23 17:47:03 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-09-18 13:27:56 -04:00
|
|
|
const (
|
|
|
|
TEMPLATE_TITLE = "{{ .Title }}"
|
|
|
|
PAGE_SIMPLE_TITLE = `---
|
2013-08-23 17:47:03 -04:00
|
|
|
title: simple template
|
|
|
|
---
|
|
|
|
content`
|
|
|
|
|
2013-09-18 13:27:56 -04:00
|
|
|
TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}"
|
|
|
|
TEMPLATE_FUNC = "{{ .Title | urlize }}"
|
|
|
|
TEMPLATE_CONTENT = "{{ .Content }}"
|
|
|
|
TEMPLATE_DATE = "{{ .Date }}"
|
|
|
|
INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
|
2013-11-05 17:28:06 -05:00
|
|
|
TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>"
|
|
|
|
TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>"
|
2013-09-18 13:27:56 -04:00
|
|
|
PAGE_URL_SPECIFIED = `---
|
2013-08-29 12:37:37 -04:00
|
|
|
title: simple template
|
|
|
|
url: "mycategory/my-whatever-content/"
|
|
|
|
---
|
|
|
|
content`
|
|
|
|
|
2013-09-18 13:27:56 -04:00
|
|
|
PAGE_WITH_MD = `---
|
2013-09-01 15:02:05 -04:00
|
|
|
title: page with md
|
|
|
|
---
|
|
|
|
# heading 1
|
|
|
|
text
|
|
|
|
## heading 2
|
|
|
|
more text
|
|
|
|
`
|
2013-09-18 13:27:56 -04:00
|
|
|
)
|
2013-09-01 15:02:05 -04:00
|
|
|
|
2013-08-23 17:47:03 -04:00
|
|
|
func pageMust(p *Page, err error) *Page {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:34:41 -04:00
|
|
|
func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
|
2013-08-23 17:47:03 -04:00
|
|
|
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
|
|
|
s := new(Site)
|
|
|
|
s.prepTemplates()
|
2013-10-02 09:34:41 -04:00
|
|
|
err := s.renderThing(p, "foobar", nil)
|
2013-08-23 17:47:03 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected err to be returned when missing the template.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddInvalidTemplate(t *testing.T) {
|
|
|
|
s := new(Site)
|
|
|
|
s.prepTemplates()
|
|
|
|
err := s.addTemplate("missing", TEMPLATE_MISSING_FUNC)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expecting the template to return an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:34:41 -04:00
|
|
|
type nopCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() error { return nil }
|
|
|
|
|
|
|
|
func NopCloser(w io.Writer) io.WriteCloser {
|
|
|
|
return nopCloser{w}
|
|
|
|
}
|
|
|
|
|
2013-08-23 17:47:03 -04:00
|
|
|
func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) {
|
2013-10-02 09:34:41 -04:00
|
|
|
content := new(bytes.Buffer)
|
|
|
|
err := s.renderThing(p, tmplName, NopCloser(content))
|
2013-08-23 17:47:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to render template.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(content.Bytes()) != expected {
|
|
|
|
t.Fatalf("Content did not match expected: %s. got: %s", expected, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 23:32:13 -05:00
|
|
|
func TestRenderThing(t *testing.T) {
|
2013-08-23 17:47:03 -04:00
|
|
|
tests := []struct {
|
|
|
|
content string
|
|
|
|
template string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"},
|
|
|
|
{PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"},
|
2013-09-03 15:41:13 -04:00
|
|
|
{PAGE_WITH_MD, TEMPLATE_CONTENT, "<h1>heading 1</h1>\n\n<p>text</p>\n\n<h2>heading 2</h2>\n\n<p>more text</p>\n"},
|
2013-09-03 17:51:06 -04:00
|
|
|
{SIMPLE_PAGE_RFC3339_DATE, TEMPLATE_DATE, "2013-05-17 16:59:30 +0000 UTC"},
|
2013-08-23 17:47:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := new(Site)
|
|
|
|
s.prepTemplates()
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2013-09-01 15:02:05 -04:00
|
|
|
p, err := ReadFrom(strings.NewReader(test.content), "content/a/file.md")
|
2013-08-23 17:47:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing buffer: %s", err)
|
|
|
|
}
|
|
|
|
templateName := fmt.Sprintf("foobar%d", i)
|
|
|
|
err = s.addTemplate(templateName, test.template)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to add template")
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:41:13 -04:00
|
|
|
p.Content = template.HTML(p.Content)
|
2013-10-02 09:34:41 -04:00
|
|
|
html := new(bytes.Buffer)
|
|
|
|
err = s.renderThing(p, templateName, NopCloser(html))
|
|
|
|
if err != nil {
|
2013-08-23 17:47:03 -04:00
|
|
|
t.Errorf("Unable to render html: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(html.Bytes()) != test.expected {
|
2013-09-03 17:51:06 -04:00
|
|
|
t.Errorf("Content does not match.\nExpected\n\t'%q'\ngot\n\t'%q'", test.expected, html)
|
2013-08-23 17:47:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 05:09:50 -04:00
|
|
|
|
2013-10-01 22:58:15 -04:00
|
|
|
func HTML(in string) string {
|
2013-11-05 17:28:06 -05:00
|
|
|
return in
|
2013-10-01 22:58:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderThingOrDefault(t *testing.T) {
|
2013-08-27 05:09:50 -04:00
|
|
|
tests := []struct {
|
|
|
|
content string
|
|
|
|
missing bool
|
|
|
|
template string
|
|
|
|
expected string
|
|
|
|
}{
|
2013-10-01 22:58:15 -04:00
|
|
|
{PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, HTML("simple template")},
|
|
|
|
{PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, HTML("simple-template")},
|
|
|
|
{PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, HTML("simple template")},
|
|
|
|
{PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")},
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
|
|
|
|
2013-10-01 22:58:15 -04:00
|
|
|
files := make(map[string][]byte)
|
|
|
|
target := &target.InMemoryTarget{Files: files}
|
|
|
|
s := &Site{
|
|
|
|
Target: target,
|
|
|
|
}
|
2013-08-27 05:09:50 -04:00
|
|
|
s.prepTemplates()
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing buffer: %s", err)
|
|
|
|
}
|
|
|
|
templateName := fmt.Sprintf("default%d", i)
|
|
|
|
err = s.addTemplate(templateName, test.template)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to add template")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err2 error
|
|
|
|
if test.missing {
|
2013-10-02 09:34:41 -04:00
|
|
|
err2 = s.render(p, "out", "missing", templateName)
|
2013-08-27 05:09:50 -04:00
|
|
|
} else {
|
2013-10-02 09:34:41 -04:00
|
|
|
err2 = s.render(p, "out", templateName, "missing_default")
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
t.Errorf("Unable to render html: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:58:15 -04:00
|
|
|
if string(files["out"]) != test.expected {
|
|
|
|
t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, files["out"])
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
2013-08-30 17:38:33 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 12:37:37 -04:00
|
|
|
|
2013-09-20 20:24:25 -04:00
|
|
|
func TestTargetPath(t *testing.T) {
|
2013-09-20 20:03:43 -04:00
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
content string
|
|
|
|
expectedOutFile string
|
|
|
|
expectedSection string
|
|
|
|
}{
|
|
|
|
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
|
2013-10-08 12:33:57 -04:00
|
|
|
{"content/x/y/deepfile.md", SIMPLE_PAGE, "x/y/deepfile.html", "x/y"},
|
|
|
|
{"content/x/y/z/deeperfile.md", SIMPLE_PAGE, "x/y/z/deeperfile.html", "x/y/z"},
|
2013-09-20 20:03:43 -04:00
|
|
|
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
|
|
|
|
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
|
|
|
|
{"file.md", SIMPLE_PAGE, "file.html", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
s := &Site{
|
|
|
|
Config: Config{ContentDir: "content"},
|
|
|
|
}
|
|
|
|
p := pageMust(ReadFrom(strings.NewReader(test.content), s.Config.GetAbsPath(test.doc)))
|
|
|
|
|
|
|
|
expected := test.expectedOutFile
|
2013-08-29 12:37:37 -04:00
|
|
|
|
2013-09-20 20:24:25 -04:00
|
|
|
if p.TargetPath() != expected {
|
|
|
|
t.Errorf("%s => OutFile expected: '%s', got: '%s'", test.doc, expected, p.TargetPath())
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
2013-08-29 12:37:37 -04:00
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
if p.Section != test.expectedSection {
|
|
|
|
t.Errorf("%s => p.Section expected: %s, got: %s", test.doc, test.expectedSection, p.Section)
|
|
|
|
}
|
2013-08-30 17:38:33 -04:00
|
|
|
}
|
2013-08-29 12:37:37 -04:00
|
|
|
}
|
2013-09-17 17:16:06 -04:00
|
|
|
|
2013-09-18 17:21:27 -04:00
|
|
|
func TestSkipRender(t *testing.T) {
|
|
|
|
files := make(map[string][]byte)
|
2013-10-01 17:56:14 -04:00
|
|
|
target := &target.InMemoryTarget{Files: files}
|
2013-09-20 20:03:43 -04:00
|
|
|
sources := []source.ByteSource{
|
|
|
|
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*"), "sect"},
|
|
|
|
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>"), "sect"},
|
|
|
|
{"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"},
|
|
|
|
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"},
|
|
|
|
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
|
2013-11-05 17:28:06 -05:00
|
|
|
{"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"},
|
2013-09-20 20:03:43 -04:00
|
|
|
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
|
2013-09-18 17:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &Site{
|
|
|
|
Target: target,
|
2013-11-05 17:28:06 -05:00
|
|
|
Config: Config{Verbose: true, BaseUrl: "http://auth/bub"},
|
2013-09-20 20:03:43 -04:00
|
|
|
Source: &source.InMemorySource{sources},
|
2013-09-18 17:21:27 -04:00
|
|
|
}
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
s.prepTemplates()
|
|
|
|
|
|
|
|
must(s.addTemplate("_default/single.html", "{{.Content}}"))
|
|
|
|
must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>"))
|
2013-11-05 17:28:06 -05:00
|
|
|
must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>"))
|
2013-09-18 17:21:27 -04:00
|
|
|
|
|
|
|
if err := s.CreatePages(); err != nil {
|
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.BuildSiteMeta(); err != nil {
|
|
|
|
t.Fatalf("Unable to build site metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.RenderPages(); err != nil {
|
|
|
|
t.Fatalf("Unable to render pages. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
expected string
|
|
|
|
}{
|
2013-11-05 17:28:06 -05:00
|
|
|
{"sect/doc1.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"},
|
|
|
|
{"sect/doc2.html", "<!doctype html><html><body>more content</body></html>"},
|
|
|
|
{"sect/doc3.html", "<h1>doc3</h1>\n\n<p><em>some</em> content</p>\n"},
|
|
|
|
{"sect/doc4.html", "<h1>doc4</h1>\n\n<p><em>some content</em></p>\n"},
|
|
|
|
{"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
|
|
|
|
{"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
|
|
|
{"doc7.html", "<html><body>doc7 content</body></html>"},
|
2013-09-18 17:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2013-10-01 17:56:14 -04:00
|
|
|
content, ok := target.Files[test.doc]
|
2013-09-18 17:21:27 -04:00
|
|
|
if !ok {
|
2013-10-01 17:56:14 -04:00
|
|
|
t.Fatalf("Did not find %s in target. %v", test.doc, target.Files)
|
2013-09-18 17:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, []byte(test.expected)) {
|
|
|
|
t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 17:16:06 -04:00
|
|
|
func TestAbsUrlify(t *testing.T) {
|
|
|
|
files := make(map[string][]byte)
|
2013-10-01 17:56:14 -04:00
|
|
|
target := &target.InMemoryTarget{Files: files}
|
2013-09-20 20:03:43 -04:00
|
|
|
sources := []source.ByteSource{
|
|
|
|
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"), "sect"},
|
|
|
|
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"), "blue"},
|
2013-09-18 18:48:36 -04:00
|
|
|
}
|
2013-09-17 17:16:06 -04:00
|
|
|
s := &Site{
|
|
|
|
Target: target,
|
2013-11-05 17:28:06 -05:00
|
|
|
Config: Config{BaseUrl: "http://auth/bub"},
|
2013-09-20 20:03:43 -04:00
|
|
|
Source: &source.InMemorySource{sources},
|
2013-09-17 17:16:06 -04:00
|
|
|
}
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
s.prepTemplates()
|
2013-11-05 17:28:06 -05:00
|
|
|
must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS))
|
2013-09-17 17:16:06 -04:00
|
|
|
|
|
|
|
if err := s.CreatePages(); err != nil {
|
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.BuildSiteMeta(); err != nil {
|
|
|
|
t.Fatalf("Unable to build site metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.RenderPages(); err != nil {
|
|
|
|
t.Fatalf("Unable to render pages. %s", err)
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:48:36 -04:00
|
|
|
tests := []struct {
|
|
|
|
file, expected string
|
|
|
|
}{
|
2013-11-05 17:28:06 -05:00
|
|
|
{"content/blue/doc2.html", "<a href=\"http://auth/bub/foobar.jpg\">Going</a>"},
|
|
|
|
{"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
|
2013-09-18 18:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2013-10-01 17:56:14 -04:00
|
|
|
content, ok := target.Files[test.file]
|
2013-09-20 20:03:43 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
|
|
|
}
|
2013-09-17 17:16:06 -04:00
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
expected := test.expected
|
|
|
|
if string(content) != expected {
|
|
|
|
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
|
|
|
}
|
2013-09-17 17:16:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 11:01:31 -04:00
|
|
|
|
|
|
|
var WEIGHTED_PAGE_1 = []byte(`+++
|
|
|
|
weight = "2"
|
|
|
|
title = "One"
|
|
|
|
+++
|
|
|
|
Front Matter with Ordered Pages`)
|
|
|
|
|
|
|
|
var WEIGHTED_PAGE_2 = []byte(`+++
|
|
|
|
weight = "6"
|
|
|
|
title = "Two"
|
|
|
|
+++
|
|
|
|
Front Matter with Ordered Pages 2`)
|
|
|
|
|
|
|
|
var WEIGHTED_PAGE_3 = []byte(`+++
|
|
|
|
weight = "4"
|
|
|
|
title = "Three"
|
|
|
|
date = "2012-04-06"
|
|
|
|
+++
|
|
|
|
Front Matter with Ordered Pages 3`)
|
|
|
|
|
|
|
|
var WEIGHTED_PAGE_4 = []byte(`+++
|
|
|
|
weight = "4"
|
|
|
|
title = "Four"
|
|
|
|
date = "2012-01-01"
|
|
|
|
+++
|
|
|
|
Front Matter with Ordered Pages 4`)
|
|
|
|
|
2013-11-05 00:29:37 -05:00
|
|
|
var WEIGHTED_SOURCES = []source.ByteSource{
|
|
|
|
{"sect/doc1.md", WEIGHTED_PAGE_1, "sect"},
|
|
|
|
{"sect/doc2.md", WEIGHTED_PAGE_2, "sect"},
|
|
|
|
{"sect/doc3.md", WEIGHTED_PAGE_3, "sect"},
|
|
|
|
{"sect/doc4.md", WEIGHTED_PAGE_4, "sect"},
|
|
|
|
}
|
|
|
|
|
2013-10-18 11:01:31 -04:00
|
|
|
func TestOrderedPages(t *testing.T) {
|
|
|
|
files := make(map[string][]byte)
|
|
|
|
target := &target.InMemoryTarget{Files: files}
|
|
|
|
s := &Site{
|
|
|
|
Target: target,
|
|
|
|
Config: Config{BaseUrl: "http://auth/bub/"},
|
2013-11-05 00:29:37 -05:00
|
|
|
Source: &source.InMemorySource{WEIGHTED_SOURCES},
|
2013-10-18 11:01:31 -04:00
|
|
|
}
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
|
|
|
|
if err := s.CreatePages(); err != nil {
|
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.BuildSiteMeta(); err != nil {
|
|
|
|
t.Fatalf("Unable to build site metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-10-26 00:20:48 -04:00
|
|
|
if s.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 {
|
|
|
|
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", 2, s.Sections["sect"][0].Weight)
|
2013-10-18 11:01:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" {
|
|
|
|
t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title)
|
|
|
|
}
|
|
|
|
}
|
2013-10-24 18:24:47 -04:00
|
|
|
|
|
|
|
var PAGE_WITH_WEIGHTED_INDEXES_2 = []byte(`+++
|
|
|
|
tags = [ "a", "b", "c" ]
|
|
|
|
tags_weight = 22
|
|
|
|
categories = ["d"]
|
|
|
|
title = "foo"
|
|
|
|
categories_weight = 44
|
|
|
|
+++
|
|
|
|
Front Matter with weighted tags and categories`)
|
|
|
|
|
|
|
|
var PAGE_WITH_WEIGHTED_INDEXES_1 = []byte(`+++
|
|
|
|
tags = [ "a" ]
|
|
|
|
tags_weight = 33
|
|
|
|
title = "bar"
|
|
|
|
categories = [ "d", "e" ]
|
|
|
|
categories_weight = 11
|
|
|
|
alias = "spf13"
|
|
|
|
date = 1979-05-27T07:32:00Z
|
|
|
|
+++
|
|
|
|
Front Matter with weighted tags and categories`)
|
|
|
|
|
|
|
|
var PAGE_WITH_WEIGHTED_INDEXES_3 = []byte(`+++
|
|
|
|
title = "bza"
|
|
|
|
categories = [ "e" ]
|
|
|
|
categories_weight = 11
|
|
|
|
alias = "spf13"
|
|
|
|
date = 2010-05-27T07:32:00Z
|
|
|
|
+++
|
|
|
|
Front Matter with weighted tags and categories`)
|
|
|
|
|
|
|
|
func TestWeightedIndexes(t *testing.T) {
|
|
|
|
files := make(map[string][]byte)
|
|
|
|
target := &target.InMemoryTarget{Files: files}
|
|
|
|
sources := []source.ByteSource{
|
|
|
|
{"sect/doc1.md", PAGE_WITH_WEIGHTED_INDEXES_1, "sect"},
|
|
|
|
{"sect/doc2.md", PAGE_WITH_WEIGHTED_INDEXES_2, "sect"},
|
|
|
|
{"sect/doc3.md", PAGE_WITH_WEIGHTED_INDEXES_3, "sect"},
|
|
|
|
}
|
|
|
|
indexes := make(map[string]string)
|
|
|
|
|
|
|
|
indexes["tag"] = "tags"
|
|
|
|
indexes["category"] = "categories"
|
|
|
|
s := &Site{
|
|
|
|
Target: target,
|
|
|
|
Config: Config{BaseUrl: "http://auth/bub/", Indexes: indexes},
|
|
|
|
Source: &source.InMemorySource{sources},
|
|
|
|
}
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
|
|
|
|
if err := s.CreatePages(); err != nil {
|
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.BuildSiteMeta(); err != nil {
|
|
|
|
t.Fatalf("Unable to build site metadata: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-10-26 00:20:48 -04:00
|
|
|
if s.Indexes["tags"]["a"][0].Page.Title != "foo" {
|
|
|
|
t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Indexes["tags"]["a"][0].Page.Title)
|
2013-10-24 18:24:47 -04:00
|
|
|
}
|
|
|
|
|
2013-10-26 00:20:48 -04:00
|
|
|
if s.Indexes["categories"]["d"][0].Page.Title != "bar" {
|
|
|
|
t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Indexes["categories"]["d"][0].Page.Title)
|
2013-10-24 18:24:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.Indexes["categories"]["e"][0].Page.Title != "bza" {
|
|
|
|
t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Indexes["categories"]["e"][0].Page.Title)
|
|
|
|
}
|
|
|
|
}
|