diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-09 10:15:11 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-10 09:19:01 +0200 |
commit | fe6676c775b8d917a661238f24fd4a9088f25d50 (patch) | |
tree | a84c05a70dc203392db4343a2185cfe0a5714140 /tpl/collections/collections_test.go | |
parent | 7a97d3e6bca1e30826e1662b5f299b66aa8ab385 (diff) |
tpl/collections: Improve type handling in collections.Slice
Fixes #5188
Diffstat (limited to 'tpl/collections/collections_test.go')
-rw-r--r-- | tpl/collections/collections_test.go | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index 07fc4afe6..a02128f37 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -25,6 +25,7 @@ import ( "testing" "time" + "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" @@ -110,6 +111,8 @@ func TestGroup(t *testing.T) { {"b", []tstGrouper2{tstGrouper2{}, tstGrouper2{}}, "b(2)"}, {"a", []*tstGrouper{}, "a(0)"}, {"a", []string{"a", "b"}, false}, + {"a", "asdf", false}, + {"a", nil, false}, {nil, []*tstGrouper{&tstGrouper{}, &tstGrouper{}}, false}, } { errMsg := fmt.Sprintf("[%d] %v", i, test) @@ -633,25 +636,47 @@ func TestShuffleRandomising(t *testing.T) { } } +var _ collections.Slicer = (*tstSlicer)(nil) + +type tstSlicer struct { + name string +} + +func (p *tstSlicer) Slice(items []interface{}) (interface{}, error) { + result := make(tstSlicers, len(items)) + for i, v := range items { + result[i] = v.(*tstSlicer) + } + return result, nil +} + +type tstSlicers []*tstSlicer + func TestSlice(t *testing.T) { t.Parallel() ns := New(&deps.Deps{}) for i, test := range []struct { - args []interface{} + args []interface{} + expected interface{} }{ - {[]interface{}{"a", "b"}}, - // errors - {[]interface{}{5, "b"}}, - {[]interface{}{tstNoStringer{}}}, + {[]interface{}{"a", "b"}, []interface{}{"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{}}, []interface{}{tstNoStringer{}}}, } { errMsg := fmt.Sprintf("[%d] %v", i, test.args) result := ns.Slice(test.args...) - assert.Equal(t, test.args, result, errMsg) + assert.Equal(t, test.expected, result, errMsg) } + + assert.Len(t, ns.Slice(), 0) } func TestUnion(t *testing.T) { |