maps_test.go (3630B)
1 // Copyright 2018 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 maps 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 PrepareParams(test.input) 77 if !reflect.DeepEqual(test.expected, test.input) { 78 t.Errorf("[%d] Expected\n%#v, got\n%#v\n", i, test.expected, test.input) 79 } 80 }) 81 } 82 } 83 84 func TestToSliceStringMap(t *testing.T) { 85 c := qt.New(t) 86 87 tests := []struct { 88 input any 89 expected []map[string]any 90 }{ 91 { 92 input: []map[string]any{ 93 {"abc": 123}, 94 }, 95 expected: []map[string]any{ 96 {"abc": 123}, 97 }, 98 }, { 99 input: []any{ 100 map[string]any{ 101 "def": 456, 102 }, 103 }, 104 expected: []map[string]any{ 105 {"def": 456}, 106 }, 107 }, 108 } 109 110 for _, test := range tests { 111 v, err := ToSliceStringMap(test.input) 112 c.Assert(err, qt.IsNil) 113 c.Assert(v, qt.DeepEquals, test.expected) 114 } 115 } 116 117 func TestToParamsAndPrepare(t *testing.T) { 118 c := qt.New(t) 119 _, ok := ToParamsAndPrepare(map[string]any{"A": "av"}) 120 c.Assert(ok, qt.IsTrue) 121 122 params, ok := ToParamsAndPrepare(nil) 123 c.Assert(ok, qt.IsTrue) 124 c.Assert(params, qt.DeepEquals, Params{}) 125 } 126 127 func TestRenameKeys(t *testing.T) { 128 c := qt.New(t) 129 130 m := map[string]any{ 131 "a": 32, 132 "ren1": "m1", 133 "ren2": "m1_2", 134 "sub": map[string]any{ 135 "subsub": map[string]any{ 136 "REN1": "m2", 137 "ren2": "m2_2", 138 }, 139 }, 140 "no": map[string]any{ 141 "ren1": "m2", 142 "ren2": "m2_2", 143 }, 144 } 145 146 expected := map[string]any{ 147 "a": 32, 148 "new1": "m1", 149 "new2": "m1_2", 150 "sub": map[string]any{ 151 "subsub": map[string]any{ 152 "new1": "m2", 153 "ren2": "m2_2", 154 }, 155 }, 156 "no": map[string]any{ 157 "ren1": "m2", 158 "ren2": "m2_2", 159 }, 160 } 161 162 renamer, err := NewKeyRenamer( 163 "{ren1,sub/*/ren1}", "new1", 164 "{Ren2,sub/ren2}", "new2", 165 ) 166 c.Assert(err, qt.IsNil) 167 168 renamer.Rename(m) 169 170 if !reflect.DeepEqual(expected, m) { 171 t.Errorf("Expected\n%#v, got\n%#v\n", expected, m) 172 } 173 } 174 175 func TestLookupEqualFold(t *testing.T) { 176 c := qt.New(t) 177 178 m1 := map[string]any{ 179 "a": "av", 180 "B": "bv", 181 } 182 183 v, found := LookupEqualFold(m1, "b") 184 c.Assert(found, qt.IsTrue) 185 c.Assert(v, qt.Equals, "bv") 186 187 m2 := map[string]string{ 188 "a": "av", 189 "B": "bv", 190 } 191 192 v, found = LookupEqualFold(m2, "b") 193 c.Assert(found, qt.IsTrue) 194 c.Assert(v, qt.Equals, "bv") 195 196 }