github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/reflectx/reflectx_z_unit_test.go (about)

     1  package reflectx_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	. "github.com/onsi/gomega"
     8  
     9  	. "github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
    10  )
    11  
    12  type Foo struct {
    13  	Field int `name:"fieldNameTag,default='0'" json:"fieldJsonTag,omitempty"`
    14  }
    15  
    16  func TestNatureType(t *testing.T) {
    17  	var v, vp = Foo{}, &Foo{}
    18  
    19  	NewWithT(t).Expect(NatureType(v).String()).To(Equal("reflectx_test.Foo"))
    20  	NewWithT(t).Expect(NatureType(vp).String()).To(Equal("reflectx_test.Foo"))
    21  	NewWithT(t).Expect(NatureType(&vp).String()).To(Equal("reflectx_test.Foo"))
    22  	NewWithT(t).Expect(NatureType(reflect.TypeOf(v)).String()).To(Equal("reflectx_test.Foo"))
    23  	NewWithT(t).Expect(NatureType(reflect.TypeOf(vp)).String()).To(Equal("reflectx_test.Foo"))
    24  	NewWithT(t).Expect(NatureType(reflect.TypeOf(&vp)).String()).To(Equal("reflectx_test.Foo"))
    25  
    26  }
    27  
    28  func TestTagValueAndFlags(t *testing.T) {
    29  	ft, _ := reflect.ValueOf(Foo{}).Type().FieldByName("Field")
    30  	nameTag, _ := ft.Tag.Lookup("name")
    31  	jsonTag, _ := ft.Tag.Lookup("json")
    32  
    33  	key, flags := TagValueAndFlags(nameTag)
    34  	NewWithT(t).Expect(key).To(Equal("fieldNameTag"))
    35  	NewWithT(t).Expect(flags).To(Equal(map[string]bool{"default='0'": true}))
    36  
    37  	key, flags = TagValueAndFlags(jsonTag)
    38  	NewWithT(t).Expect(key).To(Equal("fieldJsonTag"))
    39  	NewWithT(t).Expect(flags).To(Equal(map[string]bool{"omitempty": true}))
    40  }