github.com/wangyougui/gf/v2@v2.6.5/util/gutil/gutil_z_unit_slice_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_SliceCopy(t *testing.T) { 18 gtest.C(t, func(t *gtest.T) { 19 s := g.Slice{ 20 "K1", "v1", "K2", "v2", 21 } 22 s1 := gutil.SliceCopy(s) 23 t.Assert(s, s1) 24 }) 25 } 26 27 func Test_SliceDelete(t *testing.T) { 28 gtest.C(t, func(t *gtest.T) { 29 s := g.Slice{ 30 "K1", "v1", "K2", "v2", 31 } 32 t.Assert(gutil.SliceDelete(s, 0), g.Slice{ 33 "v1", "K2", "v2", 34 }) 35 t.Assert(gutil.SliceDelete(s, 5), s) 36 }) 37 } 38 39 func Test_SliceToMap(t *testing.T) { 40 gtest.C(t, func(t *gtest.T) { 41 s := g.Slice{ 42 "K1", "v1", "K2", "v2", 43 } 44 m := gutil.SliceToMap(s) 45 t.Assert(len(m), 2) 46 t.Assert(m, g.Map{ 47 "K1": "v1", 48 "K2": "v2", 49 }) 50 51 m1 := gutil.SliceToMap(&s) 52 t.Assert(len(m1), 2) 53 t.Assert(m1, g.Map{ 54 "K1": "v1", 55 "K2": "v2", 56 }) 57 }) 58 gtest.C(t, func(t *gtest.T) { 59 s := g.Slice{ 60 "K1", "v1", "K2", 61 } 62 m := gutil.SliceToMap(s) 63 t.Assert(len(m), 0) 64 t.Assert(m, nil) 65 }) 66 gtest.C(t, func(t *gtest.T) { 67 m := gutil.SliceToMap(1) 68 t.Assert(len(m), 0) 69 t.Assert(m, nil) 70 }) 71 } 72 73 func Test_SliceToMapWithColumnAsKey(t *testing.T) { 74 m1 := g.Map{"K1": "v1", "K2": 1} 75 m2 := g.Map{"K1": "v2", "K2": 2} 76 s := g.Slice{m1, m2} 77 gtest.C(t, func(t *gtest.T) { 78 m := gutil.SliceToMapWithColumnAsKey(s, "K1") 79 t.Assert(m, g.MapAnyAny{ 80 "v1": m1, 81 "v2": m2, 82 }) 83 84 n := gutil.SliceToMapWithColumnAsKey(&s, "K1") 85 t.Assert(n, g.MapAnyAny{ 86 "v1": m1, 87 "v2": m2, 88 }) 89 }) 90 gtest.C(t, func(t *gtest.T) { 91 m := gutil.SliceToMapWithColumnAsKey(s, "K2") 92 t.Assert(m, g.MapAnyAny{ 93 1: m1, 94 2: m2, 95 }) 96 }) 97 }