hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit f8212d20009c4b5cc6e1ec733d09531eb6525d9f
parent 78e8a744b34e885e8169bf0a8bb64c73288e555a
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Sun,  1 Jul 2018 20:34:02 +0200

tpl/collections: Return en empty slice in after instead of error

When the given index is out of bounds. So it can safely be used with `with` etc. without extra length checking.

Fixes #4894

Diffstat:
Mtpl/collections/collections.go | 2+-
Mtpl/collections/collections_test.go | 7++++---
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go
@@ -74,7 +74,7 @@ func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, err
 	}
 
 	if indexv >= seqv.Len() {
-		return nil, errors.New("no items left")
+		return seqv.Slice(0, 0).Interface(), nil
 	}
 
 	return seqv.Slice(indexv, seqv.Len()).Interface(), nil
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go
@@ -49,12 +49,13 @@ func TestAfter(t *testing.T) {
 		expect interface{}
 	}{
 		{int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}},
-		{int32(3), []string{"a", "b"}, false},
+		{int32(3), []string{"a", "b"}, []string{}},
 		{int64(2), []int{100, 200, 300}, []int{300}},
-		{100, []int{100, 200}, false},
+		{100, []int{100, 200}, []int{}},
 		{"1", []int{100, 200, 300}, []int{200, 300}},
 		{int64(-1), []int{100, 200, 300}, false},
 		{"noint", []int{100, 200, 300}, false},
+		{2, []string{}, []string{}},
 		{1, nil, false},
 		{nil, []int{100}, false},
 		{1, t, false},
@@ -70,7 +71,7 @@ func TestAfter(t *testing.T) {
 		}
 
 		require.NoError(t, err, errMsg)
-		assert.Equal(t, test.expect, result, errMsg)
+		require.Equal(t, test.expect, result, errMsg)
 	}
 }