1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 23:01:18 +01:00
gojekyll/pages/permalinks_test.go

81 lines
2.3 KiB
Go
Raw Normal View History

2017-06-22 11:02:32 -04:00
package pages
2017-06-11 16:00:03 -04:00
import (
2017-06-22 20:39:53 -04:00
"path/filepath"
2017-06-11 16:00:03 -04:00
"testing"
2017-06-22 20:39:53 -04:00
"time"
2017-06-11 16:00:03 -04:00
2017-07-01 14:55:50 -04:00
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/templates"
"github.com/stretchr/testify/require"
2017-06-11 16:00:03 -04:00
)
2017-06-22 20:39:53 -04: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{
{"_c/a/b/c.d", "/prefix/:collection/post", "/prefix/c/post"},
{"_c/a/b/c.d", "/prefix:path/post", "/prefix/a/b/c/post"},
}
2017-06-11 16:00:03 -04:00
func TestExpandPermalinkPattern(t *testing.T) {
2017-06-13 08:51:58 -04:00
var (
2017-07-06 19:31:36 -04:00
c = containerFake{config.Default(), ""}
2017-06-30 23:56:29 -04:00
d = map[string]interface{}{
2017-06-22 20:39:53 -04:00
"categories": "b a",
}
2017-06-13 08:51:58 -04:00
)
2017-06-30 23:56:29 -04:00
testPermalinkPattern := func(pattern, path string, data map[string]interface{}) (string, error) {
2017-07-03 09:37:14 -04:00
fm := templates.MergeVariableMaps(data, map[string]interface{}{"permalink": pattern})
2017-06-22 20:39:53 -04:00
ext := filepath.Ext(path)
switch ext {
case ".md", ".markdown":
ext = ".html"
}
2017-07-03 09:37:14 -04:00
p := file{container: c, relpath: path, frontMatter: fm, outputExt: ext}
2017-06-22 20:39:53 -04:00
t0, err := time.Parse(time.RFC3339, "2006-02-03T15:04:05Z")
require.NoError(t, err)
2017-06-23 14:57:28 -04:00
p.fileModTime = t0
2017-07-03 09:37:14 -04:00
return p.computePermalink(p.permalinkVariables())
}
2017-06-22 20:39:53 -04: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 16:00:03 -04:00
2017-07-06 19:31:36 -04:00
c = containerFake{config.Default(), "_c/"}
2017-06-22 20:39:53 -04: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 10:37:31 -04:00
})
2017-06-11 16:00:03 -04:00
}