github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/collections/index_test.go (about)

     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  
    22  	qt "github.com/frankban/quicktest"
    23  	"github.com/gohugoio/hugo/deps"
    24  )
    25  
    26  func TestIndex(t *testing.T) {
    27  	t.Parallel()
    28  	c := qt.New(t)
    29  	ns := New(&deps.Deps{})
    30  
    31  	for i, test := range []struct {
    32  		item    interface{}
    33  		indices []interface{}
    34  		expect  interface{}
    35  		isErr   bool
    36  	}{
    37  		{[]int{0, 1}, []interface{}{0}, 0, false},
    38  		{[]int{0, 1}, []interface{}{9}, nil, false}, // index out of range
    39  		{[]uint{0, 1}, nil, []uint{0, 1}, false},
    40  		{[][]int{{1, 2}, {3, 4}}, []interface{}{0, 0}, 1, false},
    41  		{map[int]int{1: 10, 2: 20}, []interface{}{1}, 10, false},
    42  		{map[int]int{1: 10, 2: 20}, []interface{}{0}, 0, false},
    43  		{map[string]map[string]string{"a": {"b": "c"}}, []interface{}{"a", "b"}, "c", false},
    44  		{[]map[string]map[string]string{{"a": {"b": "c"}}}, []interface{}{0, "a", "b"}, "c", false},
    45  		{map[string]map[string]interface{}{"a": {"b": []string{"c", "d"}}}, []interface{}{"a", "b", 1}, "d", false},
    46  		{map[string]map[string]string{"a": {"b": "c"}}, []interface{}{[]string{"a", "b"}}, "c", false},
    47  		{maps.Params{"a": "av"}, []interface{}{"A"}, "av", false},
    48  		{maps.Params{"a": map[string]interface{}{"b": "bv"}}, []interface{}{"A", "B"}, "bv", false},
    49  		// errors
    50  		{nil, nil, nil, true},
    51  		{[]int{0, 1}, []interface{}{"1"}, nil, true},
    52  		{[]int{0, 1}, []interface{}{nil}, nil, true},
    53  		{tstNoStringer{}, []interface{}{0}, nil, true},
    54  	} {
    55  		c.Run(fmt.Sprint(i), func(c *qt.C) {
    56  			errMsg := qt.Commentf("[%d] %v", i, test)
    57  
    58  			result, err := ns.Index(test.item, test.indices...)
    59  
    60  			if test.isErr {
    61  				c.Assert(err, qt.Not(qt.IsNil), errMsg)
    62  				return
    63  			}
    64  			c.Assert(err, qt.IsNil, errMsg)
    65  			c.Assert(result, qt.DeepEquals, test.expect, errMsg)
    66  		})
    67  	}
    68  }