github.com/gogf/gf/v2@v2.7.4/container/gvar/gvar_z_unit_vars_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 gvar_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/container/gvar" 13 "github.com/gogf/gf/v2/frame/g" 14 "github.com/gogf/gf/v2/test/gtest" 15 ) 16 17 func TestVars(t *testing.T) { 18 gtest.C(t, func(t *gtest.T) { 19 var vs = gvar.Vars{ 20 gvar.New(1), 21 gvar.New(2), 22 gvar.New(3), 23 } 24 t.AssertEQ(vs.Strings(), []string{"1", "2", "3"}) 25 t.AssertEQ(vs.Interfaces(), []interface{}{1, 2, 3}) 26 t.AssertEQ(vs.Float32s(), []float32{1, 2, 3}) 27 t.AssertEQ(vs.Float64s(), []float64{1, 2, 3}) 28 t.AssertEQ(vs.Ints(), []int{1, 2, 3}) 29 t.AssertEQ(vs.Int8s(), []int8{1, 2, 3}) 30 t.AssertEQ(vs.Int16s(), []int16{1, 2, 3}) 31 t.AssertEQ(vs.Int32s(), []int32{1, 2, 3}) 32 t.AssertEQ(vs.Int64s(), []int64{1, 2, 3}) 33 t.AssertEQ(vs.Uints(), []uint{1, 2, 3}) 34 t.AssertEQ(vs.Uint8s(), []uint8{1, 2, 3}) 35 t.AssertEQ(vs.Uint16s(), []uint16{1, 2, 3}) 36 t.AssertEQ(vs.Uint32s(), []uint32{1, 2, 3}) 37 t.AssertEQ(vs.Uint64s(), []uint64{1, 2, 3}) 38 }) 39 } 40 41 func TestVars_Scan(t *testing.T) { 42 gtest.C(t, func(t *gtest.T) { 43 type User struct { 44 Id int 45 Name string 46 } 47 var vs = gvar.Vars{ 48 gvar.New(g.Map{"id": 1, "name": "john"}), 49 gvar.New(g.Map{"id": 2, "name": "smith"}), 50 } 51 var users []User 52 err := vs.Scan(&users) 53 t.AssertNil(err) 54 t.Assert(len(users), 2) 55 t.Assert(users[0].Id, 1) 56 t.Assert(users[0].Name, "john") 57 t.Assert(users[1].Id, 2) 58 t.Assert(users[1].Name, "smith") 59 }) 60 }