From 241b21b0fd34d91fccb2ce69874110dceae6f926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 4 Jan 2023 18:24:36 +0100 Subject: 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 --- resources/page/page_matcher_test.go | 89 ++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 7 deletions(-) (limited to 'resources/page/page_matcher_test.go') 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) + } -- cgit v1.2.3