github.com/wangyougui/gf/v2@v2.6.5/container/gvar/gvar_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/wangyougui/gf. 6 7 package gvar_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/container/gvar" 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/test/gtest" 15 ) 16 17 func TestVar_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(gvar.New(listMap).ListItemValues("id"), g.Slice{1, 2, 3}) 25 t.Assert(gvar.New(listMap).ListItemValues("score"), g.Slice{100, 99, 99}) 26 }) 27 gtest.C(t, func(t *gtest.T) { 28 listMap := g.List{ 29 g.Map{"id": 1, "score": 100}, 30 g.Map{"id": 2, "score": nil}, 31 g.Map{"id": 3, "score": 0}, 32 } 33 t.Assert(gvar.New(listMap).ListItemValues("id"), g.Slice{1, 2, 3}) 34 t.Assert(gvar.New(listMap).ListItemValues("score"), g.Slice{100, nil, 0}) 35 }) 36 } 37 38 func TestVar_ListItemValues_Struct(t *testing.T) { 39 gtest.C(t, func(t *gtest.T) { 40 type T struct { 41 Id int 42 Score float64 43 } 44 listStruct := g.Slice{ 45 T{1, 100}, 46 T{2, 99}, 47 T{3, 0}, 48 } 49 t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3}) 50 t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, 99, 0}) 51 }) 52 // Pointer items. 53 gtest.C(t, func(t *gtest.T) { 54 type T struct { 55 Id int 56 Score float64 57 } 58 listStruct := g.Slice{ 59 &T{1, 100}, 60 &T{2, 99}, 61 &T{3, 0}, 62 } 63 t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3}) 64 t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, 99, 0}) 65 }) 66 // Nil element value. 67 gtest.C(t, func(t *gtest.T) { 68 type T struct { 69 Id int 70 Score interface{} 71 } 72 listStruct := g.Slice{ 73 T{1, 100}, 74 T{2, nil}, 75 T{3, 0}, 76 } 77 t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3}) 78 t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, nil, 0}) 79 }) 80 } 81 82 func TestVar_ListItemValuesUnique(t *testing.T) { 83 gtest.C(t, func(t *gtest.T) { 84 listMap := g.List{ 85 g.Map{"id": 1, "score": 100}, 86 g.Map{"id": 2, "score": 100}, 87 g.Map{"id": 3, "score": 100}, 88 g.Map{"id": 4, "score": 100}, 89 g.Map{"id": 5, "score": 100}, 90 } 91 t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5}) 92 t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100}) 93 }) 94 gtest.C(t, func(t *gtest.T) { 95 listMap := g.List{ 96 g.Map{"id": 1, "score": 100}, 97 g.Map{"id": 2, "score": 100}, 98 g.Map{"id": 3, "score": 100}, 99 g.Map{"id": 4, "score": 100}, 100 g.Map{"id": 5, "score": 99}, 101 } 102 t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5}) 103 t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100, 99}) 104 }) 105 gtest.C(t, func(t *gtest.T) { 106 listMap := g.List{ 107 g.Map{"id": 1, "score": 100}, 108 g.Map{"id": 2, "score": 100}, 109 g.Map{"id": 3, "score": 0}, 110 g.Map{"id": 4, "score": 100}, 111 g.Map{"id": 5, "score": 99}, 112 } 113 t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5}) 114 t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100, 0, 99}) 115 }) 116 }