1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 01:14:40 +01:00
gojekyll/pages/permalinks_test.go

82 lines
2.3 KiB
Go
Raw Normal View History

2017-06-22 17:02:32 +02:00
package pages
2017-06-11 22:00:03 +02:00
import (
2017-06-23 02:39:53 +02:00
"path/filepath"
2017-06-11 22:00:03 +02:00
"testing"
2017-06-23 02:39:53 +02:00
"time"
2017-06-11 22:00:03 +02:00
2017-07-01 20:55:50 +02:00
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/frontmatter"
"github.com/stretchr/testify/require"
2017-06-11 22:00:03 +02:00
)
2017-06-23 02:39:53 +02:00
type pathTest struct{ path, pattern, out string }
var tests = []pathTest{
{"/a/b/base.html", "/out:output_ext", "/out.html"},
{"/a/b/base.md", "/out:output_ext", "/out.html"},
{"/a/b/base.markdown", "/out:output_ext", "/out.html"},
{"/a/b/base.html", "/:path/out:output_ext", "/a/b/base/out.html"},
{"/a/b/base.html", "/prefix/:name", "/prefix/base"},
{"/a/b/base.html", "/prefix/:path/post", "/prefix/a/b/base/post"},
{"/a/b/base.html", "/prefix/:title", "/prefix/base"},
{"/a/b/base.html", "/prefix/:slug", "/prefix/base"},
{"base", "/:categories/:name:output_ext", "/a/b/base"},
{"base", "date", "/a/b/2006/02/03/base.html"},
{"base", "pretty", "/a/b/2006/02/03/base/"},
{"base", "ordinal", "/a/b/2006/34/base.html"},
{"base", "none", "/a/b/base.html"},
}
var collectionTests = []pathTest{
2017-07-10 19:23:51 +02:00
{"/a/b/c.d", "/prefix/:collection/post", "/prefix/c/post"},
{"/a/b/c.d", "/prefix:path/post", "/prefix/a/b/c/post"},
2017-06-23 02:39:53 +02:00
}
2017-06-11 22:00:03 +02:00
func TestExpandPermalinkPattern(t *testing.T) {
2017-06-13 14:51:58 +02:00
var (
2017-07-10 19:23:51 +02:00
s = siteFake{t, config.Default()}
2017-07-01 05:56:29 +02:00
d = map[string]interface{}{
2017-06-23 02:39:53 +02:00
"categories": "b a",
}
2017-06-13 14:51:58 +02:00
)
2017-07-01 05:56:29 +02:00
testPermalinkPattern := func(pattern, path string, data map[string]interface{}) (string, error) {
2017-09-03 18:21:55 +02:00
fm := frontmatter.Merge(data, FrontMatter{"permalink": pattern})
2017-06-23 02:39:53 +02:00
ext := filepath.Ext(path)
switch ext {
case ".md", ".markdown":
ext = ".html"
}
f := file{site: s, relPath: path, fm: fm, outputExt: ext}
2017-07-13 18:05:32 +02:00
p := page{file: f}
2017-06-23 02:39:53 +02:00
t0, err := time.Parse(time.RFC3339, "2006-02-03T15:04:05Z")
require.NoError(t, err)
p.modTime = t0
2017-07-03 15:37:14 +02:00
return p.computePermalink(p.permalinkVariables())
}
2017-06-23 02:39:53 +02:00
runTests := func(tests []pathTest) {
for i, test := range tests {
t.Run(test.pattern, func(t *testing.T) {
p, err := testPermalinkPattern(test.pattern, test.path, d)
require.NoError(t, err)
require.Equalf(t, test.out, p, "%d: pattern=%s", i+1, test.pattern)
})
}
}
runTests(tests)
2017-06-11 22:00:03 +02:00
2017-07-10 19:23:51 +02:00
s = siteFake{t, config.Default()}
2017-06-23 02:39:53 +02:00
d["collection"] = "c"
runTests(collectionTests)
t.Run("invalid template variable", func(t *testing.T) {
p, err := testPermalinkPattern("/:invalid", "/a/b/base.html", d)
require.Error(t, err)
require.Zero(t, p)
2017-06-22 16:37:31 +02:00
})
2017-06-11 22:00:03 +02:00
}