github.com/yaoapp/kun@v0.9.0/maps/strany_test.go (about) 1 package maps 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/yaoapp/kun/interfaces" 8 ) 9 10 func TestStrAnyMake(t *testing.T) { 11 m1 := Map{ 12 "foo": "bar", 13 "nested": Map{ 14 "foo": "bar", 15 }, 16 } 17 assert.Equal(t, "bar", m1.Get("foo")) 18 assert.Equal(t, Map{"foo": "bar"}, m1.Get("nested")) 19 20 m2 := Of(map[string]interface{}{ 21 "foo": "bar", 22 "nested": map[string]interface{}{ 23 "foo": "bar", 24 }, 25 }) 26 assert.Equal(t, "bar", m2.Get("foo")) 27 assert.Equal(t, map[string]interface{}{"foo": "bar"}, m2.Get("nested")) 28 assert.IsType(t, MapStrAny{}, Make()) 29 assert.IsType(t, MapStrAny{}, MakeMap()) 30 assert.IsType(t, MapStrAny{}, MakeMapStr()) 31 assert.IsType(t, MapStrAny{}, MakeStr()) 32 assert.IsType(t, MapStrAny{}, MakeStrAny()) 33 assert.IsType(t, MapStrAny{}, MakeMapStrAny()) 34 assert.IsType(t, MapStrAny{}, Of(map[string]interface{}{"foo": "bar"})) 35 assert.IsType(t, MapStrAny{}, MapOf(map[string]interface{}{"foo": "bar"})) 36 assert.IsType(t, MapStrAny{}, MapStrOf(map[string]interface{}{"foo": "bar"})) 37 assert.IsType(t, MapStrAny{}, StrOf(map[string]interface{}{"foo": "bar"})) 38 assert.IsType(t, MapStrAny{}, StrAnyOf(map[string]interface{}{"foo": "bar"})) 39 assert.IsType(t, MapStrAny{}, MapStrAnyOf(map[string]interface{}{"foo": "bar"})) 40 } 41 42 func TestStrAnySetBasic(t *testing.T) { 43 basic, _, _, _, _ := prepareTestingData() 44 m := MakeMapStrAny() 45 for key, value := range basic { 46 m.Set(key, value) 47 } 48 if assert.Equal(t, 16, m.Len(), "The length of map should be 16") { 49 checkBaiscValues(t, m) 50 } 51 } 52 53 func TestStrAnySetAll(t *testing.T) { 54 _, _, _, _, all := prepareTestingData() 55 m := MakeMapStrAny() 56 for key, value := range all { 57 m.Set(key, value) 58 } 59 if assert.Equal(t, 22, m.Len(), "The length of map should be 22") { 60 checkBaiscValues(t, m) 61 checkArrayValues(t, m) 62 checkSliceValues(t, m) 63 checkMapValues(t, m) 64 checkNestedValues(t, m) 65 checkStructValues(t, m) 66 } 67 } 68 69 func TestStrAnyKeys(t *testing.T) { 70 _, _, _, _, all := prepareTestingData() 71 keys := Of(all).Keys() 72 assert.Equal(t, []string{ 73 "arrayint64", 74 "bool", 75 "byte", 76 "float32", 77 "float64", 78 "int", 79 "int16", 80 "int32", 81 "int64", 82 "int8", 83 "mapint64", 84 "nested", 85 "nested2", 86 "sliceint64", 87 "string", 88 "struct", 89 "uint", 90 "uint16", 91 "uint32", 92 "uint64", 93 "uint8", 94 "uintptr"}, keys) 95 } 96 97 func TestStrAnyKeysValues(t *testing.T) { 98 _, _, _, _, all := prepareTestingData() 99 m := Of(all) 100 keys := m.Keys() 101 values := m.Values() 102 if assert.Equal(t, 22, len(keys), "The length of map should be 21") { 103 for i := 0; i < 22; i++ { 104 key := keys[i] 105 value := values[i] 106 assert.Equal(t, value, m.Get(key)) 107 } 108 } 109 } 110 111 func TestStrAnyRange(t *testing.T) { 112 _, _, _, _, all := prepareTestingData() 113 m := Of(all) 114 values := []interface{}{} 115 m.Range(func(key string, value interface{}) bool { 116 values = append(values, value) 117 return true 118 }) 119 assert.Equal(t, m.Len(), len(values)) 120 121 values = []interface{}{} 122 m.Range(func(key string, value interface{}) bool { 123 values = append(values, value) 124 return false 125 }) 126 assert.Equal(t, 1, len(values)) 127 128 } 129 130 func TestStrAnyFlatten(t *testing.T) { 131 _, _, _, _, all := prepareTestingData() 132 flatten := Of(all).Flatten() 133 134 if assert.Equal(t, 64, flatten.Len(), "The length of map should be 64") { 135 values := flatten.Values() 136 for i, key := range flatten.Keys() { 137 assert.Equal(t, values[i], flatten.Get(key)) 138 } 139 } 140 } 141 142 func TestStrAnyHas(t *testing.T) { 143 _, _, _, _, all := prepareTestingData() 144 flatten := Of(all).Flatten() 145 keys := flatten.Keys() 146 if assert.Equal(t, 64, len(keys), "The length of keys should be 64") { 147 for _, key := range keys { 148 assert.True(t, flatten.Has(key)) 149 } 150 } 151 assert.False(t, flatten.Has("not_existed_key")) 152 } 153 154 func TestStrAnyDel(t *testing.T) { 155 _, _, _, _, all := prepareTestingData() 156 m := Of(all) 157 keys := m.Keys() 158 if assert.Equal(t, 22, len(keys), "The length of keys should be 22") { 159 for _, key := range keys { 160 m.Del(key) 161 } 162 } 163 assert.Equal(t, 0, m.Len()) 164 } 165 166 func TestStrAnyGetAndDel(t *testing.T) { 167 _, _, _, _, all := prepareTestingData() 168 m := Of(all) 169 keys := m.Keys() 170 valuesBefore := m.Values() 171 valuesAfter := []interface{}{} 172 if assert.Equal(t, 22, len(keys), "The length of keys should be 22") { 173 for _, key := range keys { 174 valuesAfter = append(valuesAfter, m.GetAndDel(key)) 175 } 176 } 177 assert.Equal(t, 0, m.Len()) 178 assert.Equal(t, 0, len(m.Values())) 179 assert.Equal(t, valuesBefore, valuesAfter) 180 assert.Nil(t, m.GetAndDel("not-exists")) 181 } 182 183 func TestStrAnyGetOrSet(t *testing.T) { 184 _, _, _, _, all := prepareTestingData() 185 m := Of(all) 186 keys := m.Keys() 187 valuesBefore := m.Values() 188 valuesAfter := []interface{}{} 189 if assert.Equal(t, 22, len(keys), "The length of keys should be 22") { 190 for _, key := range keys { 191 valuesAfter = append(valuesAfter, m.GetOrSet(key, "getorset")) 192 } 193 } 194 assert.Equal(t, 22, m.Len()) 195 assert.Equal(t, valuesBefore, valuesAfter) 196 197 value := m.GetOrSet("new-key", "getorset") 198 assert.Equal(t, 23, m.Len()) 199 assert.Equal(t, value, "getorset") 200 assert.Equal(t, value, m.Get("new-key")) 201 } 202 203 func TestStrAnyIsEmpty(t *testing.T) { 204 _, _, _, _, all := prepareTestingData() 205 m := Of(all) 206 assert.False(t, m.IsEmpty()) 207 208 keys := m.Keys() 209 if assert.Equal(t, 22, len(keys), "The length of keys should be 22") { 210 for _, key := range keys { 211 m.Del(key) 212 } 213 } 214 assert.True(t, m.IsEmpty()) 215 } 216 217 func TestStrAnyMerge(t *testing.T) { 218 basic, array, slice, _, _ := prepareTestingData() 219 m := Of(basic) 220 221 var new interfaces.MapStr = Of(array) 222 m.Merge(new) 223 if assert.Equal(t, 16, m.Len(), "The length of keys should be 22") { 224 assert.Equal(t, int64(64), m.Dot().Get("int64.0")) 225 assert.Equal(t, int64(64), m.Dot().Get("int64.1")) 226 } 227 228 var new2 interfaces.MapStr = Of(slice) 229 m.Merge(new, new2) 230 if assert.Equal(t, 16, m.Len(), "The length of keys should be 22") { 231 assert.Equal(t, int64(64), m.Dot().Get("int64.0")) 232 assert.Equal(t, int64(64), m.Dot().Get("int64.1")) 233 assert.Equal(t, int64(64), m.Dot().Get("int64.2")) 234 } 235 } 236 237 func TestStrAnyUnDot(t *testing.T) { 238 m1 := Map{ 239 "foo": "bar", 240 "dot.hello.world": "hello world", 241 "dot.bingo.world": "hello bingo", 242 "nested": Map{ 243 "foo": "bar", 244 }, 245 } 246 res := m1.UnFlatten() 247 dot, ok := res.Get("dot").(Map) 248 assert.True(t, ok) 249 if ok { 250 assert.Equal(t, dot.Get("hello"), Map{"world": "hello world"}) 251 assert.Equal(t, dot.Get("bingo"), Map{"world": "hello bingo"}) 252 } 253 } 254 255 func checkArrayValues(t *testing.T, m interfaces.MapStrAny) { 256 assert.Equal(t, [2]int64{64, 64}, m.Get("arrayint64")) 257 } 258 259 func checkSliceValues(t *testing.T, m interfaces.MapStrAny) { 260 assert.Equal(t, []int64{64, 64, 64}, m.Get("sliceint64")) 261 } 262 263 func checkMapValues(t *testing.T, m interfaces.MapStrAny) { 264 assert.Equal(t, map[int64]interface{}{64: "hello"}, m.Get("mapint64")) 265 } 266 267 func checkStructValues(t *testing.T, m interfaces.MapStrAny) { 268 assert.Equal(t, 269 struct { 270 Name string 271 Value interface{} 272 }{Name: "unit-test", Value: "hello"}, m.Get("struct")) 273 } 274 275 func checkNestedValues(t *testing.T, m interfaces.MapStrAny) { 276 assert.Equal(t, map[string]interface{}{ 277 "basic": map[string]interface{}{ 278 "int64": int64(64), 279 "int32": int32(32), 280 "int16": int16(16), 281 "int8": int8(8), 282 "int": 1, 283 "uint64": uint64(64), 284 "uint32": uint32(32), 285 "uint16": uint16(16), 286 "uint8": uint8(8), 287 "uint": uint(1), 288 "float64": float64(9.65), 289 "float32": float32(9.65), 290 "byte": byte(55), 291 "bool": true, 292 "uintptr": uintptr(19), 293 "string": "string", 294 }, 295 }, m.Get("nested")) 296 } 297 298 func checkBaiscValues(t *testing.T, m interfaces.MapStrAny) { 299 assert.Equal(t, int64(64), m.Get("int64")) 300 assert.Equal(t, int32(32), m.Get("int32")) 301 assert.Equal(t, int16(16), m.Get("int16")) 302 assert.Equal(t, int8(8), m.Get("int8")) 303 assert.Equal(t, int(1), m.Get("int")) 304 assert.Equal(t, uint64(64), m.Get("uint64")) 305 assert.Equal(t, uint32(32), m.Get("uint32")) 306 assert.Equal(t, uint16(16), m.Get("uint16")) 307 assert.Equal(t, uint8(8), m.Get("uint8")) 308 assert.Equal(t, uint(1), m.Get("uint")) 309 assert.Equal(t, float64(9.65), m.Get("float64")) 310 assert.Equal(t, float32(9.65), m.Get("float32")) 311 assert.Equal(t, byte(55), m.Get("byte")) 312 assert.Equal(t, true, m.Get("bool")) 313 assert.Equal(t, uintptr(19), m.Get("uintptr")) 314 assert.Equal(t, "string", m.Get("string")) 315 } 316 317 // prepareTestingData prepare the data for testing 318 // baiscValues, arrayValues, sliceValues, mapValues, allValues 319 func prepareTestingData() (baiscValues, arrayValues, sliceValues, mapValues, allValues map[string]interface{}) { 320 321 var structValue = struct { 322 Name string 323 Value interface{} 324 }{Name: "unit-test", Value: "hello"} 325 326 baiscValues = map[string]interface{}{ 327 "int64": int64(64), 328 "int32": int32(32), 329 "int16": int16(16), 330 "int8": int8(8), 331 "int": 1, 332 "uint64": uint64(64), 333 "uint32": uint32(32), 334 "uint16": uint16(16), 335 "uint8": uint8(8), 336 "uint": uint(1), 337 "float64": float64(9.65), 338 "float32": float32(9.65), 339 "byte": byte(55), 340 "bool": true, 341 "uintptr": uintptr(19), 342 "string": "string", 343 } 344 345 arrayValues = map[string]interface{}{ 346 "int64": [2]int64{64, 64}, 347 } 348 349 sliceValues = map[string]interface{}{ 350 "int64": []int64{64, 64, 64}, 351 } 352 353 mapValues = map[string]interface{}{ 354 "int64": map[int64]interface{}{64: "hello"}, 355 } 356 357 allValues = map[string]interface{}{} 358 for key, value := range baiscValues { 359 allValues[key] = value 360 } 361 for key, value := range arrayValues { 362 allValues["array"+key] = value 363 } 364 for key, value := range sliceValues { 365 allValues["slice"+key] = value 366 } 367 for key, value := range mapValues { 368 allValues["map"+key] = value 369 } 370 371 allValues["struct"] = structValue 372 allValues["nested"] = map[string]interface{}{} 373 for key, value := range baiscValues { 374 allValues[key] = value 375 } 376 allValues["nested"].(map[string]interface{})["basic"] = map[string]interface{}{} 377 for key, value := range baiscValues { 378 allValues["nested"].(map[string]interface{})["basic"].(map[string]interface{})[key] = value 379 } 380 381 allValues["nested2"] = Map{ 382 "basic": Of(baiscValues), 383 } 384 385 return baiscValues, arrayValues, sliceValues, mapValues, allValues 386 }