summaryrefslogtreecommitdiffstats
path: root/resources/page/page_matcher_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'resources/page/page_matcher_test.go')
-rw-r--r--resources/page/page_matcher_test.go89
1 files changed, 82 insertions, 7 deletions
diff --git a/resources/page/page_matcher_test.go b/resources/page/page_matcher_test.go
index 4a59dc502..990312ed1 100644
--- a/resources/page/page_matcher_test.go
+++ b/resources/page/page_matcher_test.go
@@ -18,6 +18,7 @@ import (
"testing"
"github.com/gohugoio/hugo/common/hugo"
+ "github.com/gohugoio/hugo/common/maps"
qt "github.com/frankban/quicktest"
)
@@ -71,13 +72,87 @@ func TestPageMatcher(t *testing.T) {
c.Run("Decode", func(c *qt.C) {
var v PageMatcher
- c.Assert(DecodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
- c.Assert(DecodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
- c.Assert(DecodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
- c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
- c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
- c.Assert(DecodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
- c.Assert(DecodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
+ c.Assert(decodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
+ c.Assert(decodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
+ c.Assert(decodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
+ c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
+ c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
+ c.Assert(decodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
+ c.Assert(decodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"})
})
+
+ c.Run("mapToPageMatcherParamsConfig", func(c *qt.C) {
+ fn := func(m map[string]any) PageMatcherParamsConfig {
+ v, err := mapToPageMatcherParamsConfig(m)
+ c.Assert(err, qt.IsNil)
+ return v
+ }
+ // Legacy.
+ c.Assert(fn(map[string]any{"_target": map[string]any{"kind": "page"}, "foo": "bar"}), qt.DeepEquals, PageMatcherParamsConfig{
+ Params: maps.Params{
+ "foo": "bar",
+ },
+ Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
+ })
+
+ // Current format.
+ c.Assert(fn(map[string]any{"target": map[string]any{"kind": "page"}, "params": map[string]any{"foo": "bar"}}), qt.DeepEquals, PageMatcherParamsConfig{
+ Params: maps.Params{
+ "foo": "bar",
+ },
+ Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
+ })
+ })
+}
+
+func TestDecodeCascadeConfig(t *testing.T) {
+ c := qt.New(t)
+
+ in := []map[string]any{
+ {
+ "params": map[string]any{
+ "a": "av",
+ },
+ "target": map[string]any{
+ "kind": "page",
+ "Environment": "production",
+ },
+ },
+ {
+ "params": map[string]any{
+ "b": "bv",
+ },
+ "target": map[string]any{
+ "kind": "page",
+ },
+ },
+ }
+
+ got, err := DecodeCascadeConfig(in)
+
+ c.Assert(err, qt.IsNil)
+ c.Assert(got, qt.IsNotNil)
+ c.Assert(got.Config, qt.DeepEquals,
+ map[PageMatcher]maps.Params{
+ {Path: "", Kind: "page", Lang: "", Environment: ""}: {
+ "b": "bv",
+ },
+ {Path: "", Kind: "page", Lang: "", Environment: "production"}: {
+ "a": "av",
+ },
+ },
+ )
+ c.Assert(got.SourceStructure, qt.DeepEquals, []PageMatcherParamsConfig{
+ {
+ Params: maps.Params{"a": string("av")},
+ Target: PageMatcher{Kind: "page", Environment: "production"},
+ },
+ {Params: maps.Params{"b": string("bv")}, Target: PageMatcher{Kind: "page"}},
+ })
+
+ got, err = DecodeCascadeConfig(nil)
+ c.Assert(err, qt.IsNil)
+ c.Assert(got, qt.IsNotNil)
+
}