hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

index_test.go (2337B)

    1 // Copyright 2017 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package collections
   15 
   16 import (
   17 	"fmt"
   18 	"testing"
   19 
   20 	"github.com/gohugoio/hugo/common/maps"
   21 	"github.com/gohugoio/hugo/config"
   22 	"github.com/gohugoio/hugo/langs"
   23 
   24 	qt "github.com/frankban/quicktest"
   25 	"github.com/gohugoio/hugo/deps"
   26 )
   27 
   28 func TestIndex(t *testing.T) {
   29 	t.Parallel()
   30 	c := qt.New(t)
   31 	ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())})
   32 
   33 	for i, test := range []struct {
   34 		item    any
   35 		indices []any
   36 		expect  any
   37 		isErr   bool
   38 	}{
   39 		{[]int{0, 1}, []any{0}, 0, false},
   40 		{[]int{0, 1}, []any{9}, nil, false}, // index out of range
   41 		{[]uint{0, 1}, nil, []uint{0, 1}, false},
   42 		{[][]int{{1, 2}, {3, 4}}, []any{0, 0}, 1, false},
   43 		{map[int]int{1: 10, 2: 20}, []any{1}, 10, false},
   44 		{map[int]int{1: 10, 2: 20}, []any{0}, 0, false},
   45 		{map[string]map[string]string{"a": {"b": "c"}}, []any{"a", "b"}, "c", false},
   46 		{[]map[string]map[string]string{{"a": {"b": "c"}}}, []any{0, "a", "b"}, "c", false},
   47 		{map[string]map[string]any{"a": {"b": []string{"c", "d"}}}, []any{"a", "b", 1}, "d", false},
   48 		{map[string]map[string]string{"a": {"b": "c"}}, []any{[]string{"a", "b"}}, "c", false},
   49 		{maps.Params{"a": "av"}, []any{"A"}, "av", false},
   50 		{maps.Params{"a": map[string]any{"b": "bv"}}, []any{"A", "B"}, "bv", false},
   51 		// errors
   52 		{nil, nil, nil, true},
   53 		{[]int{0, 1}, []any{"1"}, nil, true},
   54 		{[]int{0, 1}, []any{nil}, nil, true},
   55 		{tstNoStringer{}, []any{0}, nil, true},
   56 	} {
   57 		c.Run(fmt.Sprint(i), func(c *qt.C) {
   58 			errMsg := qt.Commentf("[%d] %v", i, test)
   59 
   60 			result, err := ns.Index(test.item, test.indices...)
   61 
   62 			if test.isErr {
   63 				c.Assert(err, qt.Not(qt.IsNil), errMsg)
   64 				return
   65 			}
   66 			c.Assert(err, qt.IsNil, errMsg)
   67 			c.Assert(result, qt.DeepEquals, test.expect, errMsg)
   68 		})
   69 	}
   70 }