github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/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  	"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  	var (
    34  		emptyInterface any
    35  		nilPointer     *int
    36  	)
    37  
    38  	for i, test := range []struct {
    39  		item    any
    40  		indices []any
    41  		expect  any
    42  		isErr   bool
    43  	}{
    44  		{[]int{0, 1}, []any{0}, 0, false},
    45  		{[]int{0, 1}, []any{9}, nil, false}, // index out of range
    46  		{[]uint{0, 1}, nil, []uint{0, 1}, false},
    47  		{[][]int{{1, 2}, {3, 4}}, []any{0, 0}, 1, false},
    48  		{map[int]int{1: 10, 2: 20}, []any{1}, 10, false},
    49  		{map[int]int{1: 10, 2: 20}, []any{0}, 0, false},
    50  		{map[string]map[string]string{"a": {"b": "c"}}, []any{"a", "b"}, "c", false},
    51  		{[]map[string]map[string]string{{"a": {"b": "c"}}}, []any{0, "a", "b"}, "c", false},
    52  		{map[string]map[string]any{"a": {"b": []string{"c", "d"}}}, []any{"a", "b", 1}, "d", false},
    53  		{maps.Params{"a": "av"}, []any{"A"}, "av", false},
    54  		{maps.Params{"a": map[string]any{"b": "bv"}}, []any{"A", "B"}, "bv", false},
    55  
    56  		// These used to be errors.
    57  		// See issue 10489.
    58  		{nil, nil, nil, false},
    59  		{nil, []any{0}, nil, false},
    60  		{emptyInterface, []any{0}, nil, false},
    61  		{nilPointer, []any{0}, nil, false},
    62  
    63  		// errors
    64  		{[]int{0, 1}, []any{"1"}, nil, true},
    65  		{[]int{0, 1}, []any{nil}, nil, true},
    66  		{tstNoStringer{}, []any{0}, nil, true},
    67  	} {
    68  
    69  		c.Run(fmt.Sprintf("vararg %d", i), func(c *qt.C) {
    70  			errMsg := qt.Commentf("[%d] %v", i, test)
    71  
    72  			result, err := ns.Index(test.item, test.indices...)
    73  
    74  			if test.isErr {
    75  				c.Assert(err, qt.Not(qt.IsNil), errMsg)
    76  				return
    77  			}
    78  			c.Assert(err, qt.IsNil, errMsg)
    79  			c.Assert(result, qt.DeepEquals, test.expect, errMsg)
    80  		})
    81  
    82  		c.Run(fmt.Sprintf("slice %d", i), func(c *qt.C) {
    83  			errMsg := qt.Commentf("[%d] %v", i, test)
    84  
    85  			result, err := ns.Index(test.item, test.indices)
    86  
    87  			if test.isErr {
    88  				c.Assert(err, qt.Not(qt.IsNil), errMsg)
    89  				return
    90  			}
    91  			c.Assert(err, qt.IsNil, errMsg)
    92  			c.Assert(result, qt.DeepEquals, test.expect, errMsg)
    93  		})
    94  	}
    95  }