github.com/gogf/gf@v1.16.9/encoding/gparser/gparser_unit_new_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 gparser_test 8 9 import ( 10 "github.com/gogf/gf/encoding/gparser" 11 "testing" 12 13 "github.com/gogf/gf/test/gtest" 14 ) 15 16 func Test_Load_NewWithTag(t *testing.T) { 17 type User struct { 18 Age int `xml:"age-xml" json:"age-json"` 19 Name string `xml:"name-xml" json:"name-json"` 20 Addr string `xml:"addr-xml" json:"addr-json"` 21 } 22 data := User{ 23 Age: 18, 24 Name: "john", 25 Addr: "chengdu", 26 } 27 // JSON 28 gtest.C(t, func(t *gtest.T) { 29 j := gparser.New(data) 30 t.AssertNE(j, nil) 31 t.Assert(j.Get("age-xml"), nil) 32 t.Assert(j.Get("age-json"), data.Age) 33 t.Assert(j.Get("name-xml"), nil) 34 t.Assert(j.Get("name-json"), data.Name) 35 t.Assert(j.Get("addr-xml"), nil) 36 t.Assert(j.Get("addr-json"), data.Addr) 37 }) 38 // XML 39 gtest.C(t, func(t *gtest.T) { 40 j := gparser.NewWithTag(data, "xml") 41 t.AssertNE(j, nil) 42 t.Assert(j.Get("age-xml"), data.Age) 43 t.Assert(j.Get("age-json"), nil) 44 t.Assert(j.Get("name-xml"), data.Name) 45 t.Assert(j.Get("name-json"), nil) 46 t.Assert(j.Get("addr-xml"), data.Addr) 47 t.Assert(j.Get("addr-json"), nil) 48 }) 49 }