hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

params_test.go (3956B)

    1 // Copyright 2019 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package maps
   15 
   16 import (
   17 	"testing"
   18 
   19 	qt "github.com/frankban/quicktest"
   20 )
   21 
   22 func TestGetNestedParam(t *testing.T) {
   23 	m := map[string]any{
   24 		"string":          "value",
   25 		"first":           1,
   26 		"with_underscore": 2,
   27 		"nested": map[string]any{
   28 			"color": "blue",
   29 			"nestednested": map[string]any{
   30 				"color": "green",
   31 			},
   32 		},
   33 	}
   34 
   35 	c := qt.New(t)
   36 
   37 	must := func(keyStr, separator string, candidates ...Params) any {
   38 		v, err := GetNestedParam(keyStr, separator, candidates...)
   39 		c.Assert(err, qt.IsNil)
   40 		return v
   41 	}
   42 
   43 	c.Assert(must("first", "_", m), qt.Equals, 1)
   44 	c.Assert(must("First", "_", m), qt.Equals, 1)
   45 	c.Assert(must("with_underscore", "_", m), qt.Equals, 2)
   46 	c.Assert(must("nested_color", "_", m), qt.Equals, "blue")
   47 	c.Assert(must("nested.nestednested.color", ".", m), qt.Equals, "green")
   48 	c.Assert(must("string.name", ".", m), qt.IsNil)
   49 	c.Assert(must("nested.foo", ".", m), qt.IsNil)
   50 }
   51 
   52 // https://github.com/gohugoio/hugo/issues/7903
   53 func TestGetNestedParamFnNestedNewKey(t *testing.T) {
   54 	c := qt.New(t)
   55 
   56 	nested := map[string]any{
   57 		"color": "blue",
   58 	}
   59 	m := map[string]any{
   60 		"nested": nested,
   61 	}
   62 
   63 	existing, nestedKey, owner, err := GetNestedParamFn("nested.new", ".", func(key string) any {
   64 		return m[key]
   65 	})
   66 
   67 	c.Assert(err, qt.IsNil)
   68 	c.Assert(existing, qt.IsNil)
   69 	c.Assert(nestedKey, qt.Equals, "new")
   70 	c.Assert(owner, qt.DeepEquals, nested)
   71 }
   72 
   73 func TestParamsSetAndMerge(t *testing.T) {
   74 	c := qt.New(t)
   75 
   76 	createParamsPair := func() (Params, Params) {
   77 		p1 := Params{"a": "av", "c": "cv", "nested": Params{"al2": "al2v", "cl2": "cl2v"}}
   78 		p2 := Params{"b": "bv", "a": "abv", "nested": Params{"bl2": "bl2v", "al2": "al2bv"}, mergeStrategyKey: ParamsMergeStrategyDeep}
   79 		return p1, p2
   80 	}
   81 
   82 	p1, p2 := createParamsPair()
   83 
   84 	p1.Set(p2)
   85 
   86 	c.Assert(p1, qt.DeepEquals, Params{
   87 		"a": "abv",
   88 		"c": "cv",
   89 		"nested": Params{
   90 			"al2": "al2bv",
   91 			"cl2": "cl2v",
   92 			"bl2": "bl2v",
   93 		},
   94 		"b":              "bv",
   95 		mergeStrategyKey: ParamsMergeStrategyDeep,
   96 	})
   97 
   98 	p1, p2 = createParamsPair()
   99 
  100 	p1.Merge(p2)
  101 
  102 	// Default is to do a shallow merge.
  103 	c.Assert(p1, qt.DeepEquals, Params{
  104 		"c": "cv",
  105 		"nested": Params{
  106 			"al2": "al2v",
  107 			"cl2": "cl2v",
  108 		},
  109 		"b": "bv",
  110 		"a": "av",
  111 	})
  112 
  113 	p1, p2 = createParamsPair()
  114 	p1.SetDefaultMergeStrategy(ParamsMergeStrategyNone)
  115 	p1.Merge(p2)
  116 	p1.DeleteMergeStrategy()
  117 
  118 	c.Assert(p1, qt.DeepEquals, Params{
  119 		"a": "av",
  120 		"c": "cv",
  121 		"nested": Params{
  122 			"al2": "al2v",
  123 			"cl2": "cl2v",
  124 		},
  125 	})
  126 
  127 	p1, p2 = createParamsPair()
  128 	p1.SetDefaultMergeStrategy(ParamsMergeStrategyShallow)
  129 	p1.Merge(p2)
  130 	p1.DeleteMergeStrategy()
  131 
  132 	c.Assert(p1, qt.DeepEquals, Params{
  133 		"a": "av",
  134 		"c": "cv",
  135 		"nested": Params{
  136 			"al2": "al2v",
  137 			"cl2": "cl2v",
  138 		},
  139 		"b": "bv",
  140 	})
  141 
  142 	p1, p2 = createParamsPair()
  143 	p1.SetDefaultMergeStrategy(ParamsMergeStrategyDeep)
  144 	p1.Merge(p2)
  145 	p1.DeleteMergeStrategy()
  146 
  147 	c.Assert(p1, qt.DeepEquals, Params{
  148 		"nested": Params{
  149 			"al2": "al2v",
  150 			"cl2": "cl2v",
  151 			"bl2": "bl2v",
  152 		},
  153 		"b": "bv",
  154 		"a": "av",
  155 		"c": "cv",
  156 	})
  157 
  158 }
  159 
  160 func TestParamsIsZero(t *testing.T) {
  161 	c := qt.New(t)
  162 
  163 	var nilParams Params
  164 
  165 	c.Assert(Params{}.IsZero(), qt.IsTrue)
  166 	c.Assert(nilParams.IsZero(), qt.IsTrue)
  167 	c.Assert(Params{"foo": "bar"}.IsZero(), qt.IsFalse)
  168 	c.Assert(Params{"_merge": "foo", "foo": "bar"}.IsZero(), qt.IsFalse)
  169 	c.Assert(Params{"_merge": "foo"}.IsZero(), qt.IsTrue)
  170 }