diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-08 10:25:15 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-08 12:30:50 +0200 |
commit | 31a8bb8c071c6f2ca8cbd73057912932a1e7e943 (patch) | |
tree | 698f8d82cf1dc2bb6c56ba3a615ef4b441361c6a /tpl/collections/collections_test.go | |
parent | 8e825ddf5b31ce5fe570c78ac7a78c8056fb60f9 (diff) |
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
Diffstat (limited to 'tpl/collections/collections_test.go')
-rw-r--r-- | tpl/collections/collections_test.go | 83 |
1 files changed, 1 insertions, 82 deletions
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index dc8929f39..c8a7207ea 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -25,7 +25,6 @@ import ( "testing" "time" - "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" @@ -644,83 +643,7 @@ func TestShuffleRandomising(t *testing.T) { } } -var _ collections.Slicer = (*tstSlicer)(nil) -var _ collections.Slicer = (*tstSlicerIn1)(nil) -var _ collections.Slicer = (*tstSlicerIn2)(nil) -var _ testSlicerInterface = (*tstSlicerIn1)(nil) -var _ testSlicerInterface = (*tstSlicerIn1)(nil) - -type testSlicerInterface interface { - Name() string -} - -type testSlicerInterfaces []testSlicerInterface - -type tstSlicerIn1 struct { - name string -} - -type tstSlicerIn2 struct { - name string -} - -type tstSlicer struct { - name string -} - -func (p *tstSlicerIn1) Slice(in interface{}) (interface{}, error) { - items := in.([]interface{}) - result := make(testSlicerInterfaces, len(items)) - for i, v := range items { - switch vv := v.(type) { - case testSlicerInterface: - result[i] = vv - default: - return nil, errors.New("invalid type") - } - - } - return result, nil -} - -func (p *tstSlicerIn2) Slice(in interface{}) (interface{}, error) { - items := in.([]interface{}) - result := make(testSlicerInterfaces, len(items)) - for i, v := range items { - switch vv := v.(type) { - case testSlicerInterface: - result[i] = vv - default: - return nil, errors.New("invalid type") - } - } - return result, nil -} - -func (p *tstSlicerIn1) Name() string { - return p.Name() -} - -func (p *tstSlicerIn2) Name() string { - return p.Name() -} - -func (p *tstSlicer) Slice(in interface{}) (interface{}, error) { - items := in.([]interface{}) - result := make(tstSlicers, len(items)) - for i, v := range items { - switch vv := v.(type) { - case *tstSlicer: - result[i] = vv - default: - return nil, errors.New("invalid type") - } - } - return result, nil -} - -type tstSlicers []*tstSlicer - +// Also see tests in commons/collection. func TestSlice(t *testing.T) { t.Parallel() @@ -731,14 +654,10 @@ func TestSlice(t *testing.T) { expected interface{} }{ {[]interface{}{"a", "b"}, []string{"a", "b"}}, - {[]interface{}{&tstSlicer{"a"}, &tstSlicer{"b"}}, tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}}, - {[]interface{}{&tstSlicer{"a"}, "b"}, []interface{}{&tstSlicer{"a"}, "b"}}, {[]interface{}{}, []interface{}{}}, {[]interface{}{nil}, []interface{}{nil}}, {[]interface{}{5, "b"}, []interface{}{5, "b"}}, {[]interface{}{tstNoStringer{}}, []tstNoStringer{tstNoStringer{}}}, - {[]interface{}{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}, testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}}, - {[]interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}, []interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}}, } { errMsg := fmt.Sprintf("[%d] %v", i, test.args) |