github.com/wangyougui/gf/v2@v2.6.5/util/gutil/gutil_z_unit_map_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 this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gutil_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/frame/g" 13 "github.com/wangyougui/gf/v2/test/gtest" 14 "github.com/wangyougui/gf/v2/util/gutil" 15 ) 16 17 func Test_MapCopy(t *testing.T) { 18 gtest.C(t, func(t *gtest.T) { 19 m1 := g.Map{ 20 "k1": "v1", 21 } 22 m2 := gutil.MapCopy(m1) 23 m2["k2"] = "v2" 24 25 t.Assert(m1["k1"], "v1") 26 t.Assert(m1["k2"], nil) 27 t.Assert(m2["k1"], "v1") 28 t.Assert(m2["k2"], "v2") 29 }) 30 } 31 32 func Test_MapContains(t *testing.T) { 33 gtest.C(t, func(t *gtest.T) { 34 m1 := g.Map{ 35 "k1": "v1", 36 } 37 t.Assert(gutil.MapContains(m1, "k1"), true) 38 t.Assert(gutil.MapContains(m1, "K1"), false) 39 t.Assert(gutil.MapContains(m1, "k2"), false) 40 m2 := g.Map{} 41 t.Assert(gutil.MapContains(m2, "k1"), false) 42 }) 43 } 44 45 func Test_MapDelete(t *testing.T) { 46 gtest.C(t, func(t *gtest.T) { 47 m1 := g.Map{ 48 "k1": "v1", 49 } 50 gutil.MapDelete(m1, "k1") 51 gutil.MapDelete(m1, "K1") 52 m2 := g.Map{} 53 gutil.MapDelete(m2, "k1") 54 }) 55 } 56 57 func Test_MapMerge(t *testing.T) { 58 gtest.C(t, func(t *gtest.T) { 59 m1 := g.Map{ 60 "k1": "v1", 61 } 62 m2 := g.Map{ 63 "k2": "v2", 64 } 65 m3 := g.Map{ 66 "k3": "v3", 67 } 68 gutil.MapMerge(m1, m2, m3, nil) 69 t.Assert(m1["k1"], "v1") 70 t.Assert(m1["k2"], "v2") 71 t.Assert(m1["k3"], "v3") 72 t.Assert(m2["k1"], nil) 73 t.Assert(m3["k1"], nil) 74 gutil.MapMerge(nil) 75 }) 76 } 77 78 func Test_MapMergeCopy(t *testing.T) { 79 gtest.C(t, func(t *gtest.T) { 80 m1 := g.Map{ 81 "k1": "v1", 82 } 83 m2 := g.Map{ 84 "k2": "v2", 85 } 86 m3 := g.Map{ 87 "k3": "v3", 88 } 89 m := gutil.MapMergeCopy(m1, m2, m3, nil) 90 t.Assert(m["k1"], "v1") 91 t.Assert(m["k2"], "v2") 92 t.Assert(m["k3"], "v3") 93 t.Assert(m1["k1"], "v1") 94 t.Assert(m1["k2"], nil) 95 t.Assert(m2["k1"], nil) 96 t.Assert(m3["k1"], nil) 97 }) 98 } 99 100 func Test_MapPossibleItemByKey(t *testing.T) { 101 gtest.C(t, func(t *gtest.T) { 102 m := g.Map{ 103 "name": "guo", 104 "NickName": "john", 105 } 106 k, v := gutil.MapPossibleItemByKey(m, "NAME") 107 t.Assert(k, "name") 108 t.Assert(v, "guo") 109 110 k, v = gutil.MapPossibleItemByKey(m, "nick name") 111 t.Assert(k, "NickName") 112 t.Assert(v, "john") 113 114 k, v = gutil.MapPossibleItemByKey(m, "none") 115 t.Assert(k, "") 116 t.Assert(v, nil) 117 }) 118 } 119 120 func Test_MapContainsPossibleKey(t *testing.T) { 121 gtest.C(t, func(t *gtest.T) { 122 m := g.Map{ 123 "name": "guo", 124 "NickName": "john", 125 } 126 t.Assert(gutil.MapContainsPossibleKey(m, "name"), true) 127 t.Assert(gutil.MapContainsPossibleKey(m, "NAME"), true) 128 t.Assert(gutil.MapContainsPossibleKey(m, "nickname"), true) 129 t.Assert(gutil.MapContainsPossibleKey(m, "nick name"), true) 130 t.Assert(gutil.MapContainsPossibleKey(m, "nick_name"), true) 131 t.Assert(gutil.MapContainsPossibleKey(m, "nick-name"), true) 132 t.Assert(gutil.MapContainsPossibleKey(m, "nick.name"), true) 133 t.Assert(gutil.MapContainsPossibleKey(m, "none"), false) 134 }) 135 } 136 137 func Test_MapOmitEmpty(t *testing.T) { 138 gtest.C(t, func(t *gtest.T) { 139 m := g.Map{ 140 "k1": "john", 141 "e1": "", 142 "e2": 0, 143 "e3": nil, 144 "k2": "smith", 145 } 146 gutil.MapOmitEmpty(m) 147 t.Assert(len(m), 2) 148 t.AssertNE(m["k1"], nil) 149 t.AssertNE(m["k2"], nil) 150 m1 := g.Map{} 151 gutil.MapOmitEmpty(m1) 152 t.Assert(len(m1), 0) 153 }) 154 } 155 156 func Test_MapToSlice(t *testing.T) { 157 gtest.C(t, func(t *gtest.T) { 158 m := g.Map{ 159 "k1": "v1", 160 "k2": "v2", 161 } 162 s := gutil.MapToSlice(m) 163 t.Assert(len(s), 4) 164 t.AssertIN(s[0], g.Slice{"k1", "k2", "v1", "v2"}) 165 t.AssertIN(s[1], g.Slice{"k1", "k2", "v1", "v2"}) 166 t.AssertIN(s[2], g.Slice{"k1", "k2", "v1", "v2"}) 167 t.AssertIN(s[3], g.Slice{"k1", "k2", "v1", "v2"}) 168 s1 := gutil.MapToSlice(&m) 169 t.Assert(len(s1), 4) 170 t.AssertIN(s1[0], g.Slice{"k1", "k2", "v1", "v2"}) 171 t.AssertIN(s1[1], g.Slice{"k1", "k2", "v1", "v2"}) 172 t.AssertIN(s1[2], g.Slice{"k1", "k2", "v1", "v2"}) 173 t.AssertIN(s1[3], g.Slice{"k1", "k2", "v1", "v2"}) 174 }) 175 gtest.C(t, func(t *gtest.T) { 176 m := g.MapStrStr{ 177 "k1": "v1", 178 "k2": "v2", 179 } 180 s := gutil.MapToSlice(m) 181 t.Assert(len(s), 4) 182 t.AssertIN(s[0], g.Slice{"k1", "k2", "v1", "v2"}) 183 t.AssertIN(s[1], g.Slice{"k1", "k2", "v1", "v2"}) 184 t.AssertIN(s[2], g.Slice{"k1", "k2", "v1", "v2"}) 185 t.AssertIN(s[3], g.Slice{"k1", "k2", "v1", "v2"}) 186 }) 187 gtest.C(t, func(t *gtest.T) { 188 m := g.MapStrStr{} 189 s := gutil.MapToSlice(m) 190 t.Assert(len(s), 0) 191 }) 192 gtest.C(t, func(t *gtest.T) { 193 s := gutil.MapToSlice(1) 194 t.Assert(s, nil) 195 }) 196 }