github.com/zhiqiangxu/util@v0.0.0-20230112053021-0a7aee056cd5/reflect_test.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"gotest.tools/assert"
     9  )
    10  
    11  func TestReflect(t *testing.T) {
    12  	var f func() int
    13  	{
    14  		ReplaceFuncVar(&f, func([]reflect.Value) []reflect.Value {
    15  			return []reflect.Value{reflect.ValueOf(30)}
    16  		})
    17  
    18  		result := f()
    19  		assert.Assert(t, result == 30)
    20  	}
    21  
    22  	{
    23  		fv := reflect.ValueOf(&f)
    24  		ReplaceFuncVar(fv, func([]reflect.Value) []reflect.Value {
    25  			return []reflect.Value{reflect.ValueOf(31)}
    26  		})
    27  
    28  		result := f()
    29  		assert.Assert(t, result == 31)
    30  	}
    31  
    32  	s := &struct {
    33  		F func() int
    34  		I int
    35  	}{}
    36  
    37  	{
    38  		ReplaceFuncVar(&s.F, func([]reflect.Value) []reflect.Value {
    39  			return []reflect.Value{reflect.ValueOf(40)}
    40  		})
    41  
    42  		result := s.F()
    43  		assert.Assert(t, result == 40)
    44  	}
    45  
    46  	{
    47  		sv := reflect.ValueOf(s)
    48  		ReplaceFuncVar(reflect.Indirect(sv).Field(0), func([]reflect.Value) []reflect.Value {
    49  			return []reflect.Value{reflect.ValueOf(41)}
    50  		})
    51  
    52  		result := s.F()
    53  		assert.Assert(t, result == 41)
    54  	}
    55  
    56  	{
    57  		sfields := StructFieldValues(s, func(_ string, field reflect.Value) bool {
    58  			return field.Kind() == reflect.Int
    59  		})
    60  		assert.Assert(t, len(sfields) == 1)
    61  		sfields["I"].Set(reflect.ValueOf(20))
    62  		assert.Assert(t, s.I == 20)
    63  	}
    64  
    65  	{
    66  		vf := Func2Value(f)
    67  		ret := vf.Call(nil)
    68  		assert.Assert(t, ret[0].Interface().(int) == 31)
    69  	}
    70  
    71  	{
    72  		inTypes := FuncInputTypes(testTarget)
    73  		assert.Assert(t, len(inTypes) == 2 && inTypes[0].Kind() == reflect.Int && inTypes[1].Kind() == reflect.String)
    74  
    75  		outTypes := FuncOutputTypes(testTarget)
    76  		assert.Assert(t, len(outTypes) == 1 && outTypes[0].Kind() == reflect.Slice)
    77  	}
    78  
    79  	{
    80  		stringType := TypeByPointer((*string)(nil))
    81  		assert.Assert(t, stringType == reflect.ValueOf("").Type())
    82  
    83  		is := InstanceByType(stringType)
    84  		_, ok := is.(string)
    85  		assert.Assert(t, ok)
    86  		isPtr := InstancePtrByType(stringType)
    87  		_, ok = isPtr.(*string)
    88  		assert.Assert(t, ok)
    89  	}
    90  
    91  	var t2 TestType
    92  	{
    93  		methods := ScanMethods(t2)
    94  		_, ok := methods["M1"]
    95  		assert.Assert(t, len(methods) == 1 && ok)
    96  
    97  		methods = ScanMethods(&t2)
    98  		_, ok = methods["M2"]
    99  		assert.Assert(t, len(methods) == 3 && ok, "%v %v", len(methods), ok)
   100  	}
   101  
   102  	{
   103  		s := "abc"
   104  		sv := reflect.ValueOf(s)
   105  		sptr := InstancePtrByClone(sv)
   106  		sp, ok := sptr.(*string)
   107  		assert.Assert(t, ok && *sp == "abc")
   108  		*sp = "def"
   109  		sp, ok = sptr.(*string)
   110  		assert.Assert(t, s == "abc" && ok && *sp == "def")
   111  	}
   112  
   113  	{
   114  		var itf interface{}
   115  		itf = t2
   116  		_, ok := itf.(interface{ M2() })
   117  		assert.Assert(t, !ok)
   118  		itf = &itf
   119  		_, ok = itf.(interface{ M2() })
   120  		assert.Assert(t, !ok)
   121  		itf = &t2
   122  		_, ok = itf.(interface{ M2() })
   123  		assert.Assert(t, ok)
   124  	}
   125  
   126  	{
   127  		a := JSON{A: 1}
   128  		b := &JSON{A: 1}
   129  		bytes1, _ := json.Marshal(a)
   130  		bytes2, _ := json.Marshal(b)
   131  		assert.Assert(t, string(bytes1) == string(bytes2))
   132  	}
   133  
   134  }
   135  
   136  type JSON struct {
   137  	A int
   138  }
   139  
   140  func testTarget(int, string) []int {
   141  	return nil
   142  }
   143  
   144  type TestType struct {
   145  	Base
   146  }
   147  
   148  type Base struct {
   149  }
   150  
   151  func (t TestType) M1() {
   152  
   153  }
   154  
   155  func (t TestType) m1() {
   156  
   157  }
   158  
   159  func (t *TestType) M2() {
   160  }
   161  
   162  func (b *Base) OK() {
   163  
   164  }