github.com/gogf/gf@v1.16.9/util/gconv/gconv_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/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "testing" 12 13 "github.com/gogf/gf/frame/g" 14 "github.com/gogf/gf/test/gtest" 15 "github.com/gogf/gf/util/gconv" 16 ) 17 18 func Test_Slice(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 value := 123.456 21 t.AssertEQ(gconv.Bytes("123"), []byte("123")) 22 t.AssertEQ(gconv.Bytes([]interface{}{1}), []byte{1}) 23 t.AssertEQ(gconv.Bytes([]interface{}{300}), []byte("[300]")) 24 t.AssertEQ(gconv.Strings(value), []string{"123.456"}) 25 t.AssertEQ(gconv.Ints(value), []int{123}) 26 t.AssertEQ(gconv.Floats(value), []float64{123.456}) 27 t.AssertEQ(gconv.Interfaces(value), []interface{}{123.456}) 28 }) 29 gtest.C(t, func(t *gtest.T) { 30 s := []*gvar.Var{ 31 gvar.New(1), 32 gvar.New(2), 33 } 34 t.AssertEQ(gconv.SliceInt64(s), []int64{1, 2}) 35 }) 36 } 37 38 func Test_Slice_Empty(t *testing.T) { 39 // Int. 40 gtest.C(t, func(t *gtest.T) { 41 t.AssertEQ(gconv.Ints(""), []int{}) 42 t.Assert(gconv.Ints(nil), nil) 43 }) 44 gtest.C(t, func(t *gtest.T) { 45 t.AssertEQ(gconv.Int32s(""), []int32{}) 46 t.Assert(gconv.Int32s(nil), nil) 47 }) 48 gtest.C(t, func(t *gtest.T) { 49 t.AssertEQ(gconv.Int64s(""), []int64{}) 50 t.Assert(gconv.Int64s(nil), nil) 51 }) 52 // Uint. 53 gtest.C(t, func(t *gtest.T) { 54 t.AssertEQ(gconv.Uints(""), []uint{}) 55 t.Assert(gconv.Uints(nil), nil) 56 }) 57 gtest.C(t, func(t *gtest.T) { 58 t.AssertEQ(gconv.Uint32s(""), []uint32{}) 59 t.Assert(gconv.Uint32s(nil), nil) 60 }) 61 gtest.C(t, func(t *gtest.T) { 62 t.AssertEQ(gconv.Uint64s(""), []uint64{}) 63 t.Assert(gconv.Uint64s(nil), nil) 64 }) 65 // Float. 66 gtest.C(t, func(t *gtest.T) { 67 t.AssertEQ(gconv.Floats(""), []float64{}) 68 t.Assert(gconv.Floats(nil), nil) 69 }) 70 gtest.C(t, func(t *gtest.T) { 71 t.AssertEQ(gconv.Float32s(""), []float32{}) 72 t.Assert(gconv.Float32s(nil), nil) 73 }) 74 gtest.C(t, func(t *gtest.T) { 75 t.AssertEQ(gconv.Float64s(""), []float64{}) 76 t.Assert(gconv.Float64s(nil), nil) 77 }) 78 } 79 80 func Test_Strings(t *testing.T) { 81 gtest.C(t, func(t *gtest.T) { 82 array := []*g.Var{ 83 g.NewVar(1), 84 g.NewVar(2), 85 g.NewVar(3), 86 } 87 t.AssertEQ(gconv.Strings(array), []string{"1", "2", "3"}) 88 }) 89 } 90 91 func Test_Slice_Interfaces(t *testing.T) { 92 // map 93 gtest.C(t, func(t *gtest.T) { 94 array := gconv.Interfaces(g.Map{ 95 "id": 1, 96 "name": "john", 97 }) 98 t.Assert(len(array), 1) 99 t.Assert(array[0].(g.Map)["id"], 1) 100 t.Assert(array[0].(g.Map)["name"], "john") 101 }) 102 // struct 103 gtest.C(t, func(t *gtest.T) { 104 type A struct { 105 Id int `json:"id"` 106 Name string 107 } 108 array := gconv.Interfaces(&A{ 109 Id: 1, 110 Name: "john", 111 }) 112 t.Assert(len(array), 1) 113 t.Assert(array[0].(*A).Id, 1) 114 t.Assert(array[0].(*A).Name, "john") 115 }) 116 } 117 118 func Test_Slice_PrivateAttribute(t *testing.T) { 119 type User struct { 120 Id int `json:"id"` 121 name string `json:"name"` 122 } 123 gtest.C(t, func(t *gtest.T) { 124 user := &User{1, "john"} 125 array := gconv.Interfaces(user) 126 t.Assert(len(array), 1) 127 t.Assert(array[0].(*User).Id, 1) 128 t.Assert(array[0].(*User).name, "john") 129 }) 130 } 131 132 func Test_Slice_Structs(t *testing.T) { 133 type Base struct { 134 Age int 135 } 136 type User struct { 137 Id int 138 Name string 139 Base 140 } 141 142 gtest.C(t, func(t *gtest.T) { 143 users := make([]User, 0) 144 params := []g.Map{ 145 {"id": 1, "name": "john", "age": 18}, 146 {"id": 2, "name": "smith", "age": 20}, 147 } 148 err := gconv.Structs(params, &users) 149 t.Assert(err, nil) 150 t.Assert(len(users), 2) 151 t.Assert(users[0].Id, params[0]["id"]) 152 t.Assert(users[0].Name, params[0]["name"]) 153 t.Assert(users[0].Age, 18) 154 155 t.Assert(users[1].Id, params[1]["id"]) 156 t.Assert(users[1].Name, params[1]["name"]) 157 t.Assert(users[1].Age, 20) 158 }) 159 }