github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_unit_str_any_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gmap_test 8 9 import ( 10 "github.com/gogf/gf/container/garray" 11 "github.com/gogf/gf/frame/g" 12 "github.com/gogf/gf/internal/json" 13 "github.com/gogf/gf/util/gconv" 14 "testing" 15 16 "github.com/gogf/gf/container/gmap" 17 "github.com/gogf/gf/test/gtest" 18 ) 19 20 func Test_StrAnyMap_Var(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 var m gmap.StrAnyMap 23 m.Set("a", 1) 24 25 t.Assert(m.Get("a"), 1) 26 t.Assert(m.Size(), 1) 27 t.Assert(m.IsEmpty(), false) 28 29 t.Assert(m.GetOrSet("b", "2"), "2") 30 t.Assert(m.SetIfNotExist("b", "2"), false) 31 32 t.Assert(m.SetIfNotExist("c", 3), true) 33 34 t.Assert(m.Remove("b"), "2") 35 t.Assert(m.Contains("b"), false) 36 37 t.AssertIN("c", m.Keys()) 38 t.AssertIN("a", m.Keys()) 39 t.AssertIN(3, m.Values()) 40 t.AssertIN(1, m.Values()) 41 42 m.Flip() 43 t.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"}) 44 45 m.Clear() 46 t.Assert(m.Size(), 0) 47 t.Assert(m.IsEmpty(), true) 48 }) 49 } 50 51 func Test_StrAnyMap_Basic(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 m := gmap.NewStrAnyMap() 54 m.Set("a", 1) 55 56 t.Assert(m.Get("a"), 1) 57 t.Assert(m.Size(), 1) 58 t.Assert(m.IsEmpty(), false) 59 60 t.Assert(m.GetOrSet("b", "2"), "2") 61 t.Assert(m.SetIfNotExist("b", "2"), false) 62 63 t.Assert(m.SetIfNotExist("c", 3), true) 64 65 t.Assert(m.Remove("b"), "2") 66 t.Assert(m.Contains("b"), false) 67 68 t.AssertIN("c", m.Keys()) 69 t.AssertIN("a", m.Keys()) 70 t.AssertIN(3, m.Values()) 71 t.AssertIN(1, m.Values()) 72 73 m.Flip() 74 t.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"}) 75 76 m.Clear() 77 t.Assert(m.Size(), 0) 78 t.Assert(m.IsEmpty(), true) 79 80 m2 := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"}) 81 t.Assert(m2.Map(), map[string]interface{}{"a": 1, "b": "2"}) 82 }) 83 } 84 85 func Test_StrAnyMap_Set_Fun(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 m := gmap.NewStrAnyMap() 88 89 m.GetOrSetFunc("a", getAny) 90 m.GetOrSetFuncLock("b", getAny) 91 t.Assert(m.Get("a"), 123) 92 t.Assert(m.Get("b"), 123) 93 t.Assert(m.SetIfNotExistFunc("a", getAny), false) 94 t.Assert(m.SetIfNotExistFunc("c", getAny), true) 95 96 t.Assert(m.SetIfNotExistFuncLock("b", getAny), false) 97 t.Assert(m.SetIfNotExistFuncLock("d", getAny), true) 98 }) 99 } 100 101 func Test_StrAnyMap_Batch(t *testing.T) { 102 gtest.C(t, func(t *gtest.T) { 103 m := gmap.NewStrAnyMap() 104 105 m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3}) 106 t.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3}) 107 m.Removes([]string{"a", "b"}) 108 t.Assert(m.Map(), map[string]interface{}{"c": 3}) 109 }) 110 } 111 112 func Test_StrAnyMap_Iterator(t *testing.T) { 113 gtest.C(t, func(t *gtest.T) { 114 expect := map[string]interface{}{"a": true, "b": false} 115 m := gmap.NewStrAnyMapFrom(expect) 116 m.Iterator(func(k string, v interface{}) bool { 117 t.Assert(expect[k], v) 118 return true 119 }) 120 // 断言返回值对遍历控制 121 i := 0 122 j := 0 123 m.Iterator(func(k string, v interface{}) bool { 124 i++ 125 return true 126 }) 127 m.Iterator(func(k string, v interface{}) bool { 128 j++ 129 return false 130 }) 131 t.Assert(i, 2) 132 t.Assert(j, 1) 133 }) 134 } 135 136 func Test_StrAnyMap_Lock(t *testing.T) { 137 gtest.C(t, func(t *gtest.T) { 138 expect := map[string]interface{}{"a": true, "b": false} 139 140 m := gmap.NewStrAnyMapFrom(expect) 141 m.LockFunc(func(m map[string]interface{}) { 142 t.Assert(m, expect) 143 }) 144 m.RLockFunc(func(m map[string]interface{}) { 145 t.Assert(m, expect) 146 }) 147 }) 148 } 149 func Test_StrAnyMap_Clone(t *testing.T) { 150 gtest.C(t, func(t *gtest.T) { 151 //clone 方法是深克隆 152 m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"}) 153 154 m_clone := m.Clone() 155 m.Remove("a") 156 //修改原 map,clone 后的 map 不影响 157 t.AssertIN("a", m_clone.Keys()) 158 159 m_clone.Remove("b") 160 //修改clone map,原 map 不影响 161 t.AssertIN("b", m.Keys()) 162 }) 163 } 164 func Test_StrAnyMap_Merge(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 m1 := gmap.NewStrAnyMap() 167 m2 := gmap.NewStrAnyMap() 168 m1.Set("a", 1) 169 m2.Set("b", "2") 170 m1.Merge(m2) 171 t.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"}) 172 }) 173 } 174 175 func Test_StrAnyMap_Map(t *testing.T) { 176 gtest.C(t, func(t *gtest.T) { 177 m := gmap.NewStrAnyMap() 178 m.Set("1", 1) 179 m.Set("2", 2) 180 t.Assert(m.Get("1"), 1) 181 t.Assert(m.Get("2"), 2) 182 data := m.Map() 183 t.Assert(data["1"], 1) 184 t.Assert(data["2"], 2) 185 data["3"] = 3 186 t.Assert(m.Get("3"), 3) 187 m.Set("4", 4) 188 t.Assert(data["4"], 4) 189 }) 190 } 191 192 func Test_StrAnyMap_MapCopy(t *testing.T) { 193 gtest.C(t, func(t *gtest.T) { 194 m := gmap.NewStrAnyMap() 195 m.Set("1", 1) 196 m.Set("2", 2) 197 t.Assert(m.Get("1"), 1) 198 t.Assert(m.Get("2"), 2) 199 data := m.MapCopy() 200 t.Assert(data["1"], 1) 201 t.Assert(data["2"], 2) 202 data["3"] = 3 203 t.Assert(m.Get("3"), nil) 204 m.Set("4", 4) 205 t.Assert(data["4"], nil) 206 }) 207 } 208 209 func Test_StrAnyMap_FilterEmpty(t *testing.T) { 210 gtest.C(t, func(t *gtest.T) { 211 m := gmap.NewStrAnyMap() 212 m.Set("1", 0) 213 m.Set("2", 2) 214 t.Assert(m.Size(), 2) 215 t.Assert(m.Get("1"), 0) 216 t.Assert(m.Get("2"), 2) 217 m.FilterEmpty() 218 t.Assert(m.Size(), 1) 219 t.Assert(m.Get("2"), 2) 220 }) 221 } 222 223 func Test_StrAnyMap_Json(t *testing.T) { 224 // Marshal 225 gtest.C(t, func(t *gtest.T) { 226 data := g.MapStrAny{ 227 "k1": "v1", 228 "k2": "v2", 229 } 230 m1 := gmap.NewStrAnyMapFrom(data) 231 b1, err1 := json.Marshal(m1) 232 b2, err2 := json.Marshal(data) 233 t.Assert(err1, err2) 234 t.Assert(b1, b2) 235 }) 236 // Unmarshal 237 gtest.C(t, func(t *gtest.T) { 238 data := g.MapStrAny{ 239 "k1": "v1", 240 "k2": "v2", 241 } 242 b, err := json.Marshal(data) 243 t.Assert(err, nil) 244 245 m := gmap.NewStrAnyMap() 246 err = json.UnmarshalUseNumber(b, m) 247 t.Assert(err, nil) 248 t.Assert(m.Get("k1"), data["k1"]) 249 t.Assert(m.Get("k2"), data["k2"]) 250 }) 251 gtest.C(t, func(t *gtest.T) { 252 data := g.MapStrAny{ 253 "k1": "v1", 254 "k2": "v2", 255 } 256 b, err := json.Marshal(data) 257 t.Assert(err, nil) 258 259 var m gmap.StrAnyMap 260 err = json.UnmarshalUseNumber(b, &m) 261 t.Assert(err, nil) 262 t.Assert(m.Get("k1"), data["k1"]) 263 t.Assert(m.Get("k2"), data["k2"]) 264 }) 265 } 266 267 func Test_StrAnyMap_Pop(t *testing.T) { 268 gtest.C(t, func(t *gtest.T) { 269 m := gmap.NewStrAnyMapFrom(g.MapStrAny{ 270 "k1": "v1", 271 "k2": "v2", 272 }) 273 t.Assert(m.Size(), 2) 274 275 k1, v1 := m.Pop() 276 t.AssertIN(k1, g.Slice{"k1", "k2"}) 277 t.AssertIN(v1, g.Slice{"v1", "v2"}) 278 t.Assert(m.Size(), 1) 279 k2, v2 := m.Pop() 280 t.AssertIN(k2, g.Slice{"k1", "k2"}) 281 t.AssertIN(v2, g.Slice{"v1", "v2"}) 282 t.Assert(m.Size(), 0) 283 284 t.AssertNE(k1, k2) 285 t.AssertNE(v1, v2) 286 }) 287 } 288 289 func Test_StrAnyMap_Pops(t *testing.T) { 290 gtest.C(t, func(t *gtest.T) { 291 m := gmap.NewStrAnyMapFrom(g.MapStrAny{ 292 "k1": "v1", 293 "k2": "v2", 294 "k3": "v3", 295 }) 296 t.Assert(m.Size(), 3) 297 298 kArray := garray.New() 299 vArray := garray.New() 300 for k, v := range m.Pops(1) { 301 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 302 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 303 kArray.Append(k) 304 vArray.Append(v) 305 } 306 t.Assert(m.Size(), 2) 307 for k, v := range m.Pops(2) { 308 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 309 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 310 kArray.Append(k) 311 vArray.Append(v) 312 } 313 t.Assert(m.Size(), 0) 314 315 t.Assert(kArray.Unique().Len(), 3) 316 t.Assert(vArray.Unique().Len(), 3) 317 }) 318 } 319 320 func TestStrAnyMap_UnmarshalValue(t *testing.T) { 321 type V struct { 322 Name string 323 Map *gmap.StrAnyMap 324 } 325 // JSON 326 gtest.C(t, func(t *gtest.T) { 327 var v *V 328 err := gconv.Struct(map[string]interface{}{ 329 "name": "john", 330 "map": []byte(`{"k1":"v1","k2":"v2"}`), 331 }, &v) 332 t.Assert(err, nil) 333 t.Assert(v.Name, "john") 334 t.Assert(v.Map.Size(), 2) 335 t.Assert(v.Map.Get("k1"), "v1") 336 t.Assert(v.Map.Get("k2"), "v2") 337 }) 338 // Map 339 gtest.C(t, func(t *gtest.T) { 340 var v *V 341 err := gconv.Struct(map[string]interface{}{ 342 "name": "john", 343 "map": g.Map{ 344 "k1": "v1", 345 "k2": "v2", 346 }, 347 }, &v) 348 t.Assert(err, nil) 349 t.Assert(v.Name, "john") 350 t.Assert(v.Map.Size(), 2) 351 t.Assert(v.Map.Get("k1"), "v1") 352 t.Assert(v.Map.Get("k2"), "v2") 353 }) 354 }