Debian Codegraph @

common/hmaps/maps_test.go

raw
1 // Copyright 2026 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 hmaps
15
16 import (
17 "fmt"
18 "reflect"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestPrepareParams(t *testing.T) {
25 tests := []struct {
26 input Params
27 expected Params
28 }{
29 {
30 map[string]any{
31 "abC": 32,
32 },
33 Params{
34 "abc": 32,
35 },
36 },
37 {
38 map[string]any{
39 "abC": 32,
40 "deF": map[any]any{
41 23: "A value",
42 24: map[string]any{
43 "AbCDe": "A value",
44 "eFgHi": "Another value",
45 },
46 },
47 "gHi": map[string]any{
48 "J": 25,
49 },
50 "jKl": map[string]string{
51 "M": "26",
52 },
53 },
54 Params{
55 "abc": 32,
56 "def": Params{
57 "23": "A value",
58 "24": Params{
59 "abcde": "A value",
60 "efghi": "Another value",
61 },
62 },
63 "ghi": Params{
64 "j": 25,
65 },
66 "jkl": Params{
67 "m": "26",
68 },
69 },
70 },
71 }
72
73 for i, test := range tests {
74 t.Run(fmt.Sprint(i), func(t *testing.T) {
75 // PrepareParams modifies input.
76 prepareClone := PrepareParamsClone(test.input)
77 PrepareParams(test.input)
78 if !reflect.DeepEqual(test.expected, test.input) {
79 t.Errorf("[%d] Expected\n%#v, got\n%#v\n", i, test.expected, test.input)
80 }
81 if !reflect.DeepEqual(test.expected, prepareClone) {
82 t.Errorf("[%d] Expected\n%#v, got\n%#v\n", i, test.expected, prepareClone)
83 }
84 })
85 }
86 }
87
88 func TestToSliceStringMap(t *testing.T) {
89 c := qt.New(t)
90
91 tests := []struct {
92 input any
93 expected []map[string]any
94 }{
95 {
96 input: []map[string]any{
97 {"abc": 123},
98 },
99 expected: []map[string]any{
100 {"abc": 123},
101 },
102 }, {
103 input: []any{
104 map[string]any{
105 "def": 456,
106 },
107 },
108 expected: []map[string]any{
109 {"def": 456},
110 },
111 },
112 }
113
114 for _, test := range tests {
115 v, err := ToSliceStringMap(test.input)
116 c.Assert(err, qt.IsNil)
117 c.Assert(v, qt.DeepEquals, test.expected)
118 }
119 }
120
121 func TestToParamsAndPrepare(t *testing.T) {
122 c := qt.New(t)
123 _, err := ToParamsAndPrepare(map[string]any{"A": "av"})
124 c.Assert(err, qt.IsNil)
125
126 params, err := ToParamsAndPrepare(nil)
127 c.Assert(err, qt.IsNil)
128 c.Assert(params, qt.DeepEquals, Params{})
129 }
130
131 func TestRenameKeys(t *testing.T) {
132 c := qt.New(t)
133
134 m := map[string]any{
135 "a": 32,
136 "ren1": "m1",
137 "ren2": "m1_2",
138 "sub": map[string]any{
139 "subsub": map[string]any{
140 "REN1": "m2",
141 "ren2": "m2_2",
142 },
143 },
144 "no": map[string]any{
145 "ren1": "m2",
146 "ren2": "m2_2",
147 },
148 }
149
150 expected := map[string]any{
151 "a": 32,
152 "new1": "m1",
153 "new2": "m1_2",
154 "sub": map[string]any{
155 "subsub": map[string]any{
156 "new1": "m2",
157 "ren2": "m2_2",
158 },
159 },
160 "no": map[string]any{
161 "ren1": "m2",
162 "ren2": "m2_2",
163 },
164 }
165
166 renamer, err := NewKeyRenamer(
167 "{ren1,sub/*/ren1}", "new1",
168 "{Ren2,sub/ren2}", "new2",
169 )
170 c.Assert(err, qt.IsNil)
171
172 renamer.Rename(m)
173
174 if !reflect.DeepEqual(expected, m) {
175 t.Errorf("Expected\n%#v, got\n%#v\n", expected, m)
176 }
177 }
178
179 func TestLookupEqualFold(t *testing.T) {
180 c := qt.New(t)
181
182 m1 := map[string]any{
183 "a": "av",
184 "B": "bv",
185 }
186
187 v, k, found := LookupEqualFold(m1, "b")
188 c.Assert(found, qt.IsTrue)
189 c.Assert(v, qt.Equals, "bv")
190 c.Assert(k, qt.Equals, "B")
191
192 m2 := map[string]string{
193 "a": "av",
194 "B": "bv",
195 }
196
197 v, k, found = LookupEqualFold(m2, "b")
198 c.Assert(found, qt.IsTrue)
199 c.Assert(k, qt.Equals, "B")
200 c.Assert(v, qt.Equals, "bv")
201 }