gf/container/gvar/gvar_z_unit_struct_test.go

77 lines
1.5 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvar_test
import (
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/test/gtest"
2020-07-05 11:23:39 +08:00
"github.com/gogf/gf/util/gconv"
"testing"
)
2021-01-19 14:26:17 +08:00
func TestVar_Struct(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type StTest struct {
Test int
}
Kv := make(map[string]int, 1)
Kv["Test"] = 100
testObj := &StTest{}
objOne := gvar.New(Kv, true)
objOne.Struct(testObj)
t.Assert(testObj.Test, Kv["Test"])
})
gtest.C(t, func(t *gtest.T) {
type StTest struct {
Test int8
}
o := &StTest{}
v := gvar.New(g.Slice{"Test", "-25"})
v.Struct(o)
t.Assert(o.Test, -25)
})
}
2020-07-05 11:23:39 +08:00
2021-01-19 14:26:17 +08:00
func TestVar_Var_Attribute_Struct(t *testing.T) {
2020-07-05 11:23:39 +08:00
gtest.C(t, func(t *gtest.T) {
type User struct {
Uid int
Name string
}
user := new(User)
err := gconv.Struct(
g.Map{
"uid": gvar.New(1),
"name": gvar.New("john"),
}, user)
t.Assert(err, nil)
t.Assert(user.Uid, 1)
t.Assert(user.Name, "john")
})
gtest.C(t, func(t *gtest.T) {
type User struct {
Uid int
Name string
}
var user *User
err := gconv.Struct(
g.Map{
"uid": gvar.New(1),
"name": gvar.New("john"),
}, &user)
t.Assert(err, nil)
t.Assert(user.Uid, 1)
t.Assert(user.Name, "john")
})
}