github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zreflect/quick_test.go (about)

     1  package zreflect
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/sohaha/zlsgo"
     8  )
     9  
    10  func TestForEachMethod(t *testing.T) {
    11  	tt := zlsgo.NewTest(t)
    12  	v := ValueOf(Demo)
    13  	err := ForEachMethod(v, func(index int, method reflect.Method, value reflect.Value) error {
    14  		tt.Log(index, method.Name, value.Kind())
    15  		return nil
    16  	})
    17  	tt.NoError(err)
    18  
    19  	v = ValueOf(&Demo)
    20  	err = ForEachMethod(v, func(index int, method reflect.Method, value reflect.Value) error {
    21  		tt.Log(index, method.Name, value.Kind())
    22  		return nil
    23  	})
    24  	tt.NoError(err)
    25  }
    26  
    27  func TestForEach(t *testing.T) {
    28  	tt := zlsgo.NewTest(t)
    29  	typ := TypeOf(Demo)
    30  
    31  	err := ForEach(typ, func(parent []string, index int, tag string, field reflect.StructField) error {
    32  		return nil
    33  	})
    34  	tt.NoError(err)
    35  	err = ForEach(typ, func(parent []string, index int, tag string, field reflect.StructField) error {
    36  		tt.Log(parent, index, tag, field.Name)
    37  		return SkipChild
    38  	})
    39  	tt.NoError(err)
    40  }
    41  
    42  func TestForEachValue(t *testing.T) {
    43  	tt := zlsgo.NewTest(t)
    44  	v := ValueOf(Demo)
    45  
    46  	err := ForEachValue(v, func(parent []string, index int, tag string, field reflect.StructField, value reflect.Value) error {
    47  		tt.Log(parent, index, tag, field.Name, value.Interface())
    48  		return nil
    49  	})
    50  	tt.NoError(err)
    51  }