github.com/jxskiss/gopkg@v0.17.3/reflectx/rtype_test.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  type simple struct {
    10  	A string
    11  }
    12  
    13  func TestRType(t *testing.T) {
    14  	var (
    15  		oneI8    int8  = 1
    16  		twoI32   int32 = 2
    17  		threeInt int   = 3
    18  		strA           = "a"
    19  	)
    20  	types := []interface{}{
    21  		int8(1),
    22  		&oneI8,
    23  		int32(2),
    24  		&twoI32,
    25  		int(3),
    26  		&threeInt,
    27  		"a",
    28  		&strA,
    29  		simple{"b"},
    30  		&simple{"b"},
    31  	}
    32  	for _, x := range types {
    33  		rtype1 := EfaceOf(&x).RType
    34  		rtype2 := RTypeOf(reflect.TypeOf(x))
    35  		rtype3 := RTypeOf(reflect.ValueOf(x))
    36  		assert.Equal(t, rtype1, rtype2)
    37  		assert.Equal(t, rtype2, rtype3)
    38  	}
    39  }
    40  
    41  func TestToRType(t *testing.T) {
    42  	var x int64
    43  	rtyp1 := RTypeOf(x)
    44  	rtyp2 := ToRType(reflect.TypeOf(x))
    45  	assert.Equal(t, rtyp1, rtyp2)
    46  }
    47  
    48  func TestRTypeOf(t *testing.T) {
    49  	var x int64
    50  	typ1 := RTypeOf(x).ToType()
    51  	typ2 := reflect.TypeOf(x)
    52  	assert.Equal(t, typ1, typ2)
    53  }
    54  
    55  func TestPtrTo(t *testing.T) {
    56  	var x int64
    57  	typ1 := PtrTo(RTypeOf(x)).ToType()
    58  	typ2 := reflect.PtrTo(reflect.TypeOf(x))
    59  	assert.Equal(t, typ1, typ2)
    60  }