diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /resources/page/page_matcher_test.go | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) |
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code.
Also,
* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.
Closes #10896
Closes #10620
Diffstat (limited to 'resources/page/page_matcher_test.go')
-rw-r--r-- | resources/page/page_matcher_test.go | 89 |
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) + } |