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"
|
2017-06-22 16:42:57 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-06-16 22:47:49 +02:00
|
|
|
"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-07-03 15:37:14 +02:00
|
|
|
fm := templates.MergeVariableMaps(data, map[string]interface{}{"permalink": pattern})
|
2017-06-23 02:39:53 +02:00
|
|
|
ext := filepath.Ext(path)
|
|
|
|
switch ext {
|
|
|
|
case ".md", ".markdown":
|
|
|
|
ext = ".html"
|
|
|
|
}
|
2017-07-13 18:05:32 +02:00
|
|
|
f := file{site: s, relpath: path, frontMatter: fm, outputExt: ext}
|
|
|
|
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)
|
2017-06-23 20:57:28 +02:00
|
|
|
p.fileModTime = t0
|
2017-07-03 15:37:14 +02:00
|
|
|
return p.computePermalink(p.permalinkVariables())
|
2017-06-16 20:52:29 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|