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