| 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 collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/common/hmaps" |
| 22 |
"github.com/gohugoio/hugo/parser" |
| 23 |
"github.com/gohugoio/hugo/parser/metadecoders" |
| 24 |
|
| 25 |
qt "github.com/frankban/quicktest" |
| 26 |
) |
| 27 |
|
| 28 |
func TestMerge(t *testing.T) { |
| 29 |
ns := newNs() |
| 30 |
|
| 31 |
simpleMap := map[string]any{"a": 1, "b": 2} |
| 32 |
|
| 33 |
for i, test := range []struct { |
| 34 |
name string |
| 35 |
params []any |
| 36 |
expect any |
| 37 |
isErr bool |
| 38 |
}{ |
| 39 |
{ |
| 40 |
"basic", |
| 41 |
[]any{ |
| 42 |
map[string]any{"a": 42, "c": 3}, |
| 43 |
map[string]any{"a": 1, "b": 2}, |
| 44 |
}, |
| 45 |
map[string]any{"a": 1, "b": 2, "c": 3}, |
| 46 |
false, |
| 47 |
}, |
| 48 |
{ |
| 49 |
"multi", |
| 50 |
[]any{ |
| 51 |
map[string]any{"a": 42, "c": 3, "e": 11}, |
| 52 |
map[string]any{"a": 1, "b": 2}, |
| 53 |
map[string]any{"a": 9, "c": 4, "d": 7}, |
| 54 |
}, |
| 55 |
map[string]any{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, |
| 56 |
false, |
| 57 |
}, |
| 58 |
{ |
| 59 |
"basic case insensitive", |
| 60 |
[]any{ |
| 61 |
map[string]any{"A": 42, "c": 3}, |
| 62 |
map[string]any{"a": 1, "b": 2}, |
| 63 |
}, |
| 64 |
map[string]any{"a": 1, "b": 2, "c": 3}, |
| 65 |
false, |
| 66 |
}, |
| 67 |
{ |
| 68 |
"nested", |
| 69 |
[]any{ |
| 70 |
map[string]any{"a": 42, "c": 3, "b": map[string]any{"d": 55, "e": 66, "f": 3}}, |
| 71 |
map[string]any{"a": 1, "b": map[string]any{"d": 1, "e": 2}}, |
| 72 |
}, |
| 73 |
map[string]any{"a": 1, "b": map[string]any{"d": 1, "e": 2, "f": 3}, "c": 3}, |
| 74 |
false, |
| 75 |
}, |
| 76 |
{ |
| 77 |
// https://github.com/gohugoio/hugo/issues/6633 |
| 78 |
"params dst", |
| 79 |
[]any{ |
| 80 |
map[string]any{"a": 42, "c": 3}, |
| 81 |
hmaps.Params{"a": 1, "b": 2}, |
| 82 |
}, |
| 83 |
hmaps.Params{"a": int(1), "b": int(2), "c": int(3)}, |
| 84 |
false, |
| 85 |
}, |
| 86 |
{ |
| 87 |
"params dst, upper case src", |
| 88 |
[]any{ |
| 89 |
map[string]any{"a": 42, "C": 3}, |
| 90 |
hmaps.Params{"a": 1, "b": 2}, |
| 91 |
}, |
| 92 |
hmaps.Params{"a": int(1), "b": int(2), "c": int(3)}, |
| 93 |
false, |
| 94 |
}, |
| 95 |
{ |
| 96 |
"params src", |
| 97 |
[]any{ |
| 98 |
hmaps.Params{"a": 42, "c": 3}, |
| 99 |
map[string]any{"a": 1, "c": 2}, |
| 100 |
}, |
| 101 |
map[string]any{"a": int(1), "c": int(2)}, |
| 102 |
false, |
| 103 |
}, |
| 104 |
{ |
| 105 |
"params src, upper case dst", |
| 106 |
[]any{ |
| 107 |
hmaps.Params{"a": 42, "c": 3}, |
| 108 |
map[string]any{"a": 1, "C": 2}, |
| 109 |
}, |
| 110 |
map[string]any{"a": int(1), "C": int(2)}, |
| 111 |
false, |
| 112 |
}, |
| 113 |
{ |
| 114 |
"nested, params dst", |
| 115 |
[]any{ |
| 116 |
map[string]any{"a": 42, "c": 3, "b": map[string]any{"d": 55, "e": 66, "f": 3}}, |
| 117 |
hmaps.Params{"a": 1, "b": hmaps.Params{"d": 1, "e": 2}}, |
| 118 |
}, |
| 119 |
hmaps.Params{"a": 1, "b": hmaps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, |
| 120 |
false, |
| 121 |
}, |
| 122 |
{ |
| 123 |
// https://github.com/gohugoio/hugo/issues/7899 |
| 124 |
"matching keys with non-map src value", |
| 125 |
[]any{ |
| 126 |
map[string]any{"k": "v"}, |
| 127 |
map[string]any{"k": map[string]any{"k2": "v2"}}, |
| 128 |
}, |
| 129 |
map[string]any{"k": map[string]any{"k2": "v2"}}, |
| 130 |
false, |
| 131 |
}, |
| 132 |
{"src nil", []any{nil, simpleMap}, simpleMap, false}, |
| 133 |
// Error cases. |
| 134 |
{"dst not a map", []any{nil, "not a map"}, nil, true}, |
| 135 |
{"src not a map", []any{"not a map", simpleMap}, nil, true}, |
| 136 |
{"different map types", []any{map[int]any{32: "a"}, simpleMap}, nil, true}, |
| 137 |
{"all nil", []any{nil, nil}, nil, true}, |
| 138 |
} { |
| 139 |
t.Run(test.name, func(t *testing.T) { |
| 140 |
t.Parallel() |
| 141 |
errMsg := qt.Commentf("[%d] %v", i, test) |
| 142 |
|
| 143 |
c := qt.New(t) |
| 144 |
|
| 145 |
result, err := ns.Merge(test.params...) |
| 146 |
|
| 147 |
if test.isErr { |
| 148 |
c.Assert(err, qt.Not(qt.IsNil), errMsg) |
| 149 |
return |
| 150 |
} |
| 151 |
|
| 152 |
c.Assert(err, qt.IsNil) |
| 153 |
c.Assert(result, qt.DeepEquals, test.expect, errMsg) |
| 154 |
}) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func BenchmarkMerge(b *testing.B) { |
| 159 |
ns := newNs() |
| 160 |
|
| 161 |
for b.Loop() { |
| 162 |
ns.Merge( |
| 163 |
map[string]any{"a": 42, "c": 3, "e": 11}, |
| 164 |
map[string]any{"a": 1, "b": 2}, |
| 165 |
map[string]any{"a": 9, "c": 4, "d": 7}, |
| 166 |
) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestMergeDataFormats(t *testing.T) { |
| 171 |
c := qt.New(t) |
| 172 |
ns := newNs() |
| 173 |
|
| 174 |
toml1 := ` |
| 175 |
V1 = "v1_1" |
| 176 |
|
| 177 |
[V2s] |
| 178 |
V21 = "v21_1" |
| 179 |
|
| 180 |
` |
| 181 |
|
| 182 |
toml2 := ` |
| 183 |
V1 = "v1_2" |
| 184 |
V2 = "v2_2" |
| 185 |
|
| 186 |
[V2s] |
| 187 |
V21 = "v21_2" |
| 188 |
V22 = "v22_2" |
| 189 |
|
| 190 |
` |
| 191 |
|
| 192 |
meta1, err := metadecoders.Default.UnmarshalToMap([]byte(toml1), metadecoders.TOML) |
| 193 |
c.Assert(err, qt.IsNil) |
| 194 |
meta2, err := metadecoders.Default.UnmarshalToMap([]byte(toml2), metadecoders.TOML) |
| 195 |
c.Assert(err, qt.IsNil) |
| 196 |
|
| 197 |
for _, format := range []metadecoders.Format{metadecoders.JSON, metadecoders.YAML, metadecoders.TOML} { |
| 198 |
|
| 199 |
var dataStr1, dataStr2 bytes.Buffer |
| 200 |
err = parser.InterfaceToConfig(meta1, format, &dataStr1) |
| 201 |
c.Assert(err, qt.IsNil) |
| 202 |
err = parser.InterfaceToConfig(meta2, format, &dataStr2) |
| 203 |
c.Assert(err, qt.IsNil) |
| 204 |
|
| 205 |
dst, err := metadecoders.Default.UnmarshalToMap(dataStr1.Bytes(), format) |
| 206 |
c.Assert(err, qt.IsNil) |
| 207 |
src, err := metadecoders.Default.UnmarshalToMap(dataStr2.Bytes(), format) |
| 208 |
c.Assert(err, qt.IsNil) |
| 209 |
|
| 210 |
merged, err := ns.Merge(src, dst) |
| 211 |
c.Assert(err, qt.IsNil) |
| 212 |
|
| 213 |
c.Assert( |
| 214 |
merged, |
| 215 |
qt.DeepEquals, |
| 216 |
map[string]any{ |
| 217 |
"V1": "v1_1", "V2": "v2_2", |
| 218 |
"V2s": map[string]any{"V21": "v21_1", "V22": "v22_2"}, |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestCaseInsensitiveMapLookup(t *testing.T) { |
| 224 |
c := qt.New(t) |
| 225 |
|
| 226 |
m1 := reflect.ValueOf(map[string]any{ |
| 227 |
"a": 1, |
| 228 |
"B": 2, |
| 229 |
}) |
| 230 |
|
| 231 |
m2 := reflect.ValueOf(map[int]any{ |
| 232 |
1: 1, |
| 233 |
2: 2, |
| 234 |
}) |
| 235 |
|
| 236 |
var found bool |
| 237 |
|
| 238 |
a, found := caseInsensitiveLookup(m1, reflect.ValueOf("A")) |
| 239 |
c.Assert(found, qt.Equals, true) |
| 240 |
c.Assert(a.Interface(), qt.Equals, 1) |
| 241 |
|
| 242 |
b, found := caseInsensitiveLookup(m1, reflect.ValueOf("b")) |
| 243 |
c.Assert(found, qt.Equals, true) |
| 244 |
c.Assert(b.Interface(), qt.Equals, 2) |
| 245 |
|
| 246 |
two, found := caseInsensitiveLookup(m2, reflect.ValueOf(2)) |
| 247 |
c.Assert(found, qt.Equals, true) |
| 248 |
c.Assert(two.Interface(), qt.Equals, 2) |
| 249 |
} |