common/maps: Improve append in Scratch

This commit consolidates the reflective collections handling in `.Scratch` vs the `tpl` package so they use the same code paths.

This commit also adds support for a corner case where a typed slice is appended to a nil or empty `[]interface{}`.

Fixes #5275
This commit is contained in:
Bjørn Erik Pedersen
2018-10-08 10:25:15 +02:00
parent 8e825ddf5b
commit 31a8bb8c07
12 changed files with 391 additions and 192 deletions

View File

@@ -18,11 +18,11 @@ import (
"reflect"
"testing"
"github.com/alecthomas/assert"
"github.com/gohugoio/hugo/deps"
"github.com/stretchr/testify/require"
)
// Also see tests in common/collection.
func TestAppend(t *testing.T) {
t.Parallel()
@@ -36,16 +36,6 @@ func TestAppend(t *testing.T) {
{[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}},
{[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
{[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}},
{nil, []interface{}{"a", "b"}, []string{"a", "b"}},
{nil, []interface{}{nil}, []interface{}{nil}},
{tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
[]interface{}{&tstSlicer{"c"}},
tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}, &tstSlicer{"c"}}},
{&tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
[]interface{}{&tstSlicer{"c"}},
tstSlicers{&tstSlicer{"a"},
&tstSlicer{"b"},
&tstSlicer{"c"}}},
// Errors
{"", []interface{}{[]string{"a", "b"}}, false},
{[]string{"a", "b"}, []interface{}{}, false},
@@ -73,5 +63,4 @@ func TestAppend(t *testing.T) {
}
}
assert.Len(t, ns.Slice(), 0)
}