github.com/wangyougui/gf/v2@v2.6.5/util/gmeta/gmeta_z_unit_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/wangyougui/gf.
     6  
     7  package gmeta_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/json"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  	"github.com/wangyougui/gf/v2/util/gconv"
    15  	"github.com/wangyougui/gf/v2/util/gmeta"
    16  )
    17  
    18  func TestMeta_Basic(t *testing.T) {
    19  	type A struct {
    20  		gmeta.Meta `tag:"123" orm:"456"`
    21  		Id         int
    22  		Name       string
    23  	}
    24  
    25  	gtest.C(t, func(t *gtest.T) {
    26  		a := &A{
    27  			Id:   100,
    28  			Name: "john",
    29  		}
    30  		t.Assert(len(gmeta.Data(a)), 2)
    31  		t.AssertEQ(gmeta.Get(a, "tag").String(), "123")
    32  		t.AssertEQ(gmeta.Get(a, "orm").String(), "456")
    33  		t.AssertEQ(gmeta.Get(a, "none"), nil)
    34  
    35  		b, err := json.Marshal(a)
    36  		t.AssertNil(err)
    37  		t.Assert(b, `{"Id":100,"Name":"john"}`)
    38  	})
    39  }
    40  
    41  func TestMeta_Convert_Map(t *testing.T) {
    42  	type A struct {
    43  		gmeta.Meta `tag:"123" orm:"456"`
    44  		Id         int
    45  		Name       string
    46  	}
    47  
    48  	gtest.C(t, func(t *gtest.T) {
    49  		a := &A{
    50  			Id:   100,
    51  			Name: "john",
    52  		}
    53  		m := gconv.Map(a)
    54  		t.Assert(len(m), 2)
    55  		t.Assert(m[`Meta`], nil)
    56  	})
    57  }
    58  
    59  func TestMeta_Json(t *testing.T) {
    60  	type A struct {
    61  		gmeta.Meta `tag:"123" orm:"456"`
    62  		Id         int
    63  	}
    64  
    65  	gtest.C(t, func(t *gtest.T) {
    66  		a := &A{
    67  			Id: 100,
    68  		}
    69  		b, err := json.Marshal(a)
    70  		t.AssertNil(err)
    71  		t.Assert(string(b), `{"Id":100}`)
    72  	})
    73  }