github.com/gogf/gf@v1.16.9/util/gmeta/gmeta_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 gmeta_test 8 9 import ( 10 "github.com/gogf/gf/internal/json" 11 "github.com/gogf/gf/util/gmeta" 12 "testing" 13 14 "github.com/gogf/gf/test/gtest" 15 ) 16 17 type A struct { 18 gmeta.Meta `tag:"123" orm:"456"` 19 Id int 20 Name string 21 } 22 23 var ( 24 a1 A 25 a2 *A 26 ) 27 28 func Benchmark_Data_Struct(b *testing.B) { 29 for i := 0; i < b.N; i++ { 30 gmeta.Data(a1) 31 } 32 } 33 34 func Benchmark_Data_Pointer1(b *testing.B) { 35 for i := 0; i < b.N; i++ { 36 gmeta.Data(a2) 37 } 38 } 39 40 func Benchmark_Data_Pointer2(b *testing.B) { 41 for i := 0; i < b.N; i++ { 42 gmeta.Data(&a2) 43 } 44 } 45 46 func Benchmark_Data_Get_Struct(b *testing.B) { 47 for i := 0; i < b.N; i++ { 48 gmeta.Get(a1, "tag") 49 } 50 } 51 52 func Benchmark_Data_Get_Pointer1(b *testing.B) { 53 for i := 0; i < b.N; i++ { 54 gmeta.Get(a2, "tag") 55 } 56 } 57 58 func Benchmark_Data_Get_Pointer2(b *testing.B) { 59 for i := 0; i < b.N; i++ { 60 gmeta.Get(&a2, "tag") 61 } 62 } 63 64 func TestMeta_Basic(t *testing.T) { 65 gtest.C(t, func(t *gtest.T) { 66 a := &A{ 67 Id: 100, 68 Name: "john", 69 } 70 t.Assert(len(gmeta.Data(a)), 2) 71 t.Assert(gmeta.Get(a, "tag").String(), "123") 72 t.Assert(gmeta.Get(a, "orm").String(), "456") 73 74 b, err := json.Marshal(a) 75 t.AssertNil(err) 76 t.Assert(b, `{"Id":100,"Name":"john"}`) 77 }) 78 }