github.com/gogf/gf/v2@v2.7.4/util/gutil/gutil_z_unit_list_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/gogf/gf. 6 7 package gutil_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/frame/g" 13 "github.com/gogf/gf/v2/test/gtest" 14 "github.com/gogf/gf/v2/util/gutil" 15 ) 16 17 func Test_ListItemValues_Map(t *testing.T) { 18 gtest.C(t, func(t *gtest.T) { 19 listMap := g.List{ 20 g.Map{"id": 1, "score": 100}, 21 g.Map{"id": 2, "score": 99}, 22 g.Map{"id": 3, "score": 99}, 23 } 24 t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3}) 25 t.Assert(gutil.ListItemValues(&listMap, "id"), g.Slice{1, 2, 3}) 26 t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, 99, 99}) 27 }) 28 gtest.C(t, func(t *gtest.T) { 29 listMap := g.List{ 30 g.Map{"id": 1, "score": 100}, 31 g.Map{"id": 2, "score": nil}, 32 g.Map{"id": 3, "score": 0}, 33 } 34 t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3}) 35 t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, nil, 0}) 36 }) 37 gtest.C(t, func(t *gtest.T) { 38 listMap := g.List{} 39 t.Assert(len(gutil.ListItemValues(listMap, "id")), 0) 40 }) 41 } 42 43 func Test_ListItemValues_Map_SubKey(t *testing.T) { 44 type Scores struct { 45 Math int 46 English int 47 } 48 gtest.C(t, func(t *gtest.T) { 49 listMap := g.List{ 50 g.Map{"id": 1, "scores": Scores{100, 60}}, 51 g.Map{"id": 2, "scores": Scores{0, 100}}, 52 g.Map{"id": 3, "scores": Scores{59, 99}}, 53 } 54 t.Assert(gutil.ListItemValues(listMap, "scores", "Math"), g.Slice{100, 0, 59}) 55 t.Assert(gutil.ListItemValues(listMap, "scores", "English"), g.Slice{60, 100, 99}) 56 t.Assert(gutil.ListItemValues(listMap, "scores", "PE"), g.Slice{}) 57 }) 58 } 59 60 func Test_ListItemValues_Map_Array_SubKey(t *testing.T) { 61 type Scores struct { 62 Math int 63 English int 64 } 65 gtest.C(t, func(t *gtest.T) { 66 listMap := g.List{ 67 g.Map{"id": 1, "scores": []Scores{{1, 2}, {3, 4}}}, 68 g.Map{"id": 2, "scores": []Scores{{5, 6}, {7, 8}}}, 69 g.Map{"id": 3, "scores": []Scores{{9, 10}, {11, 12}}}, 70 } 71 t.Assert(gutil.ListItemValues(listMap, "scores", "Math"), g.Slice{1, 3, 5, 7, 9, 11}) 72 t.Assert(gutil.ListItemValues(listMap, "scores", "English"), g.Slice{2, 4, 6, 8, 10, 12}) 73 t.Assert(gutil.ListItemValues(listMap, "scores", "PE"), g.Slice{}) 74 }) 75 } 76 77 func Test_ListItemValues_Struct(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 type T struct { 80 Id int 81 Score float64 82 } 83 listStruct := g.Slice{ 84 T{1, 100}, 85 T{2, 99}, 86 T{3, 0}, 87 } 88 t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3}) 89 t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, 99, 0}) 90 }) 91 // Pointer items. 92 gtest.C(t, func(t *gtest.T) { 93 type T struct { 94 Id int 95 Score float64 96 } 97 listStruct := g.Slice{ 98 &T{1, 100}, 99 &T{2, 99}, 100 &T{3, 0}, 101 } 102 t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3}) 103 t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, 99, 0}) 104 }) 105 // Nil element value. 106 gtest.C(t, func(t *gtest.T) { 107 type T struct { 108 Id int 109 Score interface{} 110 } 111 listStruct := g.Slice{ 112 T{1, 100}, 113 T{2, nil}, 114 T{3, 0}, 115 } 116 t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3}) 117 t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, nil, 0}) 118 }) 119 } 120 121 func Test_ListItemValues_Struct_SubKey(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 type Student struct { 124 Id int 125 Score float64 126 } 127 type Class struct { 128 Total int 129 Students []Student 130 } 131 listStruct := g.Slice{ 132 Class{2, []Student{{1, 1}, {2, 2}}}, 133 Class{3, []Student{{3, 3}, {4, 4}, {5, 5}}}, 134 Class{1, []Student{{6, 6}}}, 135 } 136 t.Assert(gutil.ListItemValues(listStruct, "Total"), g.Slice{2, 3, 1}) 137 t.Assert(gutil.ListItemValues(listStruct, "Students"), `[[{"Id":1,"Score":1},{"Id":2,"Score":2}],[{"Id":3,"Score":3},{"Id":4,"Score":4},{"Id":5,"Score":5}],[{"Id":6,"Score":6}]]`) 138 t.Assert(gutil.ListItemValues(listStruct, "Students", "Id"), g.Slice{1, 2, 3, 4, 5, 6}) 139 }) 140 gtest.C(t, func(t *gtest.T) { 141 type Student struct { 142 Id int 143 Score float64 144 } 145 type Class struct { 146 Total int 147 Students []*Student 148 } 149 listStruct := g.Slice{ 150 &Class{2, []*Student{{1, 1}, {2, 2}}}, 151 &Class{3, []*Student{{3, 3}, {4, 4}, {5, 5}}}, 152 &Class{1, []*Student{{6, 6}}}, 153 } 154 t.Assert(gutil.ListItemValues(listStruct, "Total"), g.Slice{2, 3, 1}) 155 t.Assert(gutil.ListItemValues(listStruct, "Students"), `[[{"Id":1,"Score":1},{"Id":2,"Score":2}],[{"Id":3,"Score":3},{"Id":4,"Score":4},{"Id":5,"Score":5}],[{"Id":6,"Score":6}]]`) 156 t.Assert(gutil.ListItemValues(listStruct, "Students", "Id"), g.Slice{1, 2, 3, 4, 5, 6}) 157 }) 158 } 159 160 func Test_ListItemValuesUnique(t *testing.T) { 161 gtest.C(t, func(t *gtest.T) { 162 listMap := g.List{ 163 g.Map{"id": 1, "score": 100}, 164 g.Map{"id": 2, "score": 100}, 165 g.Map{"id": 3, "score": 100}, 166 g.Map{"id": 4, "score": 100}, 167 g.Map{"id": 5, "score": 100}, 168 } 169 t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5}) 170 t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100}) 171 }) 172 gtest.C(t, func(t *gtest.T) { 173 listMap := g.List{ 174 g.Map{"id": 1, "score": 100}, 175 g.Map{"id": 2, "score": 100}, 176 g.Map{"id": 3, "score": 100}, 177 g.Map{"id": 4, "score": 100}, 178 g.Map{"id": 5, "score": 99}, 179 } 180 t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5}) 181 t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100, 99}) 182 }) 183 gtest.C(t, func(t *gtest.T) { 184 listMap := g.List{ 185 g.Map{"id": 1, "score": 100}, 186 g.Map{"id": 2, "score": 100}, 187 g.Map{"id": 3, "score": 0}, 188 g.Map{"id": 4, "score": 100}, 189 g.Map{"id": 5, "score": 99}, 190 } 191 t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5}) 192 t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100, 0, 99}) 193 }) 194 } 195 196 func Test_ListItemValuesUnique_Struct_SubKey(t *testing.T) { 197 gtest.C(t, func(t *gtest.T) { 198 type Student struct { 199 Id int 200 Score float64 201 } 202 type Class struct { 203 Total int 204 Students []Student 205 } 206 listStruct := g.Slice{ 207 Class{2, []Student{{1, 1}, {1, 2}}}, 208 Class{3, []Student{{2, 3}, {2, 4}, {5, 5}}}, 209 Class{1, []Student{{6, 6}}}, 210 } 211 t.Assert(gutil.ListItemValuesUnique(listStruct, "Total"), g.Slice{2, 3, 1}) 212 t.Assert(gutil.ListItemValuesUnique(listStruct, "Students", "Id"), g.Slice{1, 2, 5, 6}) 213 }) 214 gtest.C(t, func(t *gtest.T) { 215 type Student struct { 216 Id int 217 Score float64 218 } 219 type Class struct { 220 Total int 221 Students []*Student 222 } 223 listStruct := g.Slice{ 224 &Class{2, []*Student{{1, 1}, {1, 2}}}, 225 &Class{3, []*Student{{2, 3}, {2, 4}, {5, 5}}}, 226 &Class{1, []*Student{{6, 6}}}, 227 } 228 t.Assert(gutil.ListItemValuesUnique(listStruct, "Total"), g.Slice{2, 3, 1}) 229 t.Assert(gutil.ListItemValuesUnique(listStruct, "Students", "Id"), g.Slice{1, 2, 5, 6}) 230 }) 231 } 232 233 func Test_ListItemValuesUnique_Map_Array_SubKey(t *testing.T) { 234 type Scores struct { 235 Math int 236 English int 237 } 238 gtest.C(t, func(t *gtest.T) { 239 listMap := g.List{ 240 g.Map{"id": 1, "scores": []Scores{{1, 2}, {1, 2}}}, 241 g.Map{"id": 2, "scores": []Scores{{5, 8}, {5, 8}}}, 242 g.Map{"id": 3, "scores": []Scores{{9, 10}, {11, 12}}}, 243 } 244 t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "Math"), g.Slice{1, 5, 9, 11}) 245 t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "English"), g.Slice{2, 8, 10, 12}) 246 t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "PE"), g.Slice{}) 247 }) 248 } 249 250 func Test_ListItemValuesUnique_Binary_ID(t *testing.T) { 251 gtest.C(t, func(t *gtest.T) { 252 listMap := g.List{ 253 g.Map{"id": []byte{1}, "score": 100}, 254 g.Map{"id": []byte{2}, "score": 100}, 255 g.Map{"id": []byte{3}, "score": 100}, 256 g.Map{"id": []byte{4}, "score": 100}, 257 g.Map{"id": []byte{4}, "score": 100}, 258 } 259 t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{[]byte{1}, []byte{2}, []byte{3}, []byte{4}}) 260 t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100}) 261 }) 262 }