2019-07-11 01:12:44 -04:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-18 15:31:34 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2020-08-27 22:34:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
2019-07-11 01:12:44 -04:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2019-07-11 01:12:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerge(t *testing.T) {
|
|
|
|
ns := New(&deps.Deps{})
|
|
|
|
|
|
|
|
simpleMap := map[string]interface{}{"a": 1, "b": 2}
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
name string
|
2020-08-27 22:34:45 -04:00
|
|
|
params []interface{}
|
2019-07-11 01:12:44 -04:00
|
|
|
expect interface{}
|
|
|
|
isErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"basic",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "c": 3},
|
|
|
|
map[string]interface{}{"a": 1, "b": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": 1, "b": 2, "c": 3},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"multi",
|
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "c": 3, "e": 11},
|
|
|
|
map[string]interface{}{"a": 1, "b": 2},
|
|
|
|
map[string]interface{}{"a": 9, "c": 4, "d": 7},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-07-11 01:12:44 -04:00
|
|
|
{
|
|
|
|
"basic case insensitive",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"A": 42, "c": 3},
|
|
|
|
map[string]interface{}{"a": 1, "b": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": 1, "b": 2, "c": 3},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-07-11 01:12:44 -04:00
|
|
|
{
|
|
|
|
"nested",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}},
|
|
|
|
map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2}},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-12-18 15:31:34 -05:00
|
|
|
{
|
|
|
|
// https://github.com/gohugoio/hugo/issues/6633
|
|
|
|
"params dst",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "c": 3},
|
|
|
|
maps.Params{"a": 1, "b": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
maps.Params{"a": int(1), "b": int(2), "c": int(3)},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-12-18 15:31:34 -05:00
|
|
|
{
|
2019-12-19 14:50:30 -05:00
|
|
|
"params dst, upper case src",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "C": 3},
|
|
|
|
maps.Params{"a": 1, "b": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
maps.Params{"a": int(1), "b": int(2), "c": int(3)},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-12-19 14:50:30 -05:00
|
|
|
{
|
2019-12-18 15:31:34 -05:00
|
|
|
"params src",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
maps.Params{"a": 42, "c": 3},
|
|
|
|
map[string]interface{}{"a": 1, "c": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": int(1), "c": int(2)},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-12-19 14:50:30 -05:00
|
|
|
{
|
|
|
|
"params src, upper case dst",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
maps.Params{"a": 42, "c": 3},
|
|
|
|
map[string]interface{}{"a": 1, "C": 2},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"a": int(1), "C": int(2)},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2019-12-19 14:50:30 -05:00
|
|
|
{
|
|
|
|
"nested, params dst",
|
2020-08-27 22:34:45 -04:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}},
|
|
|
|
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2}},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3},
|
|
|
|
false,
|
2020-08-27 22:34:45 -04:00
|
|
|
},
|
2020-10-28 22:44:38 -04:00
|
|
|
{
|
|
|
|
// https://github.com/gohugoio/hugo/issues/7899
|
|
|
|
"matching keys with non-map src value",
|
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{"k": "v"},
|
|
|
|
map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}},
|
|
|
|
},
|
2020-12-02 07:23:25 -05:00
|
|
|
map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}},
|
|
|
|
false,
|
2020-10-28 22:44:38 -04:00
|
|
|
},
|
2020-08-27 22:34:45 -04:00
|
|
|
{"src nil", []interface{}{nil, simpleMap}, simpleMap, false},
|
2019-07-11 01:12:44 -04:00
|
|
|
// Error cases.
|
2020-08-27 22:34:45 -04:00
|
|
|
{"dst not a map", []interface{}{nil, "not a map"}, nil, true},
|
|
|
|
{"src not a map", []interface{}{"not a map", simpleMap}, nil, true},
|
|
|
|
{"different map types", []interface{}{map[int]interface{}{32: "a"}, simpleMap}, nil, true},
|
|
|
|
{"all nil", []interface{}{nil, nil}, nil, true},
|
2019-07-11 01:12:44 -04:00
|
|
|
} {
|
|
|
|
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
2020-08-27 22:34:45 -04:00
|
|
|
result, err := ns.Merge(test.params...)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
if test.isErr {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2019-07-11 01:12:44 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2019-07-11 01:12:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeDataFormats(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-07-11 01:12:44 -04:00
|
|
|
ns := New(&deps.Deps{})
|
|
|
|
|
|
|
|
toml1 := `
|
|
|
|
V1 = "v1_1"
|
|
|
|
|
|
|
|
[V2s]
|
|
|
|
V21 = "v21_1"
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
toml2 := `
|
|
|
|
V1 = "v1_2"
|
|
|
|
V2 = "v2_2"
|
|
|
|
|
|
|
|
[V2s]
|
|
|
|
V21 = "v21_2"
|
|
|
|
V22 = "v22_2"
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
meta1, err := metadecoders.Default.UnmarshalToMap([]byte(toml1), metadecoders.TOML)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
meta2, err := metadecoders.Default.UnmarshalToMap([]byte(toml2), metadecoders.TOML)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
for _, format := range []metadecoders.Format{metadecoders.JSON, metadecoders.YAML, metadecoders.TOML} {
|
|
|
|
|
|
|
|
var dataStr1, dataStr2 bytes.Buffer
|
|
|
|
err = parser.InterfaceToConfig(meta1, format, &dataStr1)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
err = parser.InterfaceToConfig(meta2, format, &dataStr2)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
dst, err := metadecoders.Default.UnmarshalToMap(dataStr1.Bytes(), format)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
src, err := metadecoders.Default.UnmarshalToMap(dataStr2.Bytes(), format)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
merged, err := ns.Merge(src, dst)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
c.Assert(
|
|
|
|
merged,
|
|
|
|
qt.DeepEquals,
|
|
|
|
map[string]interface{}{
|
|
|
|
"V1": "v1_1", "V2": "v2_2",
|
2020-08-27 22:34:45 -04:00
|
|
|
"V2s": map[string]interface{}{"V21": "v21_1", "V22": "v22_2"},
|
|
|
|
})
|
2019-07-11 01:12:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaseInsensitiveMapLookup(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
m1 := reflect.ValueOf(map[string]interface{}{
|
|
|
|
"a": 1,
|
|
|
|
"B": 2,
|
|
|
|
})
|
|
|
|
|
|
|
|
m2 := reflect.ValueOf(map[int]interface{}{
|
|
|
|
1: 1,
|
|
|
|
2: 2,
|
|
|
|
})
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
|
|
|
|
a, found := caseInsensitiveLookup(m1, reflect.ValueOf("A"))
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
|
|
|
c.Assert(a.Interface(), qt.Equals, 1)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
b, found := caseInsensitiveLookup(m1, reflect.ValueOf("b"))
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
|
|
|
c.Assert(b.Interface(), qt.Equals, 2)
|
2019-07-11 01:12:44 -04:00
|
|
|
|
|
|
|
two, found := caseInsensitiveLookup(m2, reflect.ValueOf(2))
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
|
|
|
c.Assert(two.Interface(), qt.Equals, 2)
|
2019-07-11 01:12:44 -04:00
|
|
|
}
|